Darren Young
Darren Young

Reputation: 11090

c# combo box winform

I have a combobox with the text 'select'. I want to set it so that the user cannot type over that. Currently they are able to. I cannot see any read only option for this though.

Can any body advise.

Thanks.

Upvotes: 1

Views: 951

Answers (5)

Developer
Developer

Reputation: 8636

This works

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

Upvotes: 0

Shuhel Ahmed
Shuhel Ahmed

Reputation: 963

Use DropDownStyle = DropDownList. Hope that helps.

Upvotes: 1

Vijay Sirigiri
Vijay Sirigiri

Reputation: 4711

If you want it for all items then

set the ComboBox's DropDownStyle property to DropDownList.

If you want it for the 'Select' item alone then handle KeyDown of ComboBox PS: I've --Select-- as the first item in ComboBox

 private void comboBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (comboBox1.SelectedIndex == 0)
            {

                e.SuppressKeyPress = true;
            }
        }

Upvotes: 1

Catch22
Catch22

Reputation: 3351

Try setting DropDownStyle = ComboBoxStyle.DropDownList

Upvotes: 1

Blorgbeard
Blorgbeard

Reputation: 103437

Set its DropDownStyle property to ComboBoxStyle.DropDownList.

Reference: http://msdn.microsoft.com/en-us/library/system.windows.forms.comboboxstyle.aspx

Upvotes: 5

Related Questions