2014-01-19 37 views
0

我有一个基本形式与标签& 2个按钮,当我在孩子的形式继承它,它只是变得标签,但不是按钮C#形式的继承问题

我不知道为什么它继承一个控制而不是其他,你呢?

什么我试图做这件事:

1-改变了基本表单控件修饰符来保护,那么公众,仍然只有标签被继承

2-装箱一个新的空测试项目有从一个文本框和一个按钮&子窗体继承它,它工作正常,即这两个控件出现在子窗体

3-回到我的项目,我删除的形式(父&子)重建的基本形式带有标签和2个默认设置按钮(无需定制l ike字体,文本或大小)然后创建子窗体继承它,直到这里它工作正常所有控件出现在子窗体上..一旦我定制父控件的构造函数上的按钮消失从孩子只有标签仍然

代码示例:

通过VS2012

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产生MainEdit表单设计文件自动

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解决希望你发布你的建议的答案,所以我可以再次接受它,并关闭该线程

感谢

+1

它是如何不继承?你的意思是这些按钮在子窗体上不可见? –

+0

@ Chiel92是的,标签贴在儿童窗体上,但没有按钮。当我将默认私人更改为受保护的时,我可以通过代码访问按钮,但它们仍未显示在表单上 – Katia

+0

您确定按钮的位置在子窗体大小内吗? –

回答

0

我认为你的问题来自你的主窗体的自动生成部分,因为拖放Visual Studio将生成主窗体的各个部分作为私人尝试定义应该在你的主类中继承的所有控件而不是由设计师生成的部分内容如下

MainEdit。cs(不是由视觉工作室生成的部分视频)

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

我尝试将修改器更改为受保护的,我可以通过代码访问控件,但它们仍未在窗体上显示。或者你的意思是我不应该使用自动生成的表单,只是写我自己的? – Katia

+0

你的意思是我不应该使用自动生成的表单,只是写我自己的?是的,我的意思是 –