Panel在继承的Windows窗体上的行为不正确?

Sun*_*000 6 c# anchor inheritance controls winforms

我需要一个解决方法,为继承的Windows窗体上的Panel不正确的行为.我有几个按钮应该固定在面板的右下角,但它们的位置以我无法在继承表格上预测的方式变化.我已经尝试了Dock和Anchor属性的各种组合,但到目前为止没有任何东西使按钮显示在它们应该的位置.所有控件都标记为Protected.

基本表单本身的行为是正确的.不正确的行为仅在继承的表单上发生; 按钮锚定到其父面板.

如果我将按钮固定在左上角,我可以将继承的表单调整得更大,最终按钮将被覆盖,在基本表单大小之外的某处.如果我将按钮固定在右下角,我就无法使表单足够大,无法揭开按钮.

我已经尝试创建一个继承Panel和GroupBox的自定义面板,但这些没有任何不同.我有同样的运气将按钮直接放在表格上,没有容器.

我想要的是:一个可调整大小的表格底部的面板,其右下角有我的按钮.表单的其余部分应该是DataGridView的可调整大小的区域(我已经解决了这个的继承和对接问题>: - /)无论窗口大小如何,按钮应始终位于右下角.我不想调整按钮的大小.

如果我必须自己调整控件的大小,我愿意这样做.但是如果框架能够正确地完成它,我只需要学习如何正确使用它,我宁愿接受教育.

我发现这个链接似乎描述了类似的不当行为:如何阻止按钮移动继承的形式.它没有得到回答.


这是从我遇到问题的程序中提取的一些代码.

基本形式

// 
// refreshButton
// 
this.refreshButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.refreshButton.Location = new System.Drawing.Point(200, 4);
this.refreshButton.Name = "refreshButton";
this.refreshButton.Size = new System.Drawing.Size(75, 23);
this.refreshButton.TabIndex = 5;
this.refreshButton.Text = "&Refresh";
this.refreshButton.UseVisualStyleBackColor = true;
// 
// saveButton
// 
this.saveButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.saveButton.Location = new System.Drawing.Point(281, 5);
this.saveButton.Name = "saveButton";
this.saveButton.Size = new System.Drawing.Size(75, 23);
this.saveButton.TabIndex = 4;
this.saveButton.Text = "&Save";
this.saveButton.UseVisualStyleBackColor = true;
// 
// buttonPanel
// 
this.buttonPanel.Controls.Add(this.saveButton);
this.buttonPanel.Controls.Add(this.refreshButton);
this.buttonPanel.Dock = System.Windows.Forms.DockStyle.Bottom;
this.buttonPanel.Location = new System.Drawing.Point(0, 231);
this.buttonPanel.Name = "buttonPanel";
this.buttonPanel.Size = new System.Drawing.Size(360, 31);
this.buttonPanel.TabIndex = 6;
Run Code Online (Sandbox Code Playgroud)

继承表单(调整大小以适合我的数据内容)

// 
// refreshButton
// 
this.refreshButton.Location = new System.Drawing.Point(286, 7);
this.refreshButton.TabIndex = 0;
// 
// saveButton
// 
this.saveButton.Location = new System.Drawing.Point(367, 7);
this.saveButton.TabIndex = 1;
// 
// buttonPanel
// 
this.buttonPanel.Location = new System.Drawing.Point(0, 232);
this.buttonPanel.Size = new System.Drawing.Size(445, 33);
Run Code Online (Sandbox Code Playgroud)

编辑:使用它后的一些额外数据.问题似乎是Visual Studio 2005设计器中的一个错误,或者设计器和C#编译器之间的奇怪交互.当我创建我继承的表单时,按钮被重定位到表单边缘的某个任意(但不是随机!)位置.按钮仍然可用,并可使用VS Properties窗口的控制下拉菜单进行选择.

我可以选择控件并修复它们的位置,使它们显示在设计图面和运行时的正确位置.当我调整表单大小时,它们甚至可以正确移动.但是,在构建之后,设计者会修改按钮的可见位置.*.Designer.cs文件中的代码仍然正确,但按钮属性已更改.在上面的继承形式中,设计者的refreshButton和saveButton的Location属性是371,7和452,7,即使包含的面板只有445像素宽.

至少这些新信息给了我一个部分修复,但我仍然不知道它为什么会发生.


答:嗯,事实证明按钮实际上确实固定在面板上.不幸的是,设计师将锚点位置更改为远离表单可见区域的某个位置.结论是VS2005设计器不可靠,并且不能正确处理继承的表单.

我推断的解决方法是覆盖OnResize()基本表单中的方法并更正按钮的位置.这是一个小小的黑客,但满足我的需求.

/// <summary>
/// Must handle some layout operations manually because Visual Studio 
/// 2005 arbitrarily changes some properties of inherited controls.
/// </summary>
/// <param name="e">Data for event.</param>
protected override void OnResize(EventArgs e)
{
    base.OnResize(e);

    // Move Refresh and Save buttons to lower right corner of button panel.
    saveButton.Top = buttonPanel.Bounds.Height -
        (saveButton.Height + saveButton.Padding.Bottom + saveButton.Margin.Bottom);
    saveButton.Left = buttonPanel.Bounds.Width -
        (saveButton.Width + saveButton.Padding.Right + saveButton.Margin.Right);
    refreshButton.Top = saveButton.Top;
    refreshButton.Left = saveButton.Left -
        (refreshButton.Width + refreshButton.Padding.Right + refreshButton.Margin.Right);
}
Run Code Online (Sandbox Code Playgroud)

Ter*_*ver 2

让我们看一些重复该行为的代码。这段代码没有:

using System;
using System.Drawing;
using System.Windows.Forms;

class FormBase : Form
{
    public FormBase()
    {
        Panel panel;
        Controls.Add(panel = new Panel { Dock = DockStyle.Bottom, Height = 120, BackColor = Color.LightGray });
        panel.Controls.Add(new Button { Text = "Button 1", Anchor = AnchorStyles.Bottom | AnchorStyles.Right, Location = new Point(panel.ClientSize.Width - 80, panel.ClientSize.Height - 60) });
        panel.Controls.Add(new Button { Text = "Button 2", Anchor = AnchorStyles.Bottom | AnchorStyles.Right, Location = new Point(panel.ClientSize.Width - 80, panel.ClientSize.Height - 30) });
    }
}

class FormInherited : FormBase
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new FormInherited());
    }
}
Run Code Online (Sandbox Code Playgroud)