I have an application where the user wants F4 to be the "Process Orders" button. (There is a long history around that key performing this feature.)
Today I found out that if the focus is in a ComboBox then F4 makes the ComboBox perform a dropdown.
Is there a way to make that not happen?
Update: I tried this using Delphi and it happens there too. While I am still curious, this seems to be a "baked in" Windows thing. I am going to ask the users to pick another shortcut.
use this
cboTest.PreviewKeyDown += (o,e) => {
if (e.Key == Key.F4)
e.Handled = true;
};
cboTest is your ComboBox Name
The Solution: Use a combobox like this:
public class myComboBox : ComboBox
{
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.Key != Key.F4)
{
base.OnKeyDown(e);
}
}
}
this is all you want.
In addition to the answers above, here is a reusable solution without the need to create a custom control:
public static class ComboBoxHelper
{
public static readonly DependencyProperty DisableF4HotKeyProperty =
DependencyProperty.RegisterAttached("DisableF4HotKey", typeof(bool),
typeof(ComboBoxHelper), new PropertyMetadata(false, OnDisableF4HotKeyChanged));
public static bool GetDisableF4HotKey(DependencyObject obj)
{
return (bool)obj.GetValue(DisableF4HotKeyProperty);
}
public static void SetDisableF4HotKey(DependencyObject obj, bool value)
{
obj.SetValue(DisableF4HotKeyProperty, value);
}
private static void OnDisableF4HotKeyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var box = d as ComboBox;
if (d == null) return;
box.PreviewKeyDown -= OnComboBoxKeyDown;
box.PreviewKeyDown += OnComboBoxKeyDown;
}
private static void OnComboBoxKeyDown(object _, KeyEventArgs e)
{
if (e.Key == System.Windows.Input.Key.F4)
{
e.Handled = true;
}
}
}
In your xaml file, add a namespace reference to the ComboBoxHelper
class and set the attached property on your ComboBox:
<ComboBox h:ComboBoxHelper.DisableF4HotKey="True" />
How are you catching the F4 key? If you use the keypreview, you can override it from bubbling down to the combo box:
private void Form1_Load(object sender, EventArgs e)
{
this.KeyPreview = true;
this.KeyDown += new KeyEventHandler(Form1_KeyDown);
}
void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F4)
{
e.Handled = true;
MessageBox.Show("F4 Pressed");
}
}
By default the F4 key opens or closes the dropdown list of a combo box. This behavior can be altered to ignore the F4 key and open the list using the down arrow key instead. This is done by sending a CB_SETEXTENDEDUI
to the combo box providing a TRUE
parameter. This is assuming that WPF does in fact use the native common controls internally.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.