简体   繁体   English

显示来自 combobox 选择的多条信息?

[英]Displaying multiple pieces of information from a combobox selection?

I am working on a program for school.我正在为学校做一个项目。 It is a C# GUI containing two tabs.它是一个包含两个选项卡的 C# GUI。

On the first tab the user can enter information about a new bank account such as: name, account id, age, and account balance.在第一个选项卡上,用户可以输入有关新银行帐户的信息,例如:姓名、帐户 ID、年龄和帐户余额。 There is also a button which puts the user's name in a combobox on the second tab.还有一个按钮,将用户名放在第二个选项卡上的 combobox 中。 So the second tab contains the combobox and a few textboxes for: name, id, age, and balance.因此,第二个选项卡包含 combobox 和一些文本框:姓名、身份证、年龄和余额。

The problem I am facing is that when I select a name from the combobox, it does not populate all of the text boxes.我面临的问题是,当我 select 来自 combobox 的名称时,它不会填充所有文本框。 I have the name textbox figured out because I'm pulling it right from the combobox. But I can't figure out how to populate the other textboxes: id, age, and balance.我想出了名称文本框,因为我是从 combobox 中提取它的。但是我不知道如何填充其他文本框:id、age 和 balance。 Here's what I have so far...这是我到目前为止所拥有的...

  class BankAccount
    {
        //attributes
        private string accountID;
        private string customerName;
        private int customerAge;
        private double balance;
        private const double DEFAULT_BALANCE = 500.00;

        //construct
        public BankAccount()
        {
        }

        public BankAccount(string anID, string aName, int anAge, double aBalance)
        {
            accountID = anID;
            customerName = aName;
            customerAge = anAge;
            balance = aBalance;
        }

        public BankAccount(string anID, string aName, int anAge)
        {
            accountID = anID;
            customerName = aName;
            customerAge = anAge;
            balance = DEFAULT_BALANCE;
        }


        //accessors
        public void SetID(string anID)
        {
            accountID = anID;
        }

        public void SetName(string aName)
        {
            customerName = aName;
        }

        public void SetAge(int anAge)
        {
            customerAge = anAge;
        }

        public void SetBalance(double aBalance)
        {
            balance = aBalance;
        }

        public string GetID()
        {
            return accountID;
        }

        public string GetName()
        {
            return customerName;
        }

        public int GetAge()
        {
            return customerAge;
        }

        public double GetBalance()
        {
            return balance;
        }



and this is the form

   public partial class Form1 : Form
    {
        ArrayList account = new ArrayList();

        public Form1()
        {
            InitializeComponent();
        }



        private void btnAddAccount_Click(object sender, EventArgs e)
        {
            BankAccount aBankAccount = new BankAccount(txtAccountID.Text, txtName.Text,
                int.Parse(txtAge.Text), double.Parse(txtBalance.Text));

            account.Add(aBankAccount);
            AddToComboBox();
            ClearText();


        }

        private void AddToComboBox()
        {
            cboAccount.Items.Clear();
            foreach (BankAccount person in account)
            {
                cboAccount.Items.Add(person.GetName());


            }






        }
        private void ClearText()
        {
            txtName.Clear();
            txtAccountID.Clear();
            txtBalance.Clear();
            txtAge.Clear();
            txtAccountID.Focus();


        }

        private void cboAccount_SelectedIndexChanged(object sender, EventArgs e)
        {

            txtNameTab2.Text = cboAccount.SelectedItem.ToString();



        }

Since you stated this is homework, I will try to guide you instead of giving the code.既然你说这是作业,我会尽量指导你而不是给出代码。

You need to examine following properties of ComboBox :您需要检查ComboBox的以下属性:

  1. ValueMember 值成员
  2. DisplayMember 显示成员
  3. DataSource数据源

You want to have a List<BankAccount> to store each of the persons您想要一个List<BankAccount>来存储每个人

So in your main form do something like this所以在你的主要表格中做这样的事情

Private List<BankAccount> account = new List<BankAccount>()

I would also probably change the Methods to get info to properties as they show the information more appropriately.我也可能会更改方法以获取属性信息,因为它们更恰当地显示信息。

  //construct
public BankAccount()
{
}

public BankAccount(string anID, string aName, int anAge, double aBalance)
{
    AccountID = anID;
    CustomerName = aName;
    CustomerAge = anAge;
    if (abalance == 0)
    {
      Balance = DEFAULT_BALANCE;
    }
    else {
      Balance = aBalance;
    }
}

private string _CustomerName;
public string CustomerName
{
  get {
    retrun _CustomerName;
  }
  set {
   _CustomerName = value;
  }

private string _AccountID;
public string AccountID
{
  get {
    retrun _AccountID;
  }
  set {
   _AccountID= value;
  }

private string _CustomerAge;
public string CustomerAge
{
  get {
    retrun _CustomerAge;
  }
  set {
   _CustomerAge= value;
  }

private string _Balance;
public string Balance
{
  get {
    retrun _Balance;
  }
  set {
   _Balance= value;
  }

The reason I would do properties is that that is basically what you are doing with your methods but you have to create additional methods to set and get them where this is built into the property.我会做属性的原因是,这基本上就是你对你的方法所做的,但你必须创建额外的方法来设置和获取它们,这是内置在属性中的。

In the Main form在主窗体中

private void btnAddAccount_Click(object sender, EventArgs e)
{
    BankAccount aBankAccount = new BankAccount(txtAccountID.Text, txtName.Text,
        int.Parse(txtAge.Text), double.Parse(txtBalance.Text));

    account.Add(aBankAccount);
    AddToComboBox();
    ClearText();
}

private void cboAccount_SelectedIndexChanged(object sender, EventArgs e)
{
    txtNameTab2.Text = account[cboAccount.SelectedIndex].CustomerName;
    txtAgeTab2.Text = account[cboAccount.SelectedIndex].CustomerAge;
    txtIDTab2.Text = account[cboAccount.SelectedIndex].AccountID;
    txtBalanceTab2.Text = account[cboAccount.SelectedIndex].Balance;
}

It's either selected index or selectedIndecies I don't remember off the top of my head right now.它要么是 selected index 要么 selectedIndecies 我现在不记得了。

You can do this very simply.你可以非常简单地做到这一点。 First, you need to override ToString() in your BankAccount class, which you can do by adding this method to the class:首先,您需要覆盖BankAccount class 中的ToString() ,您可以通过将此方法添加到 class 来实现:

public override string ToString() {
    return self.CustomerName;
}

Then add the BankAccount objects as BankAccount objects (instead of adding their GetName() value as a string):然后将BankAccount对象添加为BankAccount对象(而不是将其 GetName() 值添加为字符串):

private void AddToComboBox()
{
    cboAccount.Items.Clear();
    foreach (BankAccount person in account)
    {
        //cboAccount.Items.Add(person.GetName());
        cboAccount.Items.Add(person);
    }
}

Now, cboAccount.SelectedItem will refer to an object of type BankAccount , and you can directly access the rest of its properties as needed.现在, cboAccount.SelectedItem将引用BankAccount类型的 object ,您可以根据需要直接访问其属性的 rest 。 A ComboBox uses the ToString() method of any objects in its Items collection to determine what text to show for that object in the box. ComboBox 使用其Items集合中任何对象的ToString()方法来确定要为框中的 object 显示什么文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM