C# if(ComboBox1)语句错误
我创建了一个组合框,用户需要在其中选择一种语言C# if(ComboBox1)语句错误,c#,if-statement,combobox,selectedindex,C#,If Statement,Combobox,Selectedindex,我创建了一个组合框,用户需要在其中选择一种语言 private void ComboBox1_SelectedIndex(object sender, EventArgs e) { if (ComboBox1.SelectedIndex.ToString == "English") { this.Frame.Navigate(typeof(MainPage)); } 我不确定我写错了什么。我应该将英语选项转换为字符串还是可以选择is作为一项?您需要ToS
private void ComboBox1_SelectedIndex(object sender, EventArgs e)
{
if (ComboBox1.SelectedIndex.ToString == "English")
{
this.Frame.Navigate(typeof(MainPage));
}
我不确定我写错了什么。我应该将英语选项转换为字符串还是可以选择is作为一项?您需要ToString-->ToString()上的括号才能编译。此外,SelectedIndex将只提供索引号,而不是值。应使用ComboBox1.SelectedItem.ToString() 尝试与此更接近的方法:
private void ComboBox1_SelectedIndex(object sender, EventArgs e)
{
var comboBox = sender as ComboBox;
if(comboBox != null)
{
if (comboBox.SelectedIndex != -1 && comboBox.SelectedItem.ToString() == "English")
{
this.Frame.Navigate(typeof(MainPage));
}
}
您甚至需要ToString-->ToString()上的括号才能编译。此外,SelectedIndex将只提供索引号,而不是值。应使用ComboBox1.SelectedItem.ToString() 尝试与此更接近的方法:
private void ComboBox1_SelectedIndex(object sender, EventArgs e)
{
var comboBox = sender as ComboBox;
if(comboBox != null)
{
if (comboBox.SelectedIndex != -1 && comboBox.SelectedItem.ToString() == "English")
{
this.Frame.Navigate(typeof(MainPage));
}
}
selecteditem是所选的值 selectedindex是所选的索引 您还必须检查combobox是否正在选择某个项目,否则将引发异常
private void ComboBox1_SelectedIndex(object sender, EventArgs e)
{
if (ComboBox1.SelectedIndex!=-1 && ComboBox1.SelectedItem.ToString() == "English")
{
this.Frame.Navigate(typeof(MainPage));
}
selecteditem是所选的值 selectedindex是所选的索引 您还必须检查combobox是否正在选择某个项目,否则将引发异常
private void ComboBox1_SelectedIndex(object sender, EventArgs e)
{
if (ComboBox1.SelectedIndex!=-1 && ComboBox1.SelectedItem.ToString() == "English")
{
this.Frame.Navigate(typeof(MainPage));
}
所选索引应为数值 因此,将代码更改为following
private void ComboBox1_SelectedIndex(object sender, EventArgs e)
{
bool result = comboBox1.SelectedValue.ToString()=="English"; // case-1
// Or use
result = comboBox1.SelectedText.ToString() == = "English"; // Case -2
//or use
result = comboBox1.SelectedText == "English"; //case-3
if(result){
// Do the resut
}
注意:要使用案例1和案例2,必须将组合框与数据源绑定所选索引应为数值 因此,将代码更改为following
private void ComboBox1_SelectedIndex(object sender, EventArgs e)
{
bool result = comboBox1.SelectedValue.ToString()=="English"; // case-1
// Or use
result = comboBox1.SelectedText.ToString() == = "English"; // Case -2
//or use
result = comboBox1.SelectedText == "English"; //case-3
if(result){
// Do the resut
}
注意:要使用案例1和案例2,必须将组合框与数据源绑定
ToString()
是正确的waymen,使用SelectedItem和not SelectedIndexToString()
是正确的waymen,使用SelectedItem和not SelectedIndexAha。非常感谢。如果我想做的是作为一个项目。ComboBox.SelectedIndex==ComboBoxItem这是statemenet吗?如何指定项目。[0]ComboBoxItem不起作用。ComboBox1.Items[i]如果您未使用ItemsSource属性YAHA。非常感谢。如果我想做的是作为一个项目。ComboBox.SelectedIndex==ComboBoxItem这是statemenet吗?如何指定项目。[0]ComboBoxItem不工作。ComboBox1.Items[i]如果您没有使用ItemsSource属性,请更改SelectedIndex.ToString的第二条语句(请更改SelectedIndex.ToString的第二条语句(