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?
Loading
Dorababu MekaPosted Jul 25, 2011, 5:20 AM
private void btn_setImage_Click(object sender, EventArgs e)
{
string idButton = cbx_buttonID.Items[cbx_buttonID.SelectedIndex].ToString();
ComboBoxItem cmb = (ComboBoxItem)cbx_image.SelectedItem;
string imagePath = cmb.Value.ToString();
toolBar1.SetButtonImage(idButton, imagePath);
}
Dorababu MekaPosted Jul 25, 2011, 5:30 AM
Mike JonsonPosted Jul 25, 2011, 5:26 AM
Dorababu MekaPosted Jul 25, 2011, 5:22 AM
Mike JonsonPosted Jul 25, 2011, 5:20 AM
Mike JonsonPosted Jul 25, 2011, 5:18 AM
Zoran HorvatPosted Jul 25, 2011, 5:12 AM
Zoran
Mike JonsonPosted Jul 25, 2011, 5:07 AM
Zoran HorvatPosted Jul 25, 2011, 5:06 AM
Mike JonsonPosted Jul 25, 2011, 5:04 AM
Zoran HorvatPosted Jul 25, 2011, 5:00 AM
cbx_image.Items.Add(new ComboBoxItem("Picture 1", imagePath));
cbx_image.Items.Add(new ComboBoxItem("Picture 2", imagePath));
cbx_image.SelectedIndex = 0;
This will select the first item in the list.
Zoran
Mike JonsonPosted Jul 25, 2011, 4:56 AM
Zoran HorvatPosted Jul 25, 2011, 4:51 AM
If you get object reference not set at that specific line, then either cbx_image is null or cbx_image.SelectedValue is null. Can you confirm this by debugging your application?
Zoran
Mike JonsonPosted Jul 25, 2011, 4:48 AM
Mike JonsonPosted Jul 25, 2011, 4:43 AM
Dorababu MekaPosted Jul 25, 2011, 4:38 AM
As per your code you are assigning a path but that did not get any file location as per you defined
myButton.BackgroundImage = System.Drawing.Image.FromFile(imagePath);
Mike JonsonPosted Jul 25, 2011, 4:35 AM
Mike JonsonPosted Jul 25, 2011, 4:28 AM
Dorababu MekaPosted Jul 25, 2011, 4:24 AM
string imagePath = cbx_image.SelectedItem.ToString();
Zoran HorvatPosted Jul 25, 2011, 4:19 AM
Looks like SelectedValue is null, i.e. nothing is selected in the combo box. Try accessing the value like this:
if (cbx_image.SelectedValue != null)
{
string imagePath = cbx_image.SelectedValue.ToString();
toolBar1.SetButtonImage(idButton, imagePath);
}
else
{
toolBar1.SetButtonImage(idButton, null); // supposing that SetButtonImage will not crash when receiving null for the image
}
Zoran
Mike JonsonPosted Jul 25, 2011, 4:07 AM
Please help
Dorababu MekaPosted Jul 25, 2011, 4:01 AM