【问题标题】:How to show all values from a class in a textbox from a combobox如何在组合框的文本框中显示类中的所有值
【发布时间】:2017-09-19 17:57:22
【问题描述】:

我目前正在创建一个 winform 程序,帐户持有人可以在其中购买特定的产品/服务。

我目前遇到的问题是,当我在组合框中选择帐户持有人姓名时,所有帐户持有人的详细信息都应该显示在多行文本框中,但是到目前为止我得到的只是帐户持有人的姓名。

这里是相关代码...

 public Form1()
    {
        InitializeComponent();

        mAccHolder[0] = new Customer("Rich", "Bronze Account", 11);
        mAccHolder[1] = new Customer("Patrick", "Silver Account", 21);
        mAccHolder[2] = new Customer("Steve", "Gold Account", 12);
        mAccHolder[3] = new Customer("Kevin", "Platinum Account", 25);

        foreach(Customer r in mAccHolder)
        {
            comboBox1.Items.Add(r.GetName());
        }
    }

以及将组合框和文本框连接在一起的代码...

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (comboBox1.SelectedIndex == -1)
            {
                cstTxtBox.Text = string.Empty;
            }
            else
            {
                cstTxtBox.Text = comboBox1.SelectedItem.ToString();
            }

【问题讨论】:

  • 查找string concatenation
  • 您需要发布 GetName()。看起来您只是在 combobox1 中添加帐户名称。

标签: c# winforms class combobox textbox


【解决方案1】:

你可以使用foreach,

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
     if (comboBox1.SelectedIndex == -1)
     {
         cstTxtBox.Text = string.Empty;
     }
     else
     {
         foreach (var item in comboBox1.Items)
         {
             cstTxtBox.Text = cstTxtBox.Text + item.ToString();
         }                    
     }
}

【讨论】:

  • 我收到了“item”和“combobox”的错误。它说 foreach 语句不能与组合框一起使用
【解决方案2】:

使用带有“客户”对象数组的示例,当组合框的选定值更改时,您需要从数组mAccHolder 中获取选定的“客户”对象。一旦你有了这个客户,你可以将“客户”值单独输出到文本框,或者你可以为“客户”创建一个“ToString”方法来将客户信息输出到您的规范中,这就是下面的代码所做的。

以下对您当前代码的更改似乎与描述的一样有效。我在组合框中添加了一个空白值,以便用户可以取消选择任何当前选择的客户。 “GetCustomer”方法简单地遍历数组mAccHolder 以在组合框中获取选定的客户。希望这会有所帮助。

客户类别

class Customer {
  public string Name { get; set; }
  public string AccountType { get; set; }
  public int ID { get; set; }

  public Customer(string inName, string actType, int inID) {
    Name = inName;
    AccountType = actType;
    ID = inID;
  }

  public string GetName() {
    return Name;
  }

  public override string ToString() {
    return "Name: " + Name + " AccountType: " + AccountType + " ID: " + ID;
  }
}

更新代码以使用上面的客户类。

Customer[] mAccHolder = new Customer[10];

public Form1() {
  InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e) {
  mAccHolder[0] = new Customer("Rich", "Bronze Account", 11);
  mAccHolder[1] = new Customer("Patrick", "Silver Account", 21);
  mAccHolder[2] = new Customer("Steve", "Gold Account", 12);
  mAccHolder[3] = new Customer("Kevin", "Platinum Account", 25);
  comboBox1.Items.Add(""); // <- add a blank selection so the user can select NO customer
  foreach (Customer r in mAccHolder) {
    comboBox1.Items.Add(r.GetName());
  }
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {
  if (comboBox1.SelectedIndex == -1) {
    cstTxtBox.Text = string.Empty;
  } else {
    if (comboBox1.SelectedItem.ToString() != "") {
      Customer selectedCustomer = GetCustomer(comboBox1.SelectedItem.ToString());
      if (selectedCustomer != null)
        cstTxtBox.Text = selectedCustomer.ToString();
      else
        cstTxtBox.Text = "Customer not found!";
    }
    else {
      cstTxtBox.Text = "No Customer selected";
    }
  }
}

private Customer GetCustomer(string targetName) {
  foreach (Customer curCustomer in mAccHolder) {
    if (curCustomer.Name.Equals(targetName)) {
      return curCustomer;
    }
  }
  return null;
}

【讨论】:

    【解决方案3】:

    您只会获得帐户持有人的姓名,因为这就是您提供组合框的全部内容。它对Customer 一无所知。它只知道它被提供了一个字符串值,仅此而已。您可以将名称传递给它,这很好,但是您需要打印与该组合框项所代表的内容相对应的信息。您可以通过使用索引来做到这一点。由于我们知道它们是按顺序在组合框中提供的,因此索引将匹配。但是,除非您覆盖 ToString,否则您只会获取对象名称,例如“客户.对象[]”。

    我给大家举了一个例子。

        private Customer[] customers = new Customer[3];
    
        public Form1()
        {
            InitializeComponent();
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
            customers[0] = new Customer("Bob", "Bronze", 22);
            customers[1] = new Customer("Jane", "Silver", 32);
            customers[2] = new Customer("Jordan", "Gold", 26);
    
            foreach(var cust in customers)
            {
                comboBox1.Items.Add(cust.Name);
            }
        }
    
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBox1.Text = customers[comboBox1.SelectedIndex].ToString();
        }
    

    此外,在您的“客户”类中,您可能希望覆盖“ToString()”以使其更好。

        public override string ToString()
        {
            return Name + "\r\n" + Membership + "\r\n" + Age;
        }
    

    【讨论】:

    • 没问题,很高兴为您提供帮助!
    【解决方案4】:

    轻松将客户直接添加到组合框。通过这种方式,您可以随时随地使用它。像这样。

    public Form1()
        {
            InitializeComponent();
    
            mAccHolder[0] = new Customer("Rich", "Bronze Account", 11);
            mAccHolder[1] = new Customer("Patrick", "Silver Account", 21);
            mAccHolder[2] = new Customer("Steve", "Gold Account", 12);
            mAccHolder[3] = new Customer("Kevin", "Platinum Account", 25);
    
            foreach(Customer r in mAccHolder)
            {
                ComboboxItem item = new ComboboxItem();
                item.Text = r.GetName();
                item.Value = r;
    
                comboBox1.Items.Add(item);
    
            }
        }
    

    然后在你想要的地方使用 Customer...

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (comboBox1.SelectedIndex == -1)
                {
                    cstTxtBox.Text = string.Empty;
                }
                else
                {
                    Customer c=(Customer) comboBox1.SelectedValue;
                    cstTxtBox.Text = c.whatyouwant....
                }
    

    【讨论】:

    • Comboboxitem 无法识别!表示当前上下文中不存在
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-18
    • 1970-01-01
    • 1970-01-01
    • 2019-07-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多