private void idTextEdit_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Return)
{
e.Handled = true;
SearchButtonClick(sender, EventArgs.Empty);
}
}
I have a text box where this code check fires for every single keypress except for the Enter/Return key which for some reason does nothing. The cursor that is active on the textbox disappears, so I'm thinking it changes focus before the keydown event can fire but I'm not sure. How can I get the return key to stop deselecting the box and register as a keypress. Also there is no other code that would set the enter key to have a different functionality and it's a pretty simple one text box screen for testing.
If you have an AcceptButton set then you'll get the behaviour you are seeing.
I tried your code with my sample form and it worked as expected.
I then set the AcceptButton to one of the buttons and the text box stopped responding to the Enter. Setting AcceptsReturn on the text box had no effect.
Set the AcceptsEnter property to true
Related
I am making a word processor and I want the user to enter the font size in the text box, and when they press enter, if no text is selected, then the whole box should change in font size. And if text is selected, it should change the size of only that text. However, this only works when the key down event is fired without checking what key was pressed. And it only works with a double literal. No variables.
For examplpe, this kind of works:
private void FontSizeBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
(new TextRange(rtb.Selection.Start, rtb.Selection.End)).ApplyPropertyValue(TextElement.FontSizeProperty, Convert.ToDouble(18));
}
But this doesn't:
private void FontSizeBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == System.Windows.Input.Key.Return)
{
(new TextRange(rtb.Selection.Start, rtb.Selection.End)).ApplyPropertyValue(TextElement.FontSizeProperty, Convert.ToDouble(18));
}
}
I have tried making it focus on the main rich text box whose font size is changed, and on the parent window, but it doesn't seem to even fire the e.Key == System.Windows.Input.Key.Return at all, because I tried using a breakpoint to see if it would hit it. And it did not hit. And so, I am very confused.
How do I make it so that if the enter key is pressed, the value of the text box is the font size of the main rich text box? BTW, the reason I used the placeholder 18 is to prevent exceptions while testing. I wanted to see if it would actually change.
Thanks in advance.
RichTextBox itself already handles the ENTER key: RichTextBox.AcceptsReturn defaults to true, which results in inserting a new line on ENTER key pressed.
Either disable this behavior (in case you want to prevent ther user from adding new lines explicitly. Entered text would still wrap) by setting AcceptsReturn to false:
<RichTextBox x:Name="rtb"
AcceptsReturn="False"
KeyDown="FontSizeBox_KeyDown" />
Or handle the tunneling PreviewKeyDown event instead of the bubbling KeyDown:
<RichTextBox x:Name="rtb"
PreviewKeyDown="FontSizeBox_PreviewKeyDown" />
In my Windows application, I made a multiline textbox by setting AcceptsReturn property to True. It lets the user enter multiple lines of text into the textbox. Also, I'd like to do something every time, the Return/Enter key is pressed in the textbox. The event handler code is as follows...
private void TextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return)
// do something here
}
It appears that, if AcceptsReturn is set to True and the Return key is pressed, this event handler is not called at all. Any other key press is detected properly. If AcceptsReturn is not set to True and the Return key is pressed, the event handler is called and Return key press is detected just fine. The problem with this is that pressing Return key doesn't advance the user to the new line in the textbox (as expected).
So, I'd like the Return key press to properly advance the user to the new line in the textbox as well as I'd like to be able to detect that Return key press. Is my approach wrong? Is there a better way to do this?
KeyDown is bubbling event, which means it is first raised on the source control (the TextBox), then on the parent, then on the parent's parent, and so on, until it is handled. When you set AcceptsReturn to true, the control handles the Return key, so the event is not bubbled. In this case you can use the tunneling version of the event: PreviewKeyDown, which is raised on each ancestor of the control from the top to the bottom before it reaches the source control.
See Routing Strategies on MSDN
I have a TextBox and set the MiltiLine property to true and AcceptsTab property to false.
When the TextBox has focus and i press Tab it works fine and the next control get the focus, but when i press Ctrl+Tab it works as if AcceptsTab property is set to true and makes a tab character into the TextBox.
The reason i press Ctrl+Tab.. when switching between forms in my MDI application.
Now how to make a Ctrl+Tab when pressed works like Tab when pressed in a MultiLine TextBox?
Well, if you want to suppress Ctrl+Tab press event in textbox, you may hanlde TextBox.KeyDown event with code like this:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.Tab)
{
e.Handled = true;
}
}
This code will suppress Tab behaviour in TextBox. But I don't know if it keeps child forms switching behaviour. Possibly you will have to implement it programmatically. In my simple MDI application with one MDIContainer form and two child forms showed this behaviour doesn't appear by default.
Normally when pressing the TAB key you change the focus to the next control in the given tab order. I would like to prevent that and have the TAB key do something else. In my case I'd like to change focus from a combobox to a completely different control. I can't do this by setting the tab order. I need to do this programatically. Any idea how? It seems like the KeyDown and KeyPress events can't handle TAB key correctly.
Thanks.
Override ProcessDialogKey or ProcessTabKey on your Form and do the logic you want depending on which control is focused.
Based on JRS's suggestion of using the PreviewKeyDown event, this sends the key press through to the control:
private void textBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Tab)
e.IsInputKey = true;
}
Then you can handle the control's KeyDown event if you want to customise the behaviour:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab)
{
MessageBox.Show("The tab key was pressed while holding these modifier keys: "
+ e.Modifiers.ToString());
}
}
TextBoxBase alternative
If the control is derived from TextBoxBase (i.e. TextBox or RichTextBox), with the Multiline property set to true, then you can simply set the AcceptsTab property to true.
TextBoxBase.AcceptsTab Property
Gets or sets a value indicating whether pressing the TAB key in a multiline text box control types a TAB character in the control instead of moving the focus to the next control in the tab order.
Override the control's LostFocus event see link below for examples:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.lostfocus.aspx
Since I am building a UserControl, I ended up using the PreviewKeyDown event on the control. This avoids having to handle key press events on the host form.
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.previewkeydown.aspx
You can try this code on your KeyDown event:
if (e.KeyCode == Keys.Tab) {
//your logic
e.SuppressKeyPress = true;
}
If the button clicked is Tab, then do any custom logic you want, then call SuppressKeyPress to stop the KeyPress event from firing and invoking the normal Tab logic for you.
I would like to control the focus of my winform application. It is made of a custom listbox and several other component.
I want all the keyboard event be managed by my window handlers in order to avoid specific control key handling (for example when I press a character and the list box is focused, the item starting with the correspondant letter is selected which is not a correct behaviour for my application).
How can I achieve this?
Make sure your form's KeyPreview property is set to true. Then this code should work for canceling your key events to the listbox...
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (this.ActiveControl == listBox1)
e.Handled = true;
}
The KeyPress event may not work for all your scenarios. In that case, I would try out the KeyDown event.