【问题标题】:Listbox just shows (Collection) every time I add an item to it每次我向其中添加项目时,列表框都会显示(集合)
【发布时间】:2017-05-19 12:17:49
【问题描述】:

所以我有 4 个课程(Account.cs/Dashboard.cs/AddAccount.cs/AccountList.cs)。 Dashboard.cs 是我的主要表单,其中包含列表框和显示方法。 Account.cs 设置帐户详细信息并包含 ToString 方法。 AddAccount.cs 只是将详细信息传递给 Account 类。那么 AccountList.cs 就是创建列表的类。

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

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);
        }

    }

【问题讨论】:

    标签: c# list inheritance listbox


    【解决方案1】:

    在您的 Dashboard 类中,使用 Account 的属性 AccountName 将类实例放入 ListBox 的正确行,如下所示:

    //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);
    }
    

    【讨论】:

    • 谢谢尼诺 :)
    • 这并没有使用你的 ToString() 实现;它只会显示 AccountName。
    • @Idle_Mind OP 说:“我怎样才能让它显示正确的帐户名称?”,而不是 Account.ToString()
    【解决方案2】:

    list_Accounts.Items.Add() 行中有一个简单的typo。去掉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
    }
    

    【讨论】:

    • 谢谢 Idle_Mind
    猜你喜欢
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    • 2015-08-03
    • 1970-01-01
    • 1970-01-01
    • 2020-10-18
    • 1970-01-01
    • 2023-03-10
    相关资源
    最近更新 更多