简体   繁体   English

VS2010 / C#:如何在IDE中设置ComboBox的默认值?

[英]VS2010/C#: How do you set the default value of a ComboBox in the IDE?

I'm writing a Windows Forms app in C#, using Visual Studio 2010. 我正在使用Visual Studio 2010在C#中编写Windows窗体应用程序。

It has a combo box. 它有一个组合框。 I've set the DropDownStyle to "DropDownList", and added a few lines to "Items". 我将DropDownStyle设置为“DropDownList”,并在“Items”中添加了几行。

Q: Is there any way for me to set SelectedItemIndex in the "Properties" editor, so that line in the "Items" collection will appear as the default when the combo box appears? 问:我有没有办法在“属性”编辑器中设置SelectedItemIndex,这样当“组合框”出现时,“Items”集合中的行将显示为默认值?

I know I can programmatically set "myComboBox.SelectedItemIndex = NNN" in my Form_Load method, but I'm SURE there's probably some way to do it in the MSVS IDE, too. 我知道我可以编程在我的Form_Load方法设置“myComboBox.SelectedItemIndex = NNN”,但我敢肯定 ,可能有一些方法来做到这一点的MSVS IDE了。

Any ideas? 有任何想法吗?

Thank you in advance! 先感谢您!

I'm not sure if this is what your asking for but if you want a specific item to be set as default IE you load the form and there is already a value selected for you. 我不确定这是否是您要求的,但如果您想将特定项目设置为默认IE,则加载表单并且已经为您选择了一个值。

Simply put this into your public Form1() method. 简单地将它放入您的public Form1()方法中。

comboBox1.SelectedItem = "Test1"; 
//comboBox1 change to the name of 
//your combobox
//Test1 change to the item in your list of items that you want 
//defaulted.

I think that is by far the best way to do it. 我认为这是迄今为止最好的方法。

Not Sure if the exact thing can be accomplished but Visual Studio provides a way of storing the values in its Application Settings, through which you can accomplish 2 things: 不确定如果可以完成确切的事情,但Visual Studio提供了一种在应用程序设置中存储值的方法,通过它您可以完成两件事:

  1. Set a Default value, the first time ever the Form is opened by the User (Note: Applicable only for the first time) 设置默认值,用户第一次打开表单(注意:仅适用于第一次)
  2. The last selection of the user gets saved and the next time the User opens the Form, his last selection gets reflected automatically which is a quite good User experience. 用户的最后一个选择被保存,下次用户打开表单时,他的最后一个选择会自动反映,这是一个非常好的用户体验。

Select the ComboBox and open its Properties Section, Under (Application Settings), select the (Property Binding), once the Application Settings for ComboBox opens, select the Text property and create an Application Setting. 选择ComboBox并打开其属性部分,在(应用程序设置)下,选择(属性绑定),一旦打开ComboBox的应用程序设置,选择Text属性并创建应用程序设置。 This would be the value which is selected by default the first time the user opens the Form, after that whatever Selection is made by the User, would be reflected the next time the Form is opened. 这将是用户首次打开表单时默认选择的值,此后用户进行的任何选择都将在下次打开表单时反映出来。

You could set the Text property of the ComboBox in the Properties window, to one of the values from your collection that you want as the default. 您可以在“ 属性”窗口中将ComboBoxText属性设置为您希望作为默认值的集合中的一个值。

在此输入图像描述

However this would require the DropDownStyle to be DropDown , and make your ComboBox editable. 但是,这需要DropDownStyleDropDown ,并使您的ComboBox可编辑。

If that's more acceptable to you, and you still want to make it un-editable, you can override the KeyPress event for the ComboBox as follows. 如果您更容易接受,并且仍希望使其不可编辑,则可以按如下方式覆盖ComboBoxKeyPress事件。

    private void comboBox_KeyPress(object sender, KeyPressEventArgs e)
    {
        e.Handled = true;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM