在C#中初始化组合框中的一个值#
答
有,你可以设置以下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
注释行直接从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;
因此,请尝试将SelectedIndex
设置为第一个索引。
答
Davenewza的答案非常适合程序化方法(我强烈推荐)。另一种不太优雅但利用属性工具箱的方法是执行以下操作:
- 从设计视图中,单击相关组合框。
- 导航到外观 - >文本并输入您希望的任何字符串。
为了安全起见,我会输入一个值,该值对应于在框中选择的内容,以防止不需要的字符串传播到其他函数/变量。如果不仔细处理,这个原因会导致很多头痛,这就是为什么程序化方法更受欢迎的原因。
Winform WPF aspnet? – V4Vendetta 2012-07-24 10:43:34