首页 > 解决方案 > 将控制器类中的列表添加到表单列表框

问题描述

嗨,我有一个问题,我目前遇到了问题。我试图让 myA 列表显示在我的管理帐户的列表框中,但是当我运行程序时,列表框中没有显示任何内容。

这是我的控制器类,它包含 MakeAccounts() 和 AddAccounts() 的方法

// Methods for handling accounts

    public void MakeAccounts()
    {
        myA.Add(new Account.Everyday());
        myA.Add(new Account.Investment(5, 10));
        myA.Add(new Account.Omni(5, 10, -100));

        myA[0].Balance = 50;
        myA[1].Balance = 500;
        myA[2].Balance = 5000;

      //  WriteBinaryData();
    }

 

这是我的 Manage Accounts From 类,它有一个 DisplayAccounts() 方法,我在 Initialize Component 部分中调用了该方法。

 private void DisplayAccounts()
    {
        listBoxManageAccounts.Items.Clear();

        foreach (Account account in control.myA)                        
        {
            listBoxManageAccounts.Items.Add(account);
        }
        if (listBoxManageAccounts.Items.Count >= 1)
        {
            listBoxManageAccounts.SelectedIndex = 0;
        }
    }

这是我的帐户类:

 public class Account
 {
    static int nextAccountID = 1;
    protected int accountID;
    protected string typeName;
    protected decimal balance;
    protected string lastTransaction;

    public Account()
    {
        accountID = nextAccountID;
        nextAccountID++;
        balance = 0;
        lastTransaction = "There are no transactions.";
    }


    public decimal Balance
    {
        get { return balance; }
        set { balance = value; }
    }

   // public int Interest
   // {
   //     set { Interest = 4; }
   // }


    public class Everyday : Account
    {
        public Everyday()
        {
            typeName = "Everyday";
        }
    }

    public class Investment : Account
    {
        protected decimal interestRate;
        protected decimal failedTransFee;

        public Investment(decimal newInterestRate, decimal newFailedTransFee)
        {
            typeName = "Investment";
            interestRate = newInterestRate;
            failedTransFee = newFailedTransFee;
        }

    }

    public class Omni : Account
    {
        protected decimal interestRate;
        protected decimal failedTransFee;
        protected decimal overdraftLimit;

        public Omni(decimal newInterestRate, decimal newFailedTransFee, decimal newOverdraftLimit)
        {
            typeName = "Omni";
            interestRate = newInterestRate;
            failedTransFee = newFailedTransFee;
            overdraftLimit = newOverdraftLimit;
        }

    }
}

所以我想要发生的是,当打开此表单时,我希望账户(日常、投资、全方位)立即出现在列表框中。目前,当我打开此表单时,没有任何反应。谁能告诉我发生了什么或我哪里出错了?

标签: c#winforms

解决方案


推荐阅读