We help IT Professionals succeed at work.

ComboBox get Value

Mickeys
Mickeys asked
on
Medium Priority
525 Views
Last Modified: 2012-05-06
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

Comment
Watch Question

Praveen VenuTechnical Project Manager

Commented:
you can use

listBox1.SelectedItem.Value

Author

Commented:
???
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
Praveen VenuTechnical Project Manager

Commented:
try this

comboBox2.SelectedValue

Author

Commented:
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.
Anurag ThakurTechnical Manager

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

Praveen VenuTechnical Project Manager

Commented:
this will work for you


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

Author

Commented:
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
Praveen VenuTechnical Project Manager

Commented:
in which line you getting  nullreferenceExceptoion
Anurag ThakurTechnical Manager

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

Author

Commented:
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?
Anurag ThakurTechnical Manager

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

Author

Commented:
praveenvenu: selectedValue = (int)comboBox2.SelectedValue;

Author

Commented:
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);

Author

Commented:
I am able to set this in the start up:

comboBox2.SelectedIndex = 1;

and then it shows the right value.

This is strange.

Author

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

I did like this

   if (comboBox2.SelectedIndex == 0)
     {
... the code
}
Technical Manager
Commented:
you are just setting the text of the combobox values and no value
so when you try to get the value it crashes

try this now
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");        
}

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
It worked just fine with this last code. Thanx for the help. I think I will be back soon for more help. :-)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.