C# 在C中的comboBox中初始化一个值#
我意识到当程序启动时,我的组合框总是空的。我必须单击旁边的箭头来选择一个值。如何使组合框在程序启动时显示值?请尝试以下代码:C# 在C中的comboBox中初始化一个值#,c#,C#,我意识到当程序启动时,我的组合框总是空的。我必须单击旁边的箭头来选择一个值。如何使组合框在程序启动时显示值?请尝试以下代码: comboBox1.Items.Add("Test"); 您可以设置以下4个属性: // Gets or sets the index specifying the currently selected item. comboBox1.SelectedIndex = someIndex; //int // Gets or sets currently selecte
comboBox1.Items.Add("Test");
您可以设置以下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的注释行:如果您有一个组合框,并且想要设置它的数据源,您可以这样做:
string[] items = new string[]{"Ram","Shyam"};
comboBox1.DataSource = items;
comboBox1.SelectedIndex = 0;
因此,请尝试将
SelectedIndex
设置为第一个索引。Davenewza的答案非常适合编程方法(我强烈推荐)。另一种不那么优雅但利用属性工具箱的方法是执行以下操作:
为了安全起见,我将输入一个值,该值对应于将在框中选择的内容,以防止不需要的字符串传播到其他函数/变量。如果不小心处理,这个原因可能会导致很多麻烦,这就是为什么首选编程方法的原因 如果将WPF与MVVM和
INotifyPropertiesChanged
接口一起使用,则可以在绑定属性中设置回退值。
SelectedIndex=“{Binding SelectedCountry.MultipleClassCountry,FallbackValue=0}”
设置
SelectedIndex=0
应选择组合框中的第一项 你们能解除对我的禁令吗。。。再给我一次机会。