Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.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中调用具有相似参数的多个不同Winforms_C#_Winforms - Fatal编程技术网

C# 如何在C中调用具有相似参数的多个不同Winforms

C# 如何在C中调用具有相似参数的多个不同Winforms,c#,winforms,C#,Winforms,我有下列表格 弗姆贝斯 frmChild1,frmChild2….,frmChild20 共21张表格。从frmBase,我将根据条件调用按钮单击时的所有其他表单。它们都有3组参数。例如: frmChild1 objForm = new frmChild1(); objForm.strName = txtName.Text1; objForm.strAddr = txtAddress.Text2; objForm.strCity = txtCity.Text2; objForm.ShowDial

我有下列表格

弗姆贝斯 frmChild1,frmChild2….,frmChild20

共21张表格。从frmBase,我将根据条件调用按钮单击时的所有其他表单。它们都有3组参数。例如:

frmChild1 objForm = new frmChild1();
objForm.strName = txtName.Text1;
objForm.strAddr = txtAddress.Text2;
objForm.strCity = txtCity.Text2;
objForm.ShowDialog();

如果我像这样调用所有表单,我有大量的编码。由于只有表单名称在更改,是否有其他简单的方法从基本表单按钮单击调用所有20个表单。

您可以尝试以下代码

解决方案1:

您可以调用此静态函数来创建子窗体,如下所示

objForm = ObjectFinder.CreateForm(frmChild1, strName, strAddr, strCity);
objForm.ShowDialog();
解决方案2:

或者,只需创建表单对象的扩展方法,如下所示

public static Form CreateForm(this Form objForm, string strName, string strAddr, string strCity)
{
    // Return the instance of the form by specifying its name.
    var objChild = (objForm)CreateObjectInstance(objForm.Name);
    objChild.strName = txtName.Text1;
    objChild.strAddr = txtAddress.Text2;
    objChild.strCity = txtCity.Text2;
    return objChild;
}
并且,如下所示调用此扩展方法

objForm = frmChild1.CreateForm(strName, strAddr, strCity);
objForm.ShowDialog();

表单应该实现您定义的公共接口,然后您可以遍历接口列表,而不管每个表单对接口有什么实现。做你想做的事。例如,类似这样的事情:

public interface IMyForm
{
    string strName { get; set; }
    string strAddress {get; set; }
    string strCity { get; set; }
    void ShowDialog();
}
然后,如果您的所有表单都实现了该接口,那么您的主表单可以在IMyForm列表中保留其所有子表单的列表,只需循环并分配/显示所有内容

如果所有表单都具有相同的控件,则还可以编写一个基本表单,并从该表单继承其他表单,并维护相同类型的列表。
如果表单在标题中的文本不同,则应该编写1个窗体,该窗体具有所有表单实例所需的控件,并将不同的表单标题传递到其创建中。

取决于您需要什么,但是您应该考虑将所有表单添加到列表中,创建一个方法或在构造函数上工作。


其优点可能是,您可以使用枚举器或foreach调用它们, 您可以将字典添加到主窗体,该字典将保存下拉列表中的选定项作为其键,并将窗体的构造函数作为其值:

private Dictionary<object, Func<IMyForm>> _SubForms = new Dictionary<object, Func<IMyForm>>()
{
    {"Form1", () => {return new Form1();}},
    {"Form2", () => {return new Form2();}
    //....
};

列出所需的表单类型,并通过循环实例化它们。但是使用ShowDialog,您一次只能显示一个表单。您不能同时对多个表单使用ShowDialog。。。一次实例化所有表单有什么意义?@ZoharPeled,不是一次实例化所有表单。我在按钮上方有一个下拉菜单。在该下拉列表中,我将在填充文本框后选择一个表单,当我单击该按钮时,相应的表单应打开。@Lennart,我知道使用ShowDialog一次只能显示一个表单。void ShowDialog;如果所有元素仍然从表单继承,则可以跳过。
private Dictionary<object, Func<IMyForm>> _SubForms = new Dictionary<object, Func<IMyForm>>()
{
    {"Form1", () => {return new Form1();}},
    {"Form2", () => {return new Form2();}
    //....
};
var selectedItem = MyDropDown.SelectedItem;
if(_SubForms.ContainsKey(selectedItem)
{
    var form = _SubForms[selectedItem]();
    form.strName = txtName.Text1;
    form.strAddr = txtAddress.Text2;
    form.strCity = txtCity.Text2;
    form.ShowDialog();
}