How to use SelectedIndexChanged-Event of ComboBox
38,667
Solution 1
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
Solution 2
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();
}
}
Solution 3
Try this below
if(cboVehicle.SelectedItem.Text == "White Fusion")
{
whiteFusionOil();
}
else
{
silverFusionOil();
}
Author by
Sealer_05
Updated on April 08, 2020Comments
-
Sealer_05 about 4 years
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(); } }
-
Sealer_05 about 12 yearsThat only allows for one change. How can I make it so I can keep changing back and forth? Thanks
-
Eugen Rieck about 12 yearsWhy does this work only for one change? There is nothing in there, that could have this effect. Look at whiteFusionOil() and silverFusionOil() for the culprit
-
Sealer_05 about 12 yearsprivate void whiteFusionOil() { using (DataClasses1DataContext db = new DataClasses1DataContext()) { var car = (from c in db.cars where c.carDesc == "White Fusion" select c.oilChange).FirstOrDefault(); txtOilChange.Text = car.ToString(); } }
-
Sealer_05 about 12 yearsThat is one of the two methods which are identical besides the where clause. As soon as I change the combo box it changes once and then its done
-
Sealer_05 about 12 yearsThat was it. Thank you so much!