I am working on a program for school. It is a C# GUI containing two tabs.
On the first tab the user can enter information about a new bank account such as: name, account id, age, and account balance. There is also a button which puts the user's name in a combobox on the second tab. So the second tab contains the combobox and a few textboxes for: name, id, age, and balance.
The problem I am facing is that when I select a name from the combobox, it does not populate all of the text boxes. I have the name textbox figured out because I'm pulling it right from the combobox. But I can't figure out how to populate the other textboxes: id, age, and balance. Here's what I have so far...
class BankAccount
{
//attributes
private string accountID;
private string customerName;
private int customerAge;
private double balance;
private const double DEFAULT_BALANCE = 500.00;
//construct
public BankAccount()
{
}
public BankAccount(string anID, string aName, int anAge, double aBalance)
{
accountID = anID;
customerName = aName;
customerAge = anAge;
balance = aBalance;
}
public BankAccount(string anID, string aName, int anAge)
{
accountID = anID;
customerName = aName;
customerAge = anAge;
balance = DEFAULT_BALANCE;
}
//accessors
public void SetID(string anID)
{
accountID = anID;
}
public void SetName(string aName)
{
customerName = aName;
}
public void SetAge(int anAge)
{
customerAge = anAge;
}
public void SetBalance(double aBalance)
{
balance = aBalance;
}
public string GetID()
{
return accountID;
}
public string GetName()
{
return customerName;
}
public int GetAge()
{
return customerAge;
}
public double GetBalance()
{
return balance;
}
and this is the form
public partial class Form1 : Form
{
ArrayList account = new ArrayList();
public Form1()
{
InitializeComponent();
}
private void btnAddAccount_Click(object sender, EventArgs e)
{
BankAccount aBankAccount = new BankAccount(txtAccountID.Text, txtName.Text,
int.Parse(txtAge.Text), double.Parse(txtBalance.Text));
account.Add(aBankAccount);
AddToComboBox();
ClearText();
}
private void AddToComboBox()
{
cboAccount.Items.Clear();
foreach (BankAccount person in account)
{
cboAccount.Items.Add(person.GetName());
}
}
private void ClearText()
{
txtName.Clear();
txtAccountID.Clear();
txtBalance.Clear();
txtAge.Clear();
txtAccountID.Focus();
}
private void cboAccount_SelectedIndexChanged(object sender, EventArgs e)
{
txtNameTab2.Text = cboAccount.SelectedItem.ToString();
}
Since you stated this is homework, I will try to guide you instead of giving the code.
You need to examine following properties of ComboBox :
You want to have a List<BankAccount>
to store each of the persons
So in your main form do something like this
Private List<BankAccount> account = new List<BankAccount>()
I would also probably change the Methods to get info to properties as they show the information more appropriately.
//construct
public BankAccount()
{
}
public BankAccount(string anID, string aName, int anAge, double aBalance)
{
AccountID = anID;
CustomerName = aName;
CustomerAge = anAge;
if (abalance == 0)
{
Balance = DEFAULT_BALANCE;
}
else {
Balance = aBalance;
}
}
private string _CustomerName;
public string CustomerName
{
get {
retrun _CustomerName;
}
set {
_CustomerName = value;
}
private string _AccountID;
public string AccountID
{
get {
retrun _AccountID;
}
set {
_AccountID= value;
}
private string _CustomerAge;
public string CustomerAge
{
get {
retrun _CustomerAge;
}
set {
_CustomerAge= value;
}
private string _Balance;
public string Balance
{
get {
retrun _Balance;
}
set {
_Balance= value;
}
The reason I would do properties is that that is basically what you are doing with your methods but you have to create additional methods to set and get them where this is built into the property.
In the Main form
private void btnAddAccount_Click(object sender, EventArgs e)
{
BankAccount aBankAccount = new BankAccount(txtAccountID.Text, txtName.Text,
int.Parse(txtAge.Text), double.Parse(txtBalance.Text));
account.Add(aBankAccount);
AddToComboBox();
ClearText();
}
private void cboAccount_SelectedIndexChanged(object sender, EventArgs e)
{
txtNameTab2.Text = account[cboAccount.SelectedIndex].CustomerName;
txtAgeTab2.Text = account[cboAccount.SelectedIndex].CustomerAge;
txtIDTab2.Text = account[cboAccount.SelectedIndex].AccountID;
txtBalanceTab2.Text = account[cboAccount.SelectedIndex].Balance;
}
It's either selected index or selectedIndecies I don't remember off the top of my head right now.
txtNameTab2.Text = account[cboAccount.SelectedIndex].CustomerName;
, and make sure you've added Private List<BankAccount> account = new List<BankAccount>();
to your class - MusiGenesis 2012-04-04 20:55
You can do this very simply. First, you need to override ToString()
in your BankAccount
class, which you can do by adding this method to the class:
public override string ToString() {
return self.CustomerName;
}
Then add the BankAccount
objects as BankAccount
objects (instead of adding their GetName() value as a string):
private void AddToComboBox()
{
cboAccount.Items.Clear();
foreach (BankAccount person in account)
{
//cboAccount.Items.Add(person.GetName());
cboAccount.Items.Add(person);
}
}
Now, cboAccount.SelectedItem
will refer to an object of type BankAccount
, and you can directly access the rest of its properties as needed. A ComboBox uses the ToString()
method of any objects in its Items
collection to determine what text to show for that object in the box.