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

前端 未结 3 797
别那么骄傲
别那么骄傲 2021-02-20 07:49

I\'m writing a Windows Forms app in C#, using Visual Studio 2010.

It has a combo box. I\'ve set the DropDownStyle to \"DropDownList\", and added a few lines to \"Items\

3条回答
  •  独厮守ぢ
    2021-02-20 08:26

    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.

    However this would require the DropDownStyle to be DropDown, and make your ComboBox editable.

    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.

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

提交回复
热议问题