在C#中的comboBox中初始化一个值

man*_*c84 4 c#

我意识到程序启动时我的comboBox总是空的.我必须单击旁边的箭头选择一个值.我们怎么做才能让comboBox在程序启动时显示一个值?

Dav*_*New 6

您可以设置以下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


Emb*_*rja 5

如果您有一个组合框并想要设置它的数据源,您可以这样做:

 string[] items = new string[]{"Ram","Shyam"};
    comboBox1.DataSource = items;
    comboBox1.SelectedIndex = 0;
Run Code Online (Sandbox Code Playgroud)

因此尝试将其设置SelectedIndex为第一个索引。


Agh*_*oub 0

试试这个代码:

comboBox1.Items.Add("Test");
Run Code Online (Sandbox Code Playgroud)