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?
There’s actually 5 ways to pass the data.
1- (Not recommended if there’s too many parameters) Passing data through the constructor.
2- Using public fields of target class. (NOT RECOMMENDED AT ALL)
3- Using properties.
4- Using tags.
5- Using delegates. (This one is a little bit tricky).
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.
Once you passed the data between forms, using it for population is not hard.
You can use event handler for combobox1 to do whatever you want with the selected item.
Hope it helps.