177

I have some elements in a ComboBox (WinForms with C#). I want their content to be static so that a user cannot change the values inside when the application is ran. I also do not want the user adding new values to the ComboBox

Dan Walker
  • 7,123
  • 6
  • 30
  • 24
Mobin
  • 4,870
  • 15
  • 45
  • 51

4 Answers4

337

Use the ComboStyle property:

comboBox.DropDownStyle = ComboBoxStyle.DropDownList;
Dan Walker
  • 7,123
  • 6
  • 30
  • 24
23

This is another method I use because changing DropDownSyle to DropDownList makes it look 3D and sometimes its just plain ugly.

You can prevent user input by handling the KeyPress event of the ComboBox like this.

private void ComboBox1_KeyPress(object sender, KeyPressEventArgs e)
{
      e.Handled = true;
}
Isuru
  • 30,617
  • 60
  • 187
  • 303
13

Yow can change the DropDownStyle in properties to DropDownList. This will not show the TextBox for filter.

DropDownStyle Property
(Screenshot provided by FUSION CHA0S.)

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Sumit Agrawal
  • 953
  • 7
  • 3
0

I tried ComboBox1_KeyPress but it allows to delete the character & you can also use copy paste command. My DropDownStyle is set to DropDownList but still no use. So I did below step to avoid combobox text editing.

  • Below code handles delete & backspace key. And also disables combination with control key (e.g. ctr+C or ctr+X)

     Private Sub CmbxInType_KeyDown(sender As Object, e As KeyEventArgs) Handles CmbxInType.KeyDown
        If e.KeyCode = Keys.Delete Or e.KeyCode = Keys.Back Then 
            e.SuppressKeyPress = True
        End If
    
        If Not (e.Control AndAlso e.KeyCode = Keys.C) Then
            e.SuppressKeyPress = True
        End If
    End Sub
    
  • In form load use below line to disable right click on combobox control to avoid cut/paste via mouse click.

    CmbxInType.ContextMenu = new ContextMenu()
    
Sushil Jadhav
  • 2,217
  • 2
  • 19
  • 33