Sagar0921 Sagar0921 - 2 months ago 30
C# Question

Disable right-click menu in ComboBox in c#

My ultimate goal is to make Combobox un-editable. Currently when user makes any selection then ComboBox shows selection and highlights it, therefore user can right-click and cut and delete the text that appears. I have added keyDown method, which prevents user from command such as ctrl + c, ctrl + v and delete. But User can still modify highlighted Text using right-click 'cut', 'copy' and 'paste'. How can i prevent user from modifying the current selection?


Ultimate goal is to make Combobox un-editable.

You could subscribe to KeyDown event and set SuppressKeyPress to true for all other actions other than Ctrl+c and create new ContextMenu to disable all default behavior.

 comboBox2.KeyDown += comboBox2_KeyDown;
 comboBox2.ContextMenu = new ContextMenu();   //diable right click

 void comboBox2_KeyDown(object sender, KeyEventArgs e)
      if (!(e.Control && e.KeyCode == Keys.C))
           e.SuppressKeyPress = true;