Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#许多父母组成一个孩子_C#_C# 4.0 - Fatal编程技术网

c#许多父母组成一个孩子

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

我有家长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 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>而不是
私有形式的logicalParentpubText
的属性!最好在子构造函数中包含该部件。既然这就是原因,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;
    }