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

发布于 2024-12-15 15:59:25 字数 976 浏览 2 评论 0 原文

我有一个主窗口窗体 (MainForm.cs),我在其中创建了一个 Customer cust 实例。

这是上述代码的片段:

private Customer cust;

public MainForm()
{
    InitializeComponent();
}

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

    dlg.ShowDialog();
}

这是 Customer 类的代码。正如您所看到的,它创建了一个 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; }}
}

现在,我有另一个名为 DepositDialog 的表单,其中有一个组合框。

我将如何:

1)传递数据BankAccountCollection帐户

2)用该BankAccountCollection的成员填充该组合框

3)将该集合显示为列表中的项目?

I have a main windows form (MainForm.cs) where I created an instance of 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. As you can see, it creates a list of 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.

How would I:

1) pass the data BankAccountCollection accounts

2) populate that comboBox with the members of that BankAccountCollection

3) display that collection as items within the list?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风月客 2024-12-22 15:59:25

您只需使用参数化构造函数并传递集合作为参数即可为您完成任务

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

检查此参数以传递参数: C# 使用新的 Windows 窗体示例

You just make use of parameterise constructor ans pass the Collection as argument may do the task for you

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

极致的悲 2024-12-22 15:59:25

1)传递BankAccountCollection账户数据

实际上有5 种方式来传递数据。

1-(如果参数太多,不推荐)通过构造函数传递数据。

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

2-使用目标类的公共字段。 (完全不推荐)

 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- 使用属性。

 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-使用标签。

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- 使用代表。 (这个有点棘手)。

 //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)
{
} 

我个人最喜欢的是属性、委托以及在极少数情况下的构造函数。

或者,您可以创建一个静态类,在其中放入一些属性,然后以其他形式使用它。
如果您的所有表单都需要共享一些信息,这确实很有帮助。由于这不是在表单之间传递数据的方法,因此我在上面没有提到此方法。

2) 用该组合框的成员填充该组合框
银行账户集合

一旦在表单之间传递数据,将其用于填充并不难。

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

3)将该集合显示为列表中的项目?

您可以使用组合框 1 的事件处理程序对所选项目执行任何您想要的操作。

希望有帮助。

1) pass the data BankAccountCollection accounts

There's actually 5 ways to pass the data.

1- (Not recommended if there's too many parameters) Passing data through the constructor.

 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. (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.

 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.

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. (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

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?

You can use event handler for combobox1 to do whatever you want with the selected item.

Hope it helps.

痕至 2024-12-22 15:59:25

您忘记了其他一些...

我最喜欢的 - 制作自定义“Initialize()”函数来设置数据,然后使用 ShowDialog() 正常打开表单。然后您可以让许多表单实现该界面,并动态地显示它们。

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


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

您可以重写 ShowDialog() 函数,但是现在只剩下三个重写,这可能是可接受的,也可能是不可接受的。如果需要,还可以使用 owner 属性覆盖该属性。

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

您可以隐藏旧的 ShowDialog() 以防止人们调用它。这可以通过简单地将类型转换为 Form 来转义,因此这并不是真正的解决方案。

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

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

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(). 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. Also override the one with the owner property if you need it.

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

You can hide the old ShowDialog() to prevent people from calling it. This can be escaped by simply casting the type as Form, so it's not really a solution.

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

(new CustomerForm()).ShowDialog();  // exception!
(new CustomerForm() as Form).ShowDialog()  // works fine
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文