[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

ComboBox get Value

Posted on 2009-02-10
17
Medium Priority
?
436 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

0
Comment
Question by:Mickeys
  • 9
  • 4
  • 4
17 Comments
 
LVL 15

Expert Comment

by:Praveen Venu
ID: 23598624
you can use

listBox1.SelectedItem.Value
0
 

Author Comment

by:Mickeys
ID: 23598705
???
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
0
 
LVL 15

Expert Comment

by:Praveen Venu
ID: 23598722
try this

comboBox2.SelectedValue

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Mickeys
ID: 23598741
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.
0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 23598797
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

0
 
LVL 15

Expert Comment

by:Praveen Venu
ID: 23598798
this will work for you


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

Author Comment

by:Mickeys
ID: 23598854
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
0
 
LVL 15

Expert Comment

by:Praveen Venu
ID: 23598887
in which line you getting  nullreferenceExceptoion
0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 23598946
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
0
 

Author Comment

by:Mickeys
ID: 23599033
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?
0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 23599063
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
0
 

Author Comment

by:Mickeys
ID: 23599065
praveenvenu: selectedValue = (int)comboBox2.SelectedValue;
0
 

Author Comment

by:Mickeys
ID: 23599081
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);
0
 

Author Comment

by:Mickeys
ID: 23599110
I am able to set this in the start up:

comboBox2.SelectedIndex = 1;

and then it shows the right value.

This is strange.
0
 

Author Comment

by:Mickeys
ID: 23599120
Hmmm looks like I solved it my self. :-)

I did like this

   if (comboBox2.SelectedIndex == 0)
     {
... the code
}
0
 
LVL 26

Accepted Solution

by:
Anurag Thakur earned 2000 total points
ID: 23599184
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");        
}
0
 

Author Closing Comment

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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Loops Section Overview
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month19 days, 16 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question