Getting selected value of a combobox

325,330

Solution 1

Try this:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    ComboBox cmb = (ComboBox)sender;
    int selectedIndex = cmb.SelectedIndex;
    int selectedValue = (int)cmb.SelectedValue;

    ComboboxItem selectedCar = (ComboboxItem)cmb.SelectedItem;
    MessageBox.Show(String.Format("Index: [{0}] CarName={1}; Value={2}", selectedIndex, selectedCar.Text, selecteVal));        
}

Solution 2

You are getting NullReferenceExeption because of you are using the cmb.SelectedValue which is null. the comboBox doesn't know what is the value of your custom class ComboboxItem, so either do:

ComboboxItem selectedCar = (ComboboxItem)comboBox2.SelectedItem;
int selecteVal = Convert.ToInt32(selectedCar.Value);

Or better of is use data binding like:

ComboboxItem item1 = new ComboboxItem();
item1.Text = "test";
item1.Value = "123";

ComboboxItem item2 = new ComboboxItem();
item2.Text = "test2";
item2.Value = "456";

List<ComboboxItem> items = new List<ComboboxItem> { item1, item2 };

this.comboBox1.DisplayMember = "Text";
this.comboBox1.ValueMember = "Value";
this.comboBox1.DataSource = items;

Solution 3

I had a similar error, My Class is

public class ServerInfo
{
    public string Text { get; set; }
    public string Value { get; set; }
    public string PortNo { get; set; }

    public override string ToString()
    {
        return Text;
    }
}

But what I did, I casted my class to the SelectedItem property of the ComboBox. So, i'll have all of the class properties of the selected item.

// Code above
ServerInfo emailServer = (ServerInfo)cbServerName.SelectedItem;

mailClient.ServerName = emailServer.Value;
mailClient.ServerPort = emailServer.PortNo;

I hope this helps someone! Cheers!

Share:
325,330

Related videos on Youtube

maxy
Author by

maxy

Updated on July 09, 2022

Comments

  • maxy
    maxy almost 2 years
    public class ComboboxItem { 
                public string Text { get; set; } 
                public string Value { get; set; }
                public override string ToString() { return Text; } 
            }
    
    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                int selectedIndex = comboBox1.SelectedIndex;
                int selecteVal = (int)comboBox1.SelectedValue; 
                ComboboxItem selectedCar = (ComboboxItem)comboBox1.SelectedItem;
                MessageBox.Show(String.Format("Index: [{0}] CarName={1}; Value={2}", selectedIndex, selectedCar.Text, selecteVal));
            }
    

    I'm adding them like:

    ComboboxItem item = new ComboboxItem();
                        item.Text = cd.Name;
                        item.Value = cd.ID;
                        this.comboBox1.Items.Add(item);
    

    I keep getting a NullReferenceExeption and not sure why. the text seems to show up just fine.

  • T30
    T30 over 9 years
    I think that ComboBoxItem class is available only in WPF projects.
  • user3532232
    user3532232 almost 8 years
    Isn't there anyway to use comboBox.SelectedValue for a custom item without using a dataSource? Eg. if you use a datasource you can't remove or add an item to the combobox Items.
  • Adrian W
    Adrian W over 5 years
    OP had a C# question. Your answer looks like C++.
  • AS7K
    AS7K about 5 years
    OP declared it in their post. Surprised there isn't one in the System.Windows.Forms namespace...or did I miss something?