2017-05-19 52 views
0

所以我有4个类(Account.cs/Dashboard.cs/AddAccount.cs/AccountList.cs)。 Dashboard.cs是我的主要形式,它包含列表框和显示方法。 Account.cs设置帐户详细信息并包含ToString方法。 AddAccount.cs只是将细节传递给Account类。然后AccountList.cs是创建列表的类。ListBox只显示(收集)每次我添加一个项目

我在努力做的是显示列表项。当我添加一个项目时,它将它放入列表中,但将其显示为(Collection)。我如何才能显示正确的帐户名称?

Account.cs:

public class Account 
    { 
     //Set the Variable 
     public string AccountName { get; set; } 
     public string AccountNo { get; set; } 
     public string StartingBalance { get; set; } 

     public Account() 
     { 
      AccountName = "Account Name not inserted!"; 
      AccountNo = "Account Number not inserted!"; 
      StartingBalance = "Interest not inserted!"; 
     } 

     public Account(string name, string accNo, string staBal) 
     { 
      AccountName = name; 
      AccountNo = accNo; 
      StartingBalance = staBal; 
      System.Diagnostics.Debug.WriteLine("Account hit"); 
     } 

     public override string ToString() 
     { 
      return String.Format("{0}, {1}, {2}", AccountName, AccountNo, StartingBalance); 
     } 
    } 

    [Serializable] 
    public class SavingsAcc : Account 
    { 

     public SavingsAcc() 
     { 
     } 

     public SavingsAcc(string name, string accNo, string staBal) : base(name, accNo, staBal) 
     { 
     } 

     public override string ToString() 
     { 
      return base.ToString() + String.Format("{0,-17}", " (Savings Account)"); 
     } 
    } 

Dashboard.cs:

//Gives us access to the AccountList methods 
     private AccountList myAccountsList = new AccountList(); 

     //display the List in the list box 
     void DisplayAccounts(List<Account> accounts) 
     { 
      list_Accounts.Items.Clear(); 

      //for each account in the list 
      foreach (Account account in accounts) 
      { 
       //add the account object to the list box 
       list_Accounts.Items.Add(accounts); 
      } 
     } 

     //Add account button 
     private void btn_AddAccountPage_Click(object sender, EventArgs e) 
     { 
      AddAccount AddAccountForm = new AddAccount(); 

      //Display form but only process results if OK is pressed 
      if (AddAccountForm.ShowDialog() == DialogResult.OK) 
      { 
       Account NewAccount = AddAccountForm.GetAccountInformation(); //get new account information 
       System.Diagnostics.Debug.WriteLine("Account Information:" + NewAccount); 

       //add the account to the list 
       myAccountsList.AllAccounts.Add(NewAccount); 

       //Display the accounts 
       DisplayAccounts(myAccountsList.AllAccounts); 
      } 
     } 

AddAccount.cs:

public Account GetAccountInformation() 
     { 
      Account a; 

      if (radio_SavingsAccount.Checked) 
      { 
       a = new SavingsAcc(input_AccountName.Text, input_AccountNo.Text, input_StartBalance.Text); 
      } 
      else 
      { 
       a = new Account(input_AccountName.Text, input_AccountNo.Text, input_StartBalance.Text); 
      } 

      return a; 
     } 

AccountList.cs:

class AccountList 
    { 
     private List<Account> allaccounts; 
     public List<Account> AllAccounts 
     { 
      get { return allaccounts; } 
     } 

     public AccountList() 
     { 
      allaccounts = new List<Account>(); 
     } 

     public void AddCurrent(Account a) 
     { 
      allaccounts.Add(a); 
     } 

    } 

回答

2

在你Dashboard类,正确的路线在那里你把类的实例为ListBoxAccount的财产AccountName,像这样:

//for each account in the list 
foreach (Account account in accounts) 
{ 
    //add the account object to the list box 
    list_Accounts.Items.Add(account.AccountName); 
} 
+0

谢谢:) – TheGarrett

+0

这是不使用'的ToString()'实现,那么NINO;它只会显示AccountName。 –

+0

@Idle_Mind OP说:“我怎么才能让它显示只是帐户名?”,而不是'Account.ToString()' – Nino

2

您已经在您的list_Accounts.Items.Add()行中有一个简单的排字错误。删除accounts中的s,使其为account

变化:

//for each account in the list 
foreach (Account account in accounts) 
{ 
    //add the account object to the list box 
    list_Accounts.Items.Add(accounts); // You're adding the entire List<> each time! 
} 

要:

//for each account in the list 
foreach (Account account in accounts) 
{ 
    //add the account object to the list box 
    list_Accounts.Items.Add(account); // Add just each individual account 
} 
+0

谢谢Idle_Mind – TheGarrett

相关问题