在Visual C#2010中设置下拉列表选定值

时间:2012-02-24 03:35:13

标签: c# winforms visual-studio-2010 drop-down-menu

我在Visual Studio 2010中有一个Windows窗体项目,我想知道如何设置下拉菜单的默认选定值。

例如,当我运行项目时,下拉菜单是空白的,直到我点击它并选择一个值。我想修改下拉菜单,使其默认具有一个值,然后用户可以更改。如何在设计器视图和运行时通过C#实现这一目标?

1 个答案:

答案 0 :(得分:2)

设置属性SelectedIndex。

<强>运行时:

comboBox1.SelectedIndex = 0; //Selects the first option

如果要选择包含特定文本的选项。

int index = comboBox1.FindString("burger"); //get index
comboBox1.SelectedIndex = index;

<强>设计时间:

选择控件,转到属性窗口并查找SelectedIndex属性,设置所需的索引。

相关问题