【问题标题】:C# Form inheritance issueC#表单继承问题
【发布时间】:2014-01-19 10:39:09
【问题描述】:

我有一个带有标签和 2 个按钮的基本表单,当我在子表单中继承它时,它只获取标签而不获取按钮

我不知道为什么它会继承一个控件而不是另一个,是吗?

我对此做了什么:

1- 将基本形式的控件修饰符更改为受保护然后公开,仍然只有标签被继承

2- 创建了一个新的空测试项目,其中包含一个文本框和一个按钮以及一个继承它的子窗体,它工作正常,即两个控件都出现在子窗体上

3-回到我的项目,我删除了表单(父和子)重新创建了带有标签和默认设置的 2 个按钮的基本表单(没有像字体、文本或大小这样的自定义),然后创建了继承它的子表单,直到这里它工作正常所有控件都出现在子窗体上。一旦我自定义了父构造函数上的控件,按钮从子窗体中消失,只剩下标签

代码示例:

VS2012自动生成的MainEdit表单设计文件

private System.Windows.Forms.Label editheader_lbl;
private System.Windows.Forms.Button cancel_btn;
private System.Windows.Forms.Button save_btn
private void InitializeComponent()
    {
        this.editheader_lbl = new System.Windows.Forms.Label();
        this.cancel_btn = new System.Windows.Forms.Button();
        this.save_btn = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        // editheader_lbl
        // 
        this.editheader_lbl.Anchor = System.Windows.Forms.AnchorStyles.Top;
        this.editheader_lbl.AutoSize = true;
        this.editheader_lbl.Location = new System.Drawing.Point(164, 11);
        this.editheader_lbl.Name = "editheader_lbl";
        this.editheader_lbl.Size = new System.Drawing.Size(44, 13);
        this.editheader_lbl.TabIndex = 7;
        this.editheader_lbl.Text = "edit info";
        this.editheader_lbl.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
        // 
        // cancel_btn
        // 
        this.cancel_btn.Location = new System.Drawing.Point(321, 278);
        this.cancel_btn.Name = "cancel_btn";
        this.cancel_btn.Size = new System.Drawing.Size(75, 23);
        this.cancel_btn.TabIndex = 9;
        this.cancel_btn.Text = "cancel";
        this.cancel_btn.UseVisualStyleBackColor = true;
        // 
        // save_btn
        // 
        this.save_btn.Location = new System.Drawing.Point(143, 278);
        this.save_btn.Name = "save_btn";
        this.save_btn.Size = new System.Drawing.Size(75, 23);
        this.save_btn.TabIndex = 8;
        this.save_btn.Text = "save";
        this.save_btn.UseVisualStyleBackColor = true;
        // 
        // MainEdit
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(533, 331);
        this.ControlBox = false;
        this.Controls.Add(this.cancel_btn);
        this.Controls.Add(this.save_btn);
        this.Controls.Add(this.editheader_lbl);
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
        this.Name = "MainEdit";
        this.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
        this.RightToLeftLayout = true;
        this.ShowIcon = false;
        this.ShowInTaskbar = false;
        this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
        this.TopMost = true;
        this.ResumeLayout(false);
        this.PerformLayout();

    }

MainEdit.cs

public MainEdit()
    {
        InitializeComponent();
        this.Text = string.Empty;
        this.editheader_lbl.Font = Program.font_l;
        this.save_btn.Font = Program.font_btn;
        this.save_btn.Size = Program.btnSize;
        this.save_btn.Text = Constants.SAVE;
        this.cancel_btn.Font = Program.font_btn;
        this.cancel_btn.Size = Program.btnSize;
        this.cancel_btn.Text = Constants.CANCEL;
    }

Edit_Employee.cs .. 子窗体

public partial class Edit_Employee : MainEdit
{
    public Edit_Employee()
    {
        InitializeComponent();
    }
}

更新

我在移动时意外按下了鼠标按钮,它在子窗体上形成了一个选择区域。按钮应该有 2 个锁符号,我想这意味着按钮在那里,但我看不到它们! 我会去尝试我到目前为止收到的建议并及时更新,感谢迄今为止帮助过的所有人

更新-2

尝试逐一添加自定义项,以查看导致此问题的原因,它是尺寸线。虽然它在运行时采用了正确的大小,但在设计时它就像在孩子中设置为 0,0!我在按钮上获得了所有相邻的调整大小点,我在父级中应用了大小,而另一个按钮正常显示。

仍然看不到尺寸编辑导致问题的原因,但我想@Chiel92 解决了这个问题希望您将您的建议作为答案发布,以便我接受并关闭此线程

再次感谢

【问题讨论】:

  • 不继承怎么办?你的意思是按钮在子窗体上不可见?
  • @Chiel92 是的,标签在子窗体上,但没有按钮。当我将默认私有更改为受保护时,我可以通过代码访问按钮,但它们仍然没有显示在表单上
  • 您确定按钮的位置在子表单大小范围内吗?
  • 你说它使用默认值,所以一个一个重做你的自定义,看看按钮在什么时候消失。
  • 按钮可能与继承形式中的其他东西重叠,可能是面板。

标签: c# winforms visual-studio-2012 windows-forms-designer


【解决方案1】:

也许你必须调用基础构造函数:

public partial class Edit_Employee : MainEdit
{
    public Edit_Employee() : base() // See this line.
    {
        InitializeComponent();
    }
}

然后你就不需要调用 InitializeComponent()。

【讨论】:

    【解决方案2】:

    我认为您的问题来自主窗体的自动生成部分,因为拖放 Visual Studio 会将主窗体的部分生成为私有尝试定义应在主类中继承的所有控件,而不是设计师生成的部分类似这样的

    MainEdit.cs(不是visual studio生成的部分)

    public partial class  MainEdit
    {
    protected System.Windows.Forms.Label editheader_lbl;
    protected System.Windows.Forms.Button cancel_btn;
    protected System.Windows.Forms.Button save_btn
    public MainEdit()
        {
            InitializeComponent();
            this.Text = string.Empty;
            this.editheader_lbl.Font = Program.font_l;
            this.save_btn.Font = Program.font_btn;
            this.save_btn.Size = Program.btnSize;
            this.save_btn.Text = Constants.SAVE;
            this.cancel_btn.Font = Program.font_btn;
            this.cancel_btn.Size = Program.btnSize;
            this.cancel_btn.Text = Constants.CANCEL;
        }
    }
    

    【讨论】:

    • 我尝试将修饰符更改为受保护的,我可以通过代码访问控件,但它们仍然没有显示在表单上。还是你的意思是我根本不应该使用自动生成的表单,而只是自己编写?
    • 你的意思是我根本不应该使用自动生成的表格,而是自己写吗?是的,这就是我的意思
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-11
    • 2011-09-01
    • 1970-01-01
    相关资源
    最近更新 更多