Sealer_05
Sealer_05

Reputation: 5566

How to use SelectedIndexChanged-Event of ComboBox

I have a ComboBox with two read only values: white fusion and silver fusion.
How do I get the correct method to run based on selecting each one in a ComboBox? The methods are just pulling an Integer from a SQL table and put it into a TextBox.

private void cboOilVehicle_SelectedIndexChanged(object sender, EventArgs e)
{
    if (cboVehicle.SelectedIndexChanged == "White Fusion")
    {
        whiteFusionOil();
    }
    else
    {
        silverFusionOil();    
    }
}

Upvotes: 4

Views: 47938

Answers (3)

Eugen Rieck
Eugen Rieck

Reputation: 65274

private void cboOilVehicle_SelectedIndexChanged(object sender, EventArgs e)
{
  if (cboVehicle.SelectedIndex == 0)
  {
    whiteFusionOil();
  }
  else
  {
    silverFusionOil();    
  }
}

Edit:

The name of the control must be cboOilVehicle (Line 1) or cboVehicle (Line 3), it can't be both. You have to decide which is correct

Upvotes: 10

Adam S
Adam S

Reputation: 3125

If you are going to be comparing the text directly, use the Text property of the combobox:

private void cboOilVehicle_SelectedIndexChanged(object sender, EventArgs e)
{
    if (cboVehicle.Text == "White Fusion")
    {
        whiteFusionOil();
    } 
    else
    {
        silverFusionOil();    
    }
}

Upvotes: 4

scartag
scartag

Reputation: 17680

Try this below

if(cboVehicle.SelectedItem.Text == "White Fusion")
{

whiteFusionOil();

}
else 
{
silverFusionOil();

}

Upvotes: 1

Related Questions