选择从组合框C#中默认项 [英] Selecting default item from Combobox C#
问题描述
我有我的组合框的项目集合几个项目,我想从这个列表中选择一个项目,并将其设置为默认项 - 应用程序启动时 - 已经将该项目的组合框
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.
我想这样的事情:
SelectPrint11.SelectedIndex=2;
但错误是:):
but error is :) :
System.ArgumentOutOfRangeException: InvalidArgument=Value of '2' is not valid for 'SelectedIndex'
编辑:
我现在不能添加评论...问题,连接或东西..但我的名单上有3个项目。 PRINTER1,PRINTER2,Printer3。所有被添加的组合框属性 - >产品 - >系列。
I can't add comment now.. problems with connection or something.. But on my list are 3 items. Printer1, Printer2, Printer3. All are added in Combobox Properties -> Items -> Collection
推荐答案
您可以通过设置的SelectedIndex
comboBox1.SelectedIndex= 1;
或
的SelectedItem
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;
这篇关于选择从组合框C#中默认项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!