I have a combobox which has focus, if the user types a character, how do I prevent the combobox to change the selected index.
Say I have a combobox with options “A” and “B”, “A” is selected and the combobox has focus, if the user presses “B”, the combobox changes to the “B” option, how do I prevent this from happening?
The reason I want to do this is because I’m using a barcode scanner and the dropdown contains a list of printers, the selected printer prints something related to what the barcode scanner scans. So If I the combobox/dropdown has focus and the barcode scanner sends input, it changes the selected printer.
This is the first solution I used: I used the keypress event on the combobox and set the handled flag to true
private void comboBox_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = true;
}
But this solution does not work for Windows 98.
You could store the originally selected value and then replace it when you’re handling the KeyPress.
I would much recommend setting the focus to the control before you set the value, or disabling the combo box for the duration of the barcode scanning.