简体   繁体   English

将实例的成员从一个Windows窗体传递到另一个窗口

[英]Passing members of an instance from one windows form to another

I have a main windows form (MainForm.cs) where I created an instance of Customer cust. 我有一个主要的Windows窗体(MainForm.cs),在其中创建了Customer cust的实例。

Here is a snippet of said code: 这是上述代码的片段:

private Customer cust;

public MainForm()
{
    InitializeComponent();
}

private void buttonDeposit_Click(object sender, EventArgs e)
{
    DepositDialog dlg = new DepositDialog();

    dlg.ShowDialog();
}

Here is the code for the Customer class. 这是Customer类的代码。 As you can see, it creates a list of BankAccounts: 如您所见,它将创建一个BankAccounts列表:

class Customer
{
    private BankAccountCollection accounts;

    public Customer(BankAccountCollection accounts, TransactionCollection transactionHistory)
    {
        accounts.Add(new SavingsAccount(true,200));
        accounts.Add(new SavingsAccount(true, 1000));
        accounts.Add(new LineOfCreditAccount(true, 0));
    }

    public BankAccountCollection Accounts
    { get { return accounts; }}
}

Now, I have another form called DepositDialog, which has a comboBox within it. 现在,我有另一个名为DepositDialog的窗体,其中包含一个comboBox。

How would I: 我将如何:

1) pass the data BankAccountCollection accounts 1)传递数据BankAccountCollection帐户

2) populate that comboBox with the members of that BankAccountCollection 2)用那个BankAccountCollection的成员填充那个comboBox

3) display that collection as items within the list? 3)将该集合显示为列表中的项目?

You just make use of parameterise constructor ans pass the Collection as argument may do the task for you 您只需使用参数化构造函数ans传递Collection,因为参数可以为您完成任务

private void buttonDeposit_Click(object sender, EventArgs e) 
{     
   DepositDialog dlg = new DepositDialog(cust.accounts);      
   dlg.ShowDialog(); 
} 

Check this for passing argument : C# Using New Windows Form Example 检查此参数是否传递: C#使用新的Windows窗体示例

1) pass the data BankAccountCollection accounts 1)传递数据BankAccountCollection帐户

There's actually 5 ways to pass the data. 实际上有5种方式传递数据。

1- (Not recommended if there's too many parameters) Passing data through the constructor. 1-(如果参数太多,不建议使用)通过构造函数传递数据。

 private void ShowForm(int a, string b, double c)
{
    Form2 frm = new Form2(a, b, c);
    frm.ShowDialog();
}

2- Using public fields of target class. 2-使用目标类别的公共字段。 (NOT RECOMMENDED AT ALL) (完全不推荐)

 private void ShowForm(int a, string b, double c)
{
    Form2 frm = new Form2();
    frm.intval = a;
    frm.strval = b;
    frm.doubleval = c;
    frm.ShowDialog();
} 

3- Using properties. 3-使用属性。

 private void ShowForm(int a, string b, double c)
{
    Form2 frm = new Form2();
    frm.IntValue = a;
    frm.StringValue = b;
    frm.DoubleValue = c;
    frm.ShowDialog();
} 

4- Using tags. 4-使用标签。

private void ShowForm(int a, string b, double c)
{
        Form2 frm = new Form2();
        frm.SomeTextBox.Tag = a;
        frm.SomeTextBox2.Tag = b;
        frm.SomeTextBox3.Tag = c;
        frm.ShowDialog();
} 

5- Using delegates. 5-使用代表。 (This one is a little bit tricky). (这有点棘手)。

 //in Form2
public delegate void PassValues(int a, string b, double c);
public PassValues passVals;

private void PassDataThroughDelegate(int a, string b, double c)
{
    if(passVals != null)
        passVals(a,b,c);
}

//in Form1
private void ShowForm(int a, string b, double c)
{
    Form2 frm = new Form2();
    frm.passVals = new Form2.PassValues(UseData);
    frm.ShowDialog();
}

private void UseData(int a, string b, double c)
{
} 

My personal favorite ones are the properties, delegates and in some rare cases constructors. 我个人最喜欢的是属性,委托以及在极少数情况下的构造函数。

Alternatively, you can create a static class , put some properties in it, then use it in other forms. 或者,您可以创建一个静态类,在其中添加一些属性,然后以其他形式使用它。 This is really helpful if all of your forms need to share some information. 如果您所有的表单都需要共享一些信息,这将非常有用。 Since this is not a way to Pass data between the forms, I did not mention this method in those above. 由于这不是在表单之间传递数据的方法,因此我在上述内容中未提及此方法。

2) populate that comboBox with the members of that BankAccountCollection 2)用那个BankAccountCollection的成员填充那个comboBox

Once you passed the data between forms, using it for population is not hard. 在表单之间传递数据后,将其用于填充并不难。

foreach(BankAccount acc in accounts)
   combobox1.Items.Add(acc.ToString());

3) display that collection as items within the list? 3)将该集合显示为列表中的项目?

You can use event handler for combobox1 to do whatever you want with the selected item. 您可以使用combobox1的事件处理程序对选定项执行任何操作。

Hope it helps. 希望能帮助到你。

You forgot a few other ones... 你忘了其他一些...

My favorite - Make a custom 'Initialize()' function to set the data, and then open the form normally using ShowDialog(). 我最喜欢的-创建一个自定义的“ Initialize()”函数来设置数据,然后使用ShowDialog()通常打开表单。 Then you can have many forms implement the interface, and show them dynamically. 然后,您可以使许多表单实现该接口,并动态显示它们。

private Customer Customer { get ; set; }
public void Initialize(Customer cust) {
    this.Customer = cust;
}


var f = new CustomerForm();
f.Initialize(_myCustomer);
f.ShowDialog();

You can override the ShowDialog() function, but that leaves you with three overrides now, which may or may not be acceptable. 您可以覆盖ShowDialog()函数,但是现在剩下三个覆盖,可能会接受,也可能无法接受。 Also override the one with the owner property if you need it. 如果需要,还可以使用owner属性覆盖该属性。

public void ShowDialog(Customer cust) {
     this.Customer = cust;
     base.ShowDialog();
}

You can hide the old ShowDialog() to prevent people from calling it. 您可以隐藏旧的ShowDialog()以防止人们调用它。 This can be escaped by simply casting the type as Form , so it's not really a solution. 可以通过将类型强制转换为Form来进行转义,因此这并不是真正的解决方案。

new public void ShowDialog() { 
     throw new Exception("arg!");
}

(new CustomerForm()).ShowDialog();  // exception!
(new CustomerForm() as Form).ShowDialog()  // works fine

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM