C#表单继承问题

时间:2014-01-19 10:39:10

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

我有一个带有标签的基本表格& 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解决的,希望你发表你的建议作为答案,所以我可以接受它并关闭这个帖子

再次感谢

2 个答案:

答案 0 :(得分:0)

我认为您的问题来自主窗体的自动生成部分,因为拖放视觉工作室将生成主窗体的部分作为私有尝试来定义应该在主类中继承的所有控件而不是设计师生成的部分内容如此

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;
    }
}

答案 1 :(得分:0)

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

def chunks(alist, n):
    i = 0
    j = n
    while j < (len(alist) + 2):
        sub = alist[i:j]
        i += n
        j += n
        return sub

for chunk in chunks([1, 2, 3, 4, 5], 2):
    print(*chunk)

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