Is there any way to define a style/trigger/template that will cause a textbox to lose focus once the Enter key is pressed? Preferably, without using any code...
Thanks!
No, there's nothing baked into WPF to do that. You'd have to add some sort of custom code to look for the Enter key and move the focus manually.
The easiest way would be to use an attached behavior. You could then turn this feature on for all or some of the TextBoxes.
Related
Is there any way to catch the Up/Down arrow keys in a WPF TextBox (System.Windows.Controls.Textbox) and allow them to alter the text? I've read about overiding the ProcessCmdKey method for a Windows Forms TextBox (System.Windows.Forms.TextBox), and it worked fine, but that TextBox is not nearly as flexible as the WPF one. Is there a similar method to accomplish this without having to use the old Windows Forms TextBox?
For my example, I have a TextBox that has a numeric text-mask. I want to be able increase/decrease the numeric value by using the up and down arrow keys.
You could add event handlers to KeyUp and/or KeyDown, if that doesn't get what you need, using PreviewKeyUp and/or PreviewKeyDown should.
Ok so there are many ways to go about fixing my problem. The title describes one.
The problem is that I have made an editor using a smart text box control (ScintillaNET) and I wish to implement an Edit -> Undo button on the menu that also displays its short-cut key in the text. Unfortunately I cannot simply bind it since it will result in the undo event being fired twice (once that the control does on its own, and once by me).
I still need the button to run the undo if it is clicked by the user (as opposed to the short-cut key).
There are 3 ways to fix this that I can see:
Unbind the key from ScintillaNET control (couldn't find out how to do this).
Find a way of identifying if the button is clicked or short-cut-keyed.
Make the appearance of a short-cut key without actually applying one.
Does anyone know how to do one of the above?
I would prefer not to override draw methods and the like.
EDIT:
I was a bit inaccurate about my question. It was actually a ToolStripMenuItem that I needed to check. Sadly it does not have a Focused property.
Found it!
I set the ShortcutKeyDisplayString in the form's constructor without actually setting a shortcut key.
public EditorForm()
{
....
undoToolStripMenuItem.ShortcutKeyDisplayString = "Ctrl+Z";
....
}
Our client requests to stay on the same row after pressing Enter to confirm a cell update. Is this possible? Perhaps instead of going down, it could go one cell to the right instead?
Many thanks,
Override the OnKeyDown method and for everything except ENTER key call the base.OnKeyDown
Not calling the parent class version will ensure that the default behavior of cell changing doesn't happen
after handling the OnKeyFown Event to ByPass the Enter key ... set another Event "OnKeyUp" and try to get Enter key and do whatever you want with that event
I added the event, I click in the label and press any key, but it doest go to the method. How can I capture?
I don't think labels can receive keyboard input. It will go to the control with focus, and labels can never have focus (by default, I guess you might pull some shenanigans though), most likely your events are going to your main form.
Maybe you could disguise a text box to look like a label.
To add content beyond my other comment. Have you looked at something like input bindings for what you are trying to do?
http://msdn.microsoft.com/en-us/library/system.windows.input.inputbinding.aspx
I have a UserControl that consists of three TextBoxes. On a form I can have one or more or my UserControl. I want to implement my own tab behavior so if the user presses Tab in the second TextBox I should only move to the third TextBox if the the second TextBox has anything entered. If nothing is entered in the second TextBox the next control of the form should get focus as per the normal tab behavior. If the user hasn't entered anything in the first or second TextBox and the presses tab there is this special case where a control on the form should be skipped.
By using the ProcessDialogKey I have managed to get it work kind of ok but I still have one problem. My question is if there is a way to detect how a WinForms control got focus since I would also like to know if the my UserControl got focus from a Tab or Shift-Tab and then do my weird stuff but if the user clicks the control I don't want to do anything special.
As a general rule, I would say overriding the standard behavior of the TAB key would be a bad idea. Maybe you can do something like disabling the 3rd text box until a valid entry is made in the 2nd text box.
Now, having said this, I've also broken this rule at the request of the customer. We made the enter key function like the tab key, where the enter key would save the value in a text field, and advance the cursor to the next field.
I don't think there's a built-in way that you could do it. All of the WinForms focus events (GotFocus,LostFocus,Enter,Leave) are called with empty EventArgs parameters, which will not give you any additional information.
Personally, I would disable the third textbox, as Rob Thomas said. If you're determined to do this, though, it wouldn't be difficult to set up a manual (read: hackish) solution. Once the tab key is pressed (if the focus is on the second textbox), set a variable inside your form. If the next object focused is then the third textbox, then you know exactly how it happened.
The reason for this odd tab behavior is all about speed in the input process. It was really good to get some input, I hadn't thought about disabling a textbox but that could actually work. But using the Enter key to accept the input hadn't even crossed my mind. That will work so much better. The user can enter the numbers and then press enter to accept the input and the next possible textbox will be the active one. It's like having the cake and eating it too, The speed factor is there since when using the enter key no unnecessary tabing must be done to get to the correct field and using the enter key next to the numeric keyboard makes it really smooth.
Thanks for the input!
I agree with DannySmurf. Messing with the tab order might give you hell later on if the requirements for the application change.
Another thing that you could do is to implement some kind of wizard for the user to go through.
Better than disabling controls, try monkeying around with TabStop - if this is false, the control will be simply skipped when tabbing.
I'd also suggest that the Changed event of the TextBox is the place to be updating TabStop on the other controls.
I've done something similar to this with a login control, where users could enter either a username or an email address (in separate fields), plus their password, and tabStop is what I used to get the job done.