2017-05-19 7 views
0

私は4つのクラス(Account.cs/Dashboard.cs/AddAccount.cs/AccountList.cs)を持っています。 Dashboard.csはリストボックスとメソッドの表示を含む私のメインフォームです。 Account.csはアカウントの詳細を設定し、ToStringメソッドを含みます。 AddAccount.csは、詳細をAccountクラスに渡します。 AccountList.csはリストを作成するクラスです。リストボックスはアイテムを追加するたびに(コレクション)を表示します

私が苦労しているのは、リスト項目を表示することです。アイテムを追加すると、リストに追加されますが、(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:あなたのDashboardクラス、正しいラインで

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

あなたはこのように、AccountのプロパティAccountNameListBoxにクラスのインスタンスを入れている:あなたがきた

//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()'実装を使用していません。 AccountNameのみが表示されます。 –

+0

@Idle_Mind OP:「Account.ToString()」ではなく「ちょうどアカウント名を表示するにはどうすればいいですか?」 – Nino

2

あなたのlist_Accounts.Items.Add()行にシンプルなのタイプがあります。 saccountsに削除して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

関連する問題