Click here to Skip to main content
15,906,947 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have used the following code in Microsoft Visual C# 2005 using Microsoft Visual Studio 2005 Professional Edition.
C#
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            _selectedVolt = comboBox1.SelectedIndex;
        } 

private void comboBox1_TextChanged(object sender, EventArgs e)
        {
            volt = Convert.ToDouble(comboBox1.SelectedText);
        }

I have put the values (0.7, 0.8, 0.9) for comboBox in Items field of Property pallette in design-time.

Now please let me know the process of storing the value of the selected text from comboBox in a variable. When I'm doing arithmetic calculation with the value of 'volt' it takes no value in it.
Posted
Updated 17-Mar-15 0:30am
v2
Comments
Andy Lanng 17-Mar-15 6:34am    
I'll try to answer but I'm not too sure what the issue is as the code you have does what you ask

Winforms:

C#
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
            // The index of the selected Item
            var index = comboBox1.SelectedIndex;

            // The Text in a writable combobox.  Is is used for text suggestion etc
            var text = comboBox1.SelectedText;

            // The text or string value as it appears in the selected item
            var value = comboBox1.SelectedItem.Text

            // comboBox1.SelectedItem.ToString() calls comboBox1.SelectedItem.Text
            // This means that you can use the object comboBox1.SelectedItem for string
            // fields such as:
            var dValue = Convert.ToDouble(comboBox1.SelectedItem);
} 
 
Share this answer
 
v4
Comments
CHill60 17-Mar-15 6:40am    
If you put a breakpoint on var value ... and check the value of SelectedText you will find that it is still blank
Andy Lanng 17-Mar-15 6:48am    
Ah - Now I see what I missed. Thanks ^_^
If you put a breakpoint on the line
SQL
volt = Convert.ToDouble(comboBox1.SelectedText);
and hover your mouse over comboBox1.SelectedText you will discover that it is blank. Instead use
volt = Convert.ToDouble(comboBox1.SelectedItem);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900