By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,786 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,786 IT Pros & Developers. It's quick & easy.

C# problem: trying to assign a combobox selected value to an integer variable

P: 3
Hi all,
Im pretty new to C#, im trying to assign a combobox selected value to a int variable.

int number;

combobox has items added (1,2,3,4,...9)

when i select a number from the combobox i want to assign this number to my integer variable.

thanks in advance.

cheers,
hhak

added:
at the moment i can do it by

if (comboBox.Items[comboBox.SelectedIndex].ToString() == "1")
{
number = 1;
}
else if (comboBox.Items[comboBox.SelectedIndex].ToString() == "2")
{
number = 2;
}
....but this is a little long
Oct 15 '07 #1
Share this Question
Share on Google+
4 Replies


P: 73
HI,
you can simply assign the value directly to the variable:

number = (int)comboBox.Items[comboBox.SelectedIndex].ToString()







Hi all,
Im pretty new to C#, im trying to assign a combobox selected value to a int variable.

int number;

combobox has items added (1,2,3,4,...9)

when i select a number from the combobox i want to assign this number to my integer variable.

thanks in advance.

cheers,
hhak

added:
at the moment i can do it by

if (comboBox.Items[comboBox.SelectedIndex].ToString() == "1")
{
number = 1;
}
else if (comboBox.Items[comboBox.SelectedIndex].ToString() == "2")
{
number = 2;
}
....but this is a little long
Oct 15 '07 #2

Plater
Expert 5K+
P: 7,872
I would say try more like this if you need to.
Expand|Select|Wrap|Line Numbers
  1. number = int.Parse(comboBox.Items[comboBox.SelectedIndex].ToString()); 
  2.  
Oct 15 '07 #3

P: 3
Thanks alot guys,

this worked for me

"number = int.Parse(comboBox.Items[comboBox.SelectedIndex].ToString());"

lol, my 20 lines is now 1

thanks again...

cheers,
hhak
Oct 16 '07 #4

P: 3
lol...sorry my earlier post got repeated...

cheers,
hhak
Oct 16 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.