列表框只是在每次向其添加项目时显示(集合)

时间:2017-05-19 12:17:50

标签: c# list inheritance listbox

所以我有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:

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 个答案:

答案 0 :(得分:2)

在您的Dashboard课程中,使用ListBox的属性Account将您的课程实例放入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);
}

答案 1 :(得分:2)

您的list_Accounts.Items.Add()行中有一个简单的拼写错误。移除s中的accounts,使其成为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
}