C# 如何使用 ComboBox 的 SelectedIndexChanged-Event
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9186979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use SelectedIndexChanged-Event of ComboBox
提问by Sealer_05
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.
我有一个带有两个只读值的 ComboBox:白色融合和银色融合。
如何根据在 ComboBox 中选择每个方法来获得正确的方法运行?这些方法只是从 SQL 表中提取一个 Integer 并将其放入 TextBox。
private void cboOilVehicle_SelectedIndexChanged(object sender, EventArgs e)
{
if (cboVehicle.SelectedIndexChanged == "White Fusion")
{
whiteFusionOil();
}
else
{
silverFusionOil();
}
}
采纳答案by Eugen Rieck
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
控件的名称必须是cboOilVehicle(第 1 行)或cboVehicle(第 3 行),不能同时是。你必须决定哪个是正确的
回答by scartag
Try this below
下面试试这个
if(cboVehicle.SelectedItem.Text == "White Fusion")
{
whiteFusionOil();
}
else
{
silverFusionOil();
}
回答by Adam S
If you are going to be comparing the text directly, use the Textproperty of the combobox:
如果您要直接比较文本,请使用Text组合框的属性:
private void cboOilVehicle_SelectedIndexChanged(object sender, EventArgs e)
{
if (cboVehicle.Text == "White Fusion")
{
whiteFusionOil();
}
else
{
silverFusionOil();
}
}

