如何设置 VB.Net ComboBox 默认值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2637544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:49:28  来源:igfitidea点击:

How do I set a VB.Net ComboBox default value

vb.net

提问by Mr Ed

I can not locate the correct method to make the first item in a combo box visible.

我找不到使组合框中的第一个项目可见的正确方法。

The app starts with an empty combo box. The user makes a radio box selection then clicks Go! (how original). The combo box is loaded via an LDAP query. All this is working just fine. The problem is the combo box still appears to the user to be empty. They must click the arrow to see the options.

该应用程序以一个空的组合框开始。用户进行单选框选择,然后单击 Go!(多么原始)。组合框通过 LDAP 查询加载。所有这些都运行良好。问题是组合框对用户来说仍然是空的。他们必须单击箭头才能看到选项。

How do I make the first option 'visible' after the users clicks Go!?

用户单击 Go! 后,如何使第一个选项“可见”?

回答by Heinzi

 ' Your code filling the combobox '
 ...

 If myComboBox.Items.Count > 0 Then
     myComboBox.SelectedIndex = 0    ' The first item has index 0 '
 End If

回答by Fer

Just go to the combo box properties - DropDownStyle and change it to "DropDownList"

只需转到组合框属性 - DropDownStyle 并将其更改为“DropDownList”

This will make visible the first item.

这将使第一项可见。

回答by Starkternate

OR

或者

you can write this down in your program

你可以在你的程序中写下这个

Private Sub ComboBoxExp_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles MyBase.Load
    AlarmHourSelect.Text = "YOUR DEFAULT VALUE"
    AlarmMinuteSelect.Text = "YOUR DEFAULT VALUE"
End Sub

so when you start your program, the first thing it would do is set it on your assigned default value and later you can easily select your required option from the drop down list. also keeping the DropDownStyle to DropDownList would make it look more cooler.

因此,当您启动程序时,它会做的第一件事就是将其设置为您指定的默认值,然后您可以轻松地从下拉列表中选择所需的选项。还将 DropDownStyle 保留为 DropDownList 会使它看起来更酷。

-Starkternate

-Starkternate

回答by manasi patil

because you have set index is 0 it shows always 1st value from combobox as input.

因为您已将索引设置为 0,所以它始终显示组合框中的第一个值作为输入。

Try this :

尝试这个 :

With Me.ComboBox1
    .DropDownStyle = ComboBoxStyle.DropDown
    .Text = " "
End With

回答by soft001

If ComboBox1.SelectedIndex = -1 Then
    ComboBox1.SelectedIndex = 0    
End If

回答by zahmed80

Much simpler solution, Select the Combo-box, and in the option of Selected item, select the item index (0 for the first item) and set it to be the default value in the combo box.

更简单的解决方案,选择Combo-box,在Selected item选项中,选择item index(0为第一项),设置为combo box中的默认值。

回答by user1388706

Another good method for setting a DropDownList style combobox:

另一个设置 DropDownList 样式组合框的好方法:

Combox1.SelectedIndex = Combox1.FindStringExact("test1")

回答by Taigong Wang

You can try this:

你可以试试这个:

Me.cbo1.Text = Me.Cbo1.Items(0).Tostring