Mike Jonson

Mike Jonson

  • NA
  • 239
  • 193.9k

Get value from combobox

Jul 25 2011 3:53 AM
I need get value selected item, but write error in code line string imagePath = cbx_image.SelectedValue.ToString();  :  object reference not set to an instance of an object

I add in my combobox:

cbx_image.Items.Add(new ComboBoxItem("Picture 1", imagePath));
cbx_image.Items.Add(new ComboBoxItem("Picture 2", imagePath));

and here i want get value:

        private void btn_setImage_Click(object sender, EventArgs e)
        {
            string idButton = cbx_buttonID.Items[cbx_buttonID.SelectedIndex].ToString();
            string imagePath = cbx_image.SelectedValue.ToString();

            //change picture
            toolBar1.SetButtonImage(idButton, imagePath);

        }

And my class ComboBoxItem:

public class ComboBoxItem
{
private string _name;

public string Name
{
get { return _name; }
set { _name = value; }
}
private object _value;

public object Value
{
get { return _value; }
set { _value = value; }
}

public ComboBoxItem(string name, string value)
{
_name = name;
_value = value;
}

public ComboBoxItem()
{ }

public override string ToString()
{
return Name;
}

}

Please help! How correct get value?

Answers (22)