从 Combobox C# 中选择默认项目 [英] Selecting default item from Combobox C#
问题描述
我的 ComboBox
项目集合中的项目很少,我想从此列表中选择一个项目并将其设置为默认项目 - 当应用程序启动时 - 该项目已经在 comboBox
.
I have few items on my ComboBox
items collection, and i'd like to select one item from this list and set it as default item - when app starts - this item is already on comboBox
.
我正在尝试这样的事情:
I'm trying something like that:
SelectPrint11.SelectedIndex=2;
但错误是:
System.ArgumentOutOfRangeException: InvalidArgument=Value of '2' is not valid for 'SelectedIndex'
在mylist
中有3个项目,Printer1
、Printer2
、Printer3
.所有都添加在 ComboBox Properties ->项目 ->收藏
In mylist
are 3 items, Printer1
, Printer2
, Printer3
. All are added in ComboBox Properties -> Items -> Collection
推荐答案
可以使用SelectedIndex
comboBox1.SelectedIndex= 1;
或
选定项目
comboBox1.SelectedItem = "your value"; //
如果组合框中的值不可用,后者不会抛出异常
The latter won't throw an exception if the value is not available in the combobox
编辑
如果要选择的值不具体,那么你最好这样做
If the value to be selected is not specific then you would be better off with this
comboBox1.SelectedIndex = comboBox1.Items.Count - 1;
这篇关于从 Combobox C# 中选择默认项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!