1

I have tried the below code to get selected value of combo-box to textbox but it give me the following error

Error : Object reference not set to an instance of an object.

Code

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();
}
Ayman
  • 99
  • 1
  • 3
  • 12
  • If you don't use a `DataSource` for `ComboBox` it returns null for `SelectedValue`. – Reza Aghaei Nov 28 '16 at 12:54
  • As a general solution, you can rely on [`GetItemValue`](http://stackoverflow.com/a/38305363/3110834) extension method. It extracts the value of item based on `ValueMember` this way: `var value = comboBox1.GetItemValue(comboBox1.SelectedItem).ToString();`. `ComboBox` and `ListBox` have a `GetItemText` for getting text but they lack `GetItemValue`. The linked post shared an extension method to resolve this lack. – Reza Aghaei Nov 29 '16 at 22:16

4 Answers4

2

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();
        }
}
Community
  • 1
  • 1
Narek Arzumanyan
  • 616
  • 3
  • 11
  • Can you explain why `dllby.SelectedValue` fail? – Damith Nov 28 '16 at 06:47
  • thanks the code returns { Text = P.Voucher Code, Value = PaymentVoucherCode } i need to get the value should i use sub-string ? – Ayman Nov 28 '16 at 06:47
  • @Ayman - you should avoid using anonymous types (see Jasmin answer). To get the `Value` you need something like this (which is ugly!) with your current code: `dllby.SelectedItem?.GetType().GetProperty("Value")?.GetValue(dllby.SelectedItem, null)` (this needs C#6, by the way) – Fer García Nov 28 '16 at 06:50
0
 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";
Jasmin Solanki
  • 369
  • 7
  • 26
0
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
    }
farrukh aziz
  • 162
  • 1
  • 2
  • 9
0
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;
    }
}
Damith
  • 62,401
  • 13
  • 102
  • 153