TextBox.SelectAll() does not work with TAB - c#

I am using maskedTextBox.SelectAll() to highlight the text in the MaskedTextBox in the Enter and MouseDown events.
It works when I use the mouse, but I go to that textbox by pressing the Tab key, it does not work.
What am I missing here?

Have you tried the GotFocus event?
When you change the focus by using the keyboard (TAB, SHIFT+TAB, and so on), by calling the Select or SelectNextControl methods, or by setting the ContainerControl.ActiveControl property to the current form, focus events occur in the following order:
It then goes on to list the events that are fired. It looks like this fires when the mouse is used so you might only need this handler.

Related

Press key and hold event for button in Winform

I'm trying to make a button change its fore color when we press a key. Like button's name is "A" and we press A it changes the fore color into red, then we release the key it turns back to its default color. But I can't find any such event.
Thank you! :D
It's KeyDown, KeyPress, and KeyUp event you should use.
Refer to this link.
http://csharp.net-informations.com/gui/key-press-cs.htm
If you want to capture a key typed anywhere on a form, you can use Keydown, KeyPress and KeyUp like ydoow said, but you will also have to set the form's KeyPreviewproperty to true to make sure your events are raised even if a key is typed while in a specific control.

Why does the LostFocus event get called at different times?

In reference to this MSDN page (or any related page on the matter), it states that:
When you change the focus by using the keyboard, focus events occur in
the following order:
Enter
GotFocus
Leave
Validating
Validated
LostFocus
However, when you use the mouse to raise events, the order changes!
When you change the focus by using the mouse or by calling the Focus method, focus events occur in the following order:
Enter
GotFocus
LostFocus
Leave
Validating
Validated
Wouldn't this make the chain of events completely different? My interpretation here is that the keyboard chain ensures everything is in working order, then raises the LostFocus event. Yet, the mouse events seem to raise it before validating for some reason. Why is that?
As noted above:
In the MSDN article you linked worded strong enough? Never use LostFocus, only Leave.
The keyboard navigation must be in this order in order to apply the validations. Those are intended to react to them in order to validate any input strings.
The best example I can think of is the e.Cancel aspect in validation. Using the keyboard for navigation is usually a control to control type of navigation (including child and parent controls). Using the mouse for form navigation does not always result in a control being selected. For example closing a form or simply click outside of the control (i.e. re-positioning the form). It is not always desirable to have the validation occur when a mouse click occurs outside a control. Hope that helps.

Silverlight detect wether loosing focus is because tab is pressed or mouse is clicked

Is there a way in silverlight to detect when a lost focus event is triggered if this
happens because the tab key is pressed or the user presses a mouse button on another control.
This is what i want to achieve:
I have a RadGridView with 1 row. In the last column i have a numeric input, when
an user tabs out of this control, a new row should be added to the sourcecollection in the viewmodel, this automaticly adds a new row to the grid, then the first column on this row should have focus and the dropdownlist in the celledittemplate should be opened.
When i use the lost focus event adding the new row works fine, though this also works when i don't use tab to unfocus the control. Also, the first column on the new row is not selected, it somehow
gives focus to row 0 column 0.
Ok, inspired by the answer Dipak gave I came up with a slightly different solution,
I handled the Gridviews keydown and keyup events, keydown sets a bool to true, keyup to false.
the execution sequence fortunately is keydown, lostfocus, keyup
so in the lost focus event I only need to check wether the bool is true;
strangely enough the keyup event is not always triggered, but since the lostfocus is
I set the bool to false there also.
yes you can trace it, Provided you have implemented mouse up/down event on each focusable element on your screen. You will have flag to check if mouse preview event up/down happen on any element, if not then its TAB key which cause lost focus.
This is work around if some one not suggest proper solution.

Handling autocompletion in text boxes. differently from pressing enter

When AutoCompleteCustomSource property of Windows.Forms.TextBox is set, list of variants appears during the entering in textbox.
One can either use a mouse to point and click on one of the variants or choose with up-down buttons and press Enter.
You can also press enter whenever you like.
The gist of a problem is, when the selection with mouse is made, KeyDown (KeyUp, KeyPress) event(s) is(are) raised exactly as if an Enter key was pressed on the keyboard.
How can i determine what really happened and handle selection using mouse curor and press of the Enter key differently?
I solved this issue by tracking mouse and keyboard events of the text box. The resulting functionality was similar to the browser address bar. I can't get to the source code right now but it was basic logic around MouseDown, MouseUp, KeyDown, KeyUp whilue storing their values in variables.

How to stimulate MouseDown event (not click) in c#?

I have designed a GUI calc ,I have used flat buttons for the numbers, to get the button click feel I have changed the mouse down color, but when I use the numpad keys to do calc I am able to stimulate the click event. How do I stimulate MouseDown event?
Take the code that changes the button color from the mousedown event, refactor it into it's own method and call it from the keydown event as well.

Categories