繁体   English   中英

C#表单继承问题

[英]C# Form inheritance issue

我有一个带有标签和2个按钮的基本表单,当我以子表单形式继承它时,它仅获取标签,而没有按钮

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

我试图做的是:

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

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

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解决,希望您将您的建议发布为答案,以便我可以接受并关闭此线程

再次感谢

也许您必须调用基本构造函数:

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

然后,您不需要调用InitializeComponent()。

我认为您的问题来自主窗体的自动生成部分,就像拖放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;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM