c#许多父母组成一个孩子
我有家长1、家长2、家长3等,并且有一个孩子表格 在父项_1、父项_2、父项_3中。。。我有:c#许多父母组成一个孩子,c#,c#-4.0,C#,C# 4.0,我有家长1、家长2、家长3等,并且有一个孩子表格 在父项_1、父项_2、父项_3中。。。我有: private string strText; public string pubText { get { return strText; } set { strText = value; } } 打开按钮: private void btbutton1_Click(object sender, EventArgs e) { var form = new
private string strText;
public string pubText {
get { return strText; }
set { strText = value; } }
打开按钮:
private void btbutton1_Click(object sender, EventArgs e)
{
var form = new fChild(this);
form.ShowDialog();
}
在子表单中,我有以下代码:
private Parent_1 logicalParent;
public fChild(Parent_1 parent)
{
InitializeComponent();
logicalParent = parent;
this.FormClosed += new FormClosedEventHandler(child_FormClosed);
}
及
这只适用于1个父窗体,如何将其用于其他父窗体???
请提供帮助您可以为三个父窗体创建公共界面:
public interface IParentForm
{
string PubText {get; set;}
}
public class Parent_1 : Form, IParentForm
{
public string PubText
{
get { return this.pubText; }
set { this.pubText = value; }
}
}
//same for Parent_2 and 3
然后在子窗体中声明
logicalParent
类型为IParentForm
,并将子窗体的构造函数更改为public fChild(IParentForm父窗体)
,单击按钮执行以下操作:
private void btbutton1_Click(object sender, EventArgs e)
{
var form = new fChild(this); // this is not more needed
form.ShowDialog();
pubText = form.pubText;
}
与上述等级制度最接近的类比是什么<代码>消息框对话框?然后,您不应该将
Parent\u 1
类型传递给child,而是传递所有父类的一些基本类型。也许<代码>表单会做的,除非你必须投很多,然后考虑使用接口/ BASE类型。使用<代码>私有PARTENT1逻辑父;code>而不是私有形式的logicalParent要设置属性,请使用GetType()和set value!或者,您可以对所有父窗体使用公共接口,并在接口中保留pubText
的属性!最好在子构造函数中包含该部件。既然这就是原因,OP为什么要问这个问题?你能告诉我-var form=new fChild();在“()”@DavidMamulashvili内,如果您的父窗体实现了IParentForm,您应该仍然能够使用var form=new fChild(this)
,这只适用于一个父窗体。我想要从孩子到父母的字符串
private void btbutton1_Click(object sender, EventArgs e)
{
var form = new fChild(this); // this is not more needed
form.ShowDialog();
pubText = form.pubText;
}