In AutoCompleteBox, when drop-down is opened and key is pressed (for example Enter) control's KeyDown event is not arroused, and this makes to press Enter two times. I'm trying to make small dialog box, that doesn't oblige user to press keyboard buttons lots of times, but which closes with result when user has found his variant and presses Enter only one time. How to catch this event?
AXAML:
<AutoCompleteBox
FilterMode="Contains"
KeyDown="InputBox_KeyDown"/>
C#:
private async void InputBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
// Getting form result
Close();
}
}
Related
The combo box which I created is not auto completing words stored in a list after typing in the first letter and quickly pressing "Tab". However if you type in slowly and then press "Tab". It's auto completes. I have figured out that the issue is with the key_down event. When you keep a key pressed then press "Tab" it doesn't auto completes, where as if you release the key and then press "Tab" it auto completes.
If I start typing in the second character and then "Tab" out it auto completes.
Key_up event:
private void cboReasonCd_KeyUp(Object eventSender, KeyEventArgs eventArgs)
{
int KeyCode = (int)eventArgs.KeyCode;
int Shift = (int)eventArgs.KeyData / 0x10000;
CancelFillCbo.ComboReasonCD_KeyUp(KeyCode, Shift);
}
Here CancelFillCbo is the form name and ComboReasonCD method is used to populate the list.
Key_Down event:
private void cboReasonCd_KeyDown(object sender, KeyEventArgs e)
{
mlCurrentPosition = cboReasonCd.SelectionStart;
}
cboReasonCd is the name of combo box.
Have you tried built-in autocomplete? You don't need pressing tab.
However if you want to write your own autocomplete, use textbox and TextChanged event to display matching words, or keydown event detecting if tab has been pressed to show them.
'Enter' key in AutoSuggestBox calls to QuerySubmitted event, like clicking the icon. It is a Windows 10 project.
I need to discriminate the Enter key because I use it to go to the next field. I tried KeyDown event, but it is not called.
How can I do it?
Don't ask me why, but you need to use KeyUp event and everything works fine.
Ex:
private void ContactsBox_KeyUp(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Enter)
{
//some stuff here
}
}
Is there an option to force Enter to be the main key on certain view? What I want to achieve is whenever I click "Enter" key I want to invoke button which has "IsDefault" property set to true, no matter what is focused.
One possibility would be to intercept the RoutedEvent before it gets to the controls that try to react to it:
Connect to the PreviewKeyDown event as far up the visual tree as you want to catch it. React to it there accordingly if the enter key was pressed, e.g. run the same code as in your button, and then set in the eventargs Handled to true.
private void previewKeyDownHandler(object sender, KeyEventArgs e)
{
if(e.Key == Key.Enter || e.Key == Key.Return)
{
e.Handled = true;
//Do your stuff
}
}
The Preview events are part of the RoutedEvent system in WPF. The 2 mechanisms are called Tunneling and Bubbling. Here you can read more on that.
I'm writing an application controlled only by numblock on keyboard and I need to solve the problem with this code:
private void btnSaveCorrection_KeyDown(object sender, KeyEventArgs e)
{
MessageBox.Show("HEY");
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("HEY");
// Save to DB and jump to next control
}
else
{
MessageBox.Show("WTF");
// if e.KeyCode is [+] or [-], jump to next/previous control
}
}
The problem is:
When I press Enter, nothing happens. When I press any other key, MessageBox "WTF" is showed. Why is all code in KeyDown event ignored when the Enter is pressed? How to fix it?
Thanks for every reply.
Well, for me it works if you link the KeyUp event instead of the KeyDown Event.
I think the problem is that the enter key is considered a "click" on the button as its default behavior.
My advice is link the KeyUp event instead of the KeyDown event.
I have a ComboBox control in C# Windows forms project. I have set the DropDownStyle of the control to DropDown and have also configured AutoComplete properties to the control. This is working well.
On this control, I have a requirement when I press Enter after selecting a value, it should fire a button click. For this, I handled the keyDown event and checked if (e.KeyCode==Keys.Enter) which works well.
The problem is the following -
When I start typing the AutoComplete box opens up for the control and if I select one of them from mouse, the KeyDown event is fired and the system thinks that an "Enter" key is pressed.
CODE
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("hello");
}
private void cb_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
button1_Click(sender, e);
}
}
BEHAVIOR
I type B from keyboard and clicked on "B2" and got the alert.
Any ideas on what could be the issue? Happy to share more details if required.