【问题标题】:Changing value of textbox according to the selected combobox value根据选定的组合框值更改文本框的值
【发布时间】:2016-11-28 06:36:47
【问题描述】:

我已尝试以下代码将组合框的选定值获取到文本框,但它给了我以下错误

错误:对象引用未设置为对象的实例。

代码

private void frmpaymentsearch_Load(object sender, EventArgs e)
{
    txtcomvalue.Text = "PaymentVoucherCode";
    dllby.DisplayMember = "Text";
    dllby.ValueMember = "Value";
    dllby.Items.Add(new { Text = "P.Voucher Code", Value = "PaymentVoucherCode" });
    dllby.Items.Add(new { Text = "Vendor", Value = "VendorName" });
    dllby.SelectedIndex = 0;
}



private void dllby_SelectedIndexChanged(object sender, EventArgs e)
{
    txtcomvalue.Text = dllby.SelectedValue.ToString();
}

【问题讨论】:

  • 如果您不使用DataSource 来表示ComboBox,它会为SelectedValue 返回null。
  • 作为一个通用的解决方案,你可以依赖GetItemValue扩展方法。它以ValueMember 的方式提取项目的值:var value = comboBox1.GetItemValue(comboBox1.SelectedItem).ToString();。 ComboBox 和 ListBox 有一个 GetItemText 用于获取文本,但它们缺少 GetItemValue。链接的帖子共享了一个扩展方法来解决这个问题。

标签: c# winforms combobox


【解决方案1】:

ComboBox SelectedItem vs SelectedValue

private void dllby_SelectedIndexChanged(object sender, EventArgs e)
{
        Type myType = dllby.SelectedItem.GetType();
        IList<PropertyInfo> props = new List<PropertyInfo>(myType.GetProperties());

        foreach (PropertyInfo prop in props)
        {
            if(prop.Name=="value")
                textBox1.Text = prop.GetValue(dllby.SelectedItem, null).ToString();
        }
}

【讨论】:

  • 你能解释一下为什么dllby.SelectedValue会失败吗?
  • 感谢代码返回 { Text = P.Voucher Code, Value = PaymentVoucherCode } 我需要获取值应该使用子字符串吗?
  • @Ayman - 您应该避免使用匿名类型(请参阅 Jasmin 答案)。要获得Value,您需要使用当前代码:dllby.SelectedItem?.GetType().GetProperty("Value")?.GetValue(dllby.SelectedItem, null)(顺便说一下,这需要 C#6)
【解决方案2】:
 Dictionary comboSource = new Dictionary();
            comboSource.Add("1", "Sunday");
            comboSource.Add("2", "Monday");
            comboSource.Add("3", "Tuesday");
            comboSource.Add("4", "Wednesday");
            comboSource.Add("5", "Thursday");
            comboSource.Add("6", "Friday");
            comboSource.Add("7", "Saturday");
            comboBox1.DataSource = new BindingSource(comboSource, null);
            comboBox1.DisplayMember = "Value";
            comboBox1.ValueMember = "Key";

【讨论】:

    【解决方案3】:
    private void frmpaymentsearch_Load(object sender, EventArgs e)
        {
            txtcomvalue.Text = "PaymentVoucherCode";
            dllby.DisplayMember = "Text";
            dllby.ValueMember = "Value";
            dllby.Items.Add(new { Text = "P.Voucher Code", Value = "PaymentVoucherCode" });
            dllby.Items.Add(new { Text = "Vendor", Value = "VendorName" });
            dllby.SelectedIndexChanged -= dllby_SelectedIndexChanged; // unsubscribe you event 
            dllby.SelectedIndex = 0;
            dllby.SelectedIndexChanged += dllby_SelectedIndexChanged; // subscribe you event 
        }
    
        private void dllby_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtcomvalue.Text = dllby.Text.ToString(); // change selectedvalue to Text
        }
    

    【讨论】:

      【解决方案4】:
      private void frmpaymentsearch_Load(object sender, EventArgs e)
      {
          txtcomvalue.Text = "PaymentVoucherCode";
          dllby.DisplayMember = "Text";
          dllby.ValueMember = "Value";
          dllby.Items.Add(new ComboboxItem(){ Text = "P.Voucher Code", Value = "PaymentVoucherCode" });
          dllby.Items.Add(new ComboboxItem(){ Text = "Vendor", Value = "VendorName" });
      
          dllby.SelectedIndex = 0;
      }
      private void dllby_SelectedIndexChanged(object sender, EventArgs e)
      {
          txtcomvalue.Text = (dllby.SelectedItem as ComboboxItem).Value.ToString();
      }
      
      public class ComboboxItem
      {
          public string Text { get; set; }
          public string Value { get; set; }
      
          public override string ToString()
          {
              return Text;
          }
      }
      

      【讨论】:

      • 找不到类型或命名空间“ComboboxItem”
      猜你喜欢
      • 1970-01-01
      • 2020-12-26
      • 1970-01-01
      • 2022-01-23
      • 1970-01-01
      • 2021-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多