C# 每次我向列表框添加项目时,它都会显示(集合)
所以我有4个类(Account.cs/Dashboard.cs/AddAccount.cs/AccountList.cs)。Dashboard.cs是我的主要表单,它包含列表框和显示方法。Account.cs设置帐户详细信息并包含ToString方法。AddAccount.cs只是将详细信息传递给Account类。然后AccountList.cs是创建列表的类 我正在努力做的是显示列表项。当我添加一个项目时,它会将其放在列表中,但显示为(集合)。如何让它显示正确的帐户名 Account.cs:C# 每次我向列表框添加项目时,它都会显示(集合),c#,list,inheritance,listbox,C#,List,Inheritance,Listbox,所以我有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)");
}
}
//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);
}
}
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;
}
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);
}
}
Dashboard.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)");
}
}
//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);
}
}
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;
}
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);
}
}
AccountList.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)");
}
}
//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);
}
}
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;
}
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);
}
}
类帐户列表
{
所有账户的私有列表;
公开列出所有帐户
{
获取{return allaccounts;}
}
公共帐户列表()
{
allaccounts=新列表();
}
公共往来账户(账户a)
{
所有账目。添加(a);
}
}
在您的仪表板
类中,使用帐户
的属性帐户名
将类实例放入列表框
的正确行,如下所示:
//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);
}
在您的
仪表板
类中,使用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);
}
您的
列表\u Accounts.Items.Add()
行中有一个简单的输入错误。删除帐户中的s
,使其成为帐户
更改:
//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!
}
您的列表\u Accounts.Items.Add()
行中有一个简单的输入错误。删除帐户中的s
,使其成为帐户
更改:
//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!
}
谢谢你空闲的时间谢谢你空闲的时间谢谢你尼诺:)这不是在使用你的ToString()
实现;它只会显示帐户名。@Idle\u Mind OP说:“我怎样才能让它显示正确的帐户名?”,而不是Account.ToString()
谢谢你,尼诺:)这不是在使用ToString()
实现;它只会显示帐户名。@Idle\u Mind OP说:“如何让它显示正确的帐户名?”,而不是Account.ToString()