我意识到程序启动时我的comboBox总是空的.我必须单击旁边的箭头选择一个值.我们怎么做才能让comboBox在程序启动时显示一个值?
您可以设置以下4个属性:
// Gets or sets the index specifying the currently selected item.
comboBox1.SelectedIndex = someIndex; //int
// Gets or sets currently selected item in the ComboBox.
comboBox1.SelectedItem = someItem; // object
// Gets or sets the text that is selected in the editable portion of a ComboBox.
comboBox1.SelectedText = someItemText; // string
// Gets or sets the value of the member property specified by the ValueMember property.
comboBox1.SelectedValue = someValue; // object
Run Code Online (Sandbox Code Playgroud)
评论直接来自MSDN:http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.aspx
如果您有一个组合框并想要设置它的数据源,您可以这样做:
string[] items = new string[]{"Ram","Shyam"};
comboBox1.DataSource = items;
comboBox1.SelectedIndex = 0;
Run Code Online (Sandbox Code Playgroud)
因此尝试将其设置SelectedIndex为第一个索引。