Mickeys
asked on
ComboBox get Value
Hi,
How do I get the value that I choose? From the begining the comobox is empty but when I change it I want to get the value I changed to.
How do I get the value that I choose? From the begining the comobox is empty but when I change it I want to get the value I changed to.
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
listBox1.Items.Clear();
string temp;
//int selectedValue = (int)comboBox2.SelectedValue;
string selectedText = comboBox2.SelectedText;
string sSelectedClient = (string)comboBox2.SelectedValue;
for (int i = 0; i < booking.Rows; i++)
{
for (int j = 0; j < booking.Cols; j++)
{
temp = "This is a test"
listBox1.Items.Add(temp);
}
}
} //end typeOfEquimpment_SelectedIndexChanged
ASKER
???
listBox1 is a box with text in. I want the value of the comboBox.
i tried what you said but there are no listbox1.selectedItem.VALU E
listBox1 is a box with text in. I want the value of the comboBox.
i tried what you said but there are no listbox1.selectedItem.VALU
try this
comboBox2.SelectedValue
comboBox2.SelectedValue
ASKER
private void comboBox2_SelectedIndexCha nged(objec t sender, EventArgs e)
{
listBox1.Items.Clear();
string temp;
int selectedValue = (int)comboBox2.SelectedVal ue;
then I get a nullreferenceExceptoion was unhandled.
Is this becuase the combobox was empty from the begining? Or why do I get it? In that case how do I handle this to be correct instead.
{
listBox1.Items.Clear();
string temp;
int selectedValue = (int)comboBox2.SelectedVal
then I get a nullreferenceExceptoion was unhandled.
Is this becuase the combobox was empty from the begining? Or why do I get it? In that case how do I handle this to be correct instead.
try this
does it work
does it work
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
listBox1.Items.Clear();
string temp;
string selectedText = comboBox2.SelectedItem.Text.ToString();
string sSelectedClient = comboBox2.SelectedItem.Value.ToString();
for (int i = 0; i < booking.Rows; i++)
{
for (int j = 0; j < booking.Cols; j++)
{
temp = "This is a test"
listBox1.Items.Add(temp);
}
}
} //end typeOfEquimpment_SelectedIndexChanged
this will work for you
if(comboxBox2.SelectedInde x>=0)
selectedValue = (int)comboBox2.SelectedVal ue;
if(comboxBox2.SelectedInde
selectedValue = (int)comboBox2.SelectedVal
ASKER
Neither of the two last comments work.
praveenvenu: get the nullreferenceExceptoion was unhandled
ragi: I get object does not contain a definition for TEXT and VALUE
Must the combobox2 be initialized in here?
public Form1()
{
InitializeComponent();
lettersRadioButton.Checked = true;
}
or it is ok that it is empty and when I choose what I want it goes into comboBox2_SelectedIndexCha nged
praveenvenu: get the nullreferenceExceptoion was unhandled
ragi: I get object does not contain a definition for TEXT and VALUE
Must the combobox2 be initialized in here?
public Form1()
{
InitializeComponent();
lettersRadioButton.Checked
}
or it is ok that it is empty and when I choose what I want it goes into comboBox2_SelectedIndexCha
in which line you getting nullreferenceExceptoion
try this one
if(comboxBox2 != null && comboxBox2..SelectedIndex != -1)
{
MessageBox.Show("Inside selected value");
string selectedText = comboBox2.SelectedText.ToS tring();
string sSelectedClient = comboBox2.SelectedValue.To String();
}
else
{
MessageBox.Show("Something wrong");
}
if combobox has null value then something is wrong.... and the values are not getting set properly in the combobox
if(comboxBox2 != null && comboxBox2..SelectedIndex != -1)
{
MessageBox.Show("Inside selected value");
string selectedText = comboBox2.SelectedText.ToS
string sSelectedClient = comboBox2.SelectedValue.To
}
else
{
MessageBox.Show("Something
}
if combobox has null value then something is wrong.... and the values are not getting set properly in the combobox
ASKER
ragi: This line gets nullreferenceException string sSelectedClient = comboBox2.SelectedValue.To String();
This must mean that the values are not getting set properly. So where should this be done? When I created my Form I added the combox2 and on this there was an arrow i pressed and edited the vaules I wanted. Isnt that enough?
This must mean that the values are not getting set properly. So where should this be done? When I created my Form I added the combox2 and on this there was an arrow i pressed and edited the vaules I wanted. Isnt that enough?
dose anything showin the combobox when you run the applicaiton?
have a look at this sample link from MSDN show how to bind data to the combobox
http://msdn.microsoft.com/en-us/library/x8ybe6s2(VS.80).aspx
have a look at this sample link from MSDN show how to bind data to the combobox
http://msdn.microsoft.com/en-us/library/x8ybe6s2(VS.80).aspx
ASKER
praveenvenu: selectedValue = (int)comboBox2.SelectedVal ue;
ASKER
Well if I dont try to get whats in the comboBox it runs just fine. I can see all my values in the drop down list and choose them. It is just when I try to get the value it crashes.
I have been into msdn and read that before and it looks like I do it right. Here is the combobox2 code
this.comboBox2.FormattingE nabled = true;
this.comboBox2.Items.AddRa nge(new object[] {
"ShowAllSeats",
"ShowOnlyVaccantSeats",
"ShowOnlyReserved"});
this.comboBox2.Location = new System.Drawing.Point(235, 79);
this.comboBox2.Name = "comboBox2";
this.comboBox2.Size = new System.Drawing.Size(164, 21);
this.comboBox2.TabIndex = 6;
this.comboBox2.SelectedInd exChanged += new System.EventHandler(this.c omboBox2_S electedInd exChanged) ;
I have been into msdn and read that before and it looks like I do it right. Here is the combobox2 code
this.comboBox2.FormattingE
this.comboBox2.Items.AddRa
"ShowAllSeats",
"ShowOnlyVaccantSeats",
"ShowOnlyReserved"});
this.comboBox2.Location = new System.Drawing.Point(235, 79);
this.comboBox2.Name = "comboBox2";
this.comboBox2.Size = new System.Drawing.Size(164, 21);
this.comboBox2.TabIndex = 6;
this.comboBox2.SelectedInd
ASKER
I am able to set this in the start up:
comboBox2.SelectedIndex = 1;
and then it shows the right value.
This is strange.
comboBox2.SelectedIndex = 1;
and then it shows the right value.
This is strange.
ASKER
Hmmm looks like I solved it my self. :-)
I did like this
if (comboBox2.SelectedIndex == 0)
{
... the code
}
I did like this
if (comboBox2.SelectedIndex == 0)
{
... the code
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It worked just fine with this last code. Thanx for the help. I think I will be back soon for more help. :-)
listBox1.SelectedItem.Valu