vb.net 组合框默认项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30003482/
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
ComboBox default item
提问by wintermute
I'm writing a simple program in VB with WinForms (well, I guess so, as I have never tried anything like that before). My google-driven development attempt was going pretty well until I tried to make a ComboBox control show one of its items by default.
我正在用 WinForms 在 VB 中编写一个简单的程序(好吧,我猜是这样,因为我以前从未尝试过这样的事情)。在我尝试让 ComboBox 控件默认显示其中一个项目之前,我的 google 驱动开发尝试进行得很顺利。
So there is ComboBox1 with two items ("Item A" and "Item B") added through graphical interface (property Itemsin Properties panel). I go to Form1_Load event description in the code window and add the following line:
因此,ComboBox1 通过图形界面(属性面板中的属性项)添加了两个项目(“项目 A”和“项目 B” )。我转到代码窗口中的 Form1_Load 事件描述并添加以下行:
ComboBox1.SelectedItem = 0
That is supposed to make "Item A" the default item preselected when the program starts. But it doesn't work. What am I doing wrong?
这应该使“项目 A”成为程序启动时预选的默认项目。但它不起作用。我究竟做错了什么?
采纳答案by Paul Ishak
That's because you are using 0(an integer) on ComboBox.SelectedItem, but ComboBox.Selected item is not an index to an element, it's an actual object.
那是因为您在 ComboBox.SelectedItem 上使用了 0(一个整数),但 ComboBox.Selected 项不是元素的索引,它是一个实际的对象。
This is how you use ComboBox.SelectedItem:
这是您使用 ComboBox.SelectedItem 的方式:
Option Strict On
Option Explicit On
Option Infer Off
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboBox1.Items.Add("Item A")
ComboBox1.Items.Add("Item B")
ComboBox1.SelectedItem = "Item A"
End Sub
End Class

