how to check if item is selected from a comboBox in C#

后端 未结 6 1906
庸人自扰
庸人自扰 2021-01-03 22:44

I\'m pretty new here.

I have a form, and want to check if the user filled it in correctly. In the form there\'s a combo box; how can I build the \"if\" statement fo

相关标签:
6条回答
  • 2021-01-03 23:17

    You can try

    if(combo1.Text == "")
    {
    
    }
    
    0 讨论(0)
  • 2021-01-03 23:18
    if (comboBox1.SelectedIndex == -1)
    {
        //Done
    }
    

    It Works,, Try it

    0 讨论(0)
  • 2021-01-03 23:29

    Here is the perfect coding which checks whether the Combo Box Item is Selected or not

    if (string.IsNullOrEmpty(comboBox1.Text))
    {
        MessageBox.Show("No Item is Selected"); 
    }
    else
    {
        MessageBox.Show("Item Selected is:" + comboBox1.Text);
    }
    
    0 讨论(0)
  • 2021-01-03 23:31

    You seem to be using Windows Forms. Look at the SelectedIndex or SelectedItem properties.

    if (this.combo1.SelectedItem == MY_OBJECT)
    {
        // do stuff
    }
    
    0 讨论(0)
  • 2021-01-03 23:34

    Use:

    if(comboBox.SelectedIndex > -1) //somthing was selected
    

    To get the selected item you do:

    Item m = comboBox.Items[comboBox.SelectedIndex];
    

    As Matthew correctly states, to get the selected item you could also do

    Item m = comboBox.SelectedItem;
    
    0 讨论(0)
  • 2021-01-03 23:37
    if (combo1.SelectedIndex > -1)
    {
        // do something
    }
    

    if any item is selected selected index will be greater than -1

    0 讨论(0)
提交回复
热议问题