Link to home
Start Free TrialLog in
Avatar of Mickeys
MickeysFlag for Sweden

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.


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

Open in new window

Avatar of Praveen Venu
Praveen Venu
Flag of India image

you can use

listBox1.SelectedItem.Value
Avatar of Mickeys

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.VALUE
try this

comboBox2.SelectedValue

Avatar of Mickeys

ASKER

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            string temp;
            int selectedValue = (int)comboBox2.SelectedValue;
         

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
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

Open in new window

this will work for you


      if(comboxBox2.SelectedIndex>=0)
             selectedValue = (int)comboBox2.SelectedValue;
Avatar of Mickeys

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_SelectedIndexChanged
in which line you getting  nullreferenceExceptoion
try this one
 if(comboxBox2 != null && comboxBox2..SelectedIndex != -1)
{
            MessageBox.Show("Inside selected value");
            string selectedText = comboBox2.SelectedText.ToString();
            string sSelectedClient = comboBox2.SelectedValue.ToString();
}
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
Avatar of Mickeys

ASKER

ragi: This line gets nullreferenceException    string sSelectedClient = comboBox2.SelectedValue.ToString();

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
Avatar of Mickeys

ASKER

praveenvenu: selectedValue = (int)comboBox2.SelectedValue;
Avatar of Mickeys

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.FormattingEnabled = true;
            this.comboBox2.Items.AddRange(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.SelectedIndexChanged += new System.EventHandler(this.comboBox2_SelectedIndexChanged);
Avatar of Mickeys

ASKER

I am able to set this in the start up:

comboBox2.SelectedIndex = 1;

and then it shows the right value.

This is strange.
Avatar of Mickeys

ASKER

Hmmm looks like I solved it my self. :-)

I did like this

   if (comboBox2.SelectedIndex == 0)
     {
... the code
}
ASKER CERTIFIED SOLUTION
Avatar of Anurag Thakur
Anurag Thakur
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mickeys

ASKER

It worked just fine with this last code. Thanx for the help. I think I will be back soon for more help. :-)