Search code examples
c#if-statementcomboboxselectedindex

if (ComboBox1) statement error


I created a ComboBox where a user needs to choose a language.

private void ComboBox1_SelectedIndex(object sender, EventArgs e)
{
    if (ComboBox1.SelectedIndex.ToString == "English")
    {
        this.Frame.Navigate(typeof(MainPage));
    }

I'm not sure what I am writing wrong. Should I convert the option of English to a string or can I select is as an item?


Solution

  • selecteditem is the value selected

    selectedindex is the index selected

    you have also to check weather your combobox is selecting an item else an exception'll be thrown

    private void ComboBox1_SelectedIndex(object sender, EventArgs e)
        {
            if (ComboBox1.SelectedIndex!=-1 && ComboBox1.SelectedItem.ToString() == "English")
            {
                this.Frame.Navigate(typeof(MainPage));
            }