Why TextBox is using e.Handled = true in order to prevent specific characters from being typed.
Don't we have a better way?
Isn't this a wrong design?
Currently I want to not receive KeyDown event if specific TextBox is in focus is there a good way to do this?Or I'll have to check is the OriginalSource the TextBox I want...
Edit:
As It seems that my original question is a bit confusing Let me reform it.
Lets suppose we have a Canvas and textbox as a child of canvas.
I registered for keyDown event of canvas to do something very interesting there.
I don't want to receive keydown events when user is typing in textBox though.
Is there a way to do this without involving e.OriginalSource?
If you want to prevent any input you can (in fact, should) also set ReadOnly = true instead of listening to the KeyDown event.
If you want to selectively prevent key strokes, listening to KeyDown and setting e.Handled is the only way.
However, selectively preventing keystrokes is bad for usability; don’t do it. Instead, use the Validating event of the text box control to validate the user input.
Related
Using c# winforms vs2008
I'm using a TabControl on a form. Each tab has a different set of input fields. Some of those fields has to be completed. Such as Username. Multiple users will use the same PC, so a the username must remain static. I have a leave event on the require fields which triggers when the textbox loses focus to check if a value was added. In the case of Username, a popup would then presents possible values. That works pretty awesome to ensure accuracy and speed. To problem comes in when the user clicks on another tab, the textbox leave event triggers. How can I prevent the leave_event code from running if the focus changes to a control not on the current Tab?
I have tried different event handlers, but the leave event seem to occur first. I tried textbox validating event handler, but also no success. I tried adding a If-statement in front of the code to check the tab number, or tabcontrol state - no joy - as before anything else changes, the leave event fires.
The only remaining thing I can think of is to loop through all the controls on the tab to check if they have focus, but that just feels messy.
Any other suggestions?
I have found a partial solution. Using a Mouse_Enter and _Leave event on the tab, I set a flag to determine whether the mouse was clicked in the form or outside. If the flag is set to false, the leave event dont run. This will not solve short cut key presses though. But that I can handle later.
Is there a way to have both a textbox and a button in focus? I know that I can use textbox1.focus to focus the textbox, and same with the button, but is there a way to be able to have enter focus on the button, while still typing in the textbox?
I know that I can just use the key-press event to capture the enter key, but I'm wondering if I can have enter focus on the button.
Just curious to know if this is possible, a simple yes or no is all that's needed. Thanks!
Only one element can have input focus at a time. You can't specify the focus for just one key.
So basically, no, you can't do that.
You don't need to have focus on both controls to get the behavior (I assume) you want. Just set the parent form's or dialog's AcceptButton property to the button you are talking about; its Click event should be triggered when you press the ENTER key even while the TextBox has focus.
https://msdn.microsoft.com/en-us/library/system.windows.forms.form.acceptbutton.aspx
To make the answer complete (edit):
As stated above in my comment to your question (and likewise by other contributors): only one control can have focus at any given time. So, the answer to your original question is:
No, there is no way.
As Bradley said, you can't.
But for your aim, there is a shortcut, called Form.keypreview property, which let's you evaluate keys before they reach the textbox or other controls.
I have a form where there is group of radio controls and some with textbox.
If the cursor is on textbox and i try switching the cursor to a different radio button, i should be able to identify the last active control( in this case.. the textbox) and do some validation.
The LostFocus() event of textbox pops up message indicating that "this item should be filled in..".
But if i want to go with a different radiobutton option in the same group, i dont want this message popping unnecessarily.
How do i avoid that?
The TextBox has Validating and Validated events-- you should use those instead of the LostFocus event. In the case of Validating, you can stop the user from leaving the TextBox if the criteria isn't correct. If you must use "something" like LostFocus, use the Leave event instead.
There is no "Last Active Control" type function. You would have to track that yourself by setting a variable on the Enter event of those controls.
That will lead to an ugly mess to maintain in my opinion. Validate at the form level is probably the best choice for the end user.
I have a Window in WPF with a KeyDown event handler attached.
When the user presses a numeric key (0-9) I respond to the event. I use the 0-9 as shortcut keys to select a radiobutton. Eg. pressing key 3 will select the 3'rd radiobutton.
The problem is that when the user presses a numeric key inside a TextBox I don't want to handle the keypress as a shortcut, because I want to keep the default implementation of the TextBox.
Right now when the user presses 3 when inside a TextBox, the text of the TextBox is set to 3, but also the 3'rd radiobutton is selected.
I thought the TextBox would set e.Handled to true when the user presses the key down inside the TextBox, but that isn't the case.
Also the TextBox is just an example. If the user enters something in other input controls in my hierarchy I don't want to respond to KeyDown events.
I guess I need a better understanding of routed events to solve this, or maybe solve this in another way?
You can check the OriginalSource property of RoutedEventArgs (KeyPressEventArgs in your case). If it is TextBox, don't handle it. If it is not, do your RadioButton selection logic.
Try using the Form.KeyPreview property and also use the ActiveControl property to trigger the logic
I have a textbox with a paragraph of information. There are certain words in the paragraph that i want the user to be able to click on, and when clicked, a different textbox is populated with more information.
I know that you can have the event for the whole textbox, but that isn't want i want. I only want to call that event when certain words within the box are clicked.
Sorry, but I don't think you're going to be able to do it like that. The text in a textbox is a singular value type string, there are no objects for "words" or "letters" which would be necessary to raise such an event.
Your best bet will be to subscribe to the textbox keypressed event and parse the string each time a new letter is added. It may not be beautiful, but it's not a huge overhead.