Selecting default item from Combobox C#

前端 未结 7 850
灰色年华
灰色年华 2021-01-03 18:25

I have few items on my ComboBox items collection, and i\'d like to select one item from this list and set it as default item - when app starts - this item is al

相关标签:
7条回答
  • 2021-01-03 18:31

    first, go to the form load where your comboBox is located,

    then try this code

    comboBox1.SelectedValue = 0; //shows the 1st item in your collection

    0 讨论(0)
  • 2021-01-03 18:43
    private void comboBox_Loaded(object sender, RoutedEventArgs e)
    {
     Combobox.selectedIndex= your index;
    }
    

    OR if you want to display some value after comparing into combobox

     foreach (var item in comboBox.Items)
                {
                    if (item.ToString().ToLower().Equals("your item in lower"))
                    {
                        comboBox.SelectedValue = item;
                    }
                }
    

    I hope it will help, it works for me.

    0 讨论(0)
  • 2021-01-03 18:45
        ComboBox1.Text = ComboBox1.Items(0).ToString
    

    This code is show you Combobox1 first item in Vb.net

    0 讨论(0)
  • 2021-01-03 18:51

    this is the correct form:

    comboBox1.Text = comboBox1.Items[0].ToString();

    U r welcome

    0 讨论(0)
  • 2021-01-03 18:52

    You can set using SelectedIndex

    comboBox1.SelectedIndex= 1;
    

    OR

    SelectedItem

    comboBox1.SelectedItem = "your value"; // 
    

    The latter won't throw an exception if the value is not available in the combobox

    EDIT

    If the value to be selected is not specific then you would be better off with this

    comboBox1.SelectedIndex = comboBox1.Items.Count - 1;
    
    0 讨论(0)
  • 2021-01-03 18:52

    Remember that collections in C# are zero-based (in other words, the first item in a collection is at position zero). If you have two items in your list, and you want to select the last item, use SelectedIndex = 1.

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