How to set an item in ComboBox to “SelectedText” property of ComboBox dynamically in Load

后端 未结 3 1226
终归单人心
终归单人心 2020-12-12 05:46

In WinForm, I have a ComboBox. I am trying to do something like this.

When ComboBox has only 1 item, that item should be set as \"Selected Text\" for ComboBox, and w

相关标签:
3条回答
  • 2020-12-12 06:00

    You may use

    if (ComboBox1.Items.Count>0) { ComboBox1.SelectedIndex=0 }
    
    0 讨论(0)
  • 2020-12-12 06:13

    if combobox has only one item, then you can use below code

    comboBox1.SelectedIndex =0;
    

    if combobox has multiple item, and you need to select a particular item... change only the index, index will start with 0, if you need to show second item, then the index will be 1

    comboBox1.SelectedIndex =1;
    
    0 讨论(0)
  • 2020-12-12 06:22

    If I understand it the right way, you want the first item of the combobox to be selected/shown in comboBox.

    This is quite easy:

    comboBox1.SelectedIndex = 0; //This will select the first item in the combobox (zero based numbering)
    

    To set it right after the form is shown simply put it after

    InitializeComponent();
    

    of the appropriate form.

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