EDIT: Yeah, if you use the Drag-N-Drop in visual studio to create your controls, don't redeclare them in your code. Rather, just look at their names in the [Design] pane and call them in your code. Nice catch, Psycho.
To create a combobox in C#, you must declare and initialize the combobox:
CODE
ComboBox myNewComboBox = new ComboBox();
OR you could do this in two separate lines:
CODE
ComboBox myNewComboBox;
.
.
.
.
myNewComboBox = new ComboBox();
You might want to do this in instances where you don't need to initialize the combo box until a certain part of your code is reached. But I think in your case you can just do the first thing.
Also, just as a suggestion, you might want to take a look at the
Switch statement rather than a collection of If statements. It is a bit faster and would probably be better suited for your application:
CODE
(this would go in your button19_Click method)
switch(myNewComboBox.SelectedIndex)
{
case 1:
//Do something here if the index selected is 1
break;
case 2:
//Do something here if the index selected is 2
break;
case 3:
//Do something here if the index selected is 3
break;
default:
//If none of the cases are correct, default to this case.
break;
}
Isn't that cleaner than before?
Or, if you are REALLY hellbent on using if statements, at least use if and then else-if. This prevents the application from checking EVERY case, even though only one can be fulfilled:
CODE
private void button19_Click(object sender, EventArgs e)
{
if (this.combobox2.SelectedIndex = 0)
{
}
else if (this.combobox2.SelectedIndex = 1)
{
}
else if (this.combobox2.SelectedIndex = 2)
{
}
else if (this.combobox2.SelectedIndex = 3)
{
}
}
Hope this helps.
This post has been edited by killnine: 2 Mar, 2008 - 06:25 PM