防止组合框在用户键入时更改所选选项。 C#
我有一个具有焦点的组合框,如果用户键入一个字符,如何防止组合框更改选定的索引。
假设我有一个带有选项“A”和“B”的组合框,选择“A”并且组合框具有焦点,如果用户按“B”,组合框将更改为“B”选项,我该如何防止这种情况发生发生?
我想这样做的原因是因为我正在使用条形码扫描仪,并且下拉列表包含打印机列表,所选打印机打印与条形码扫描仪扫描的内容相关的内容。因此,如果组合框/下拉菜单具有焦点并且条形码扫描仪发送输入,它会更改所选的打印机。
这是我使用的第一个解决方案:我在组合框上使用了按键事件并将已处理标志设置为 true
private void comboBox_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = true;
}
但此解决方案不适用于 Windows 98。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以存储最初选择的值,然后在处理按键时替换它。
我强烈建议您在设置值之前将焦点设置为控件,或者在条形码扫描期间禁用组合框。
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.