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.
Related
I'm wondering if there is a way to use an event to check a string in a textBox in a windows form before the user clicks on the OK button?
You can use the KeyDown, KeyPress, or KeyUp event.
A full list of events can be found here: http://msdn.microsoft.com/en-us/library/system.windows.forms.textbox_events.aspx
It's all alphabetical, so scroll down to Key....
A better approach is to use the Validating event of the textbox. This way, your validation routine runs when the user attempts to leave the textbox.
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.
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.
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.
Basic question, but I can't figure it out...I have a label with an accelerator key set (e.g. "&Add") and I want to give focus to a textbox when that shortcut, Alt+A, is pressed. What event does this correspond to? I tried the click event but that didn't do anything...
edit: found my own answer - it sets focus to the control that is next in the tab order. so I guess my revised question is, is there a way to catch this so I can have it select all the text in the textbox?
Handle GotFocus event of the TextBox