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

后端 未结 6 1938
庸人自扰
庸人自扰 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: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;
    

提交回复
热议问题