tab key, mouse click, or select method

F

Fred

In the enter event of a combo box, how to know how this control got the
focus :
- by clicking on it with the mouse
- with the tab key
- with the Select method of the control class
I will want to do an action only if the combo box received focus after a
tab key press.

Thanks
Fred
(France -> imperfect english)
 
K

Kevin Spencer

Set the Form's KeyPreview property to true, and override the OnKeyDown
method. Create a Form-level boolean variable and if it is a Tab Key, set the
variable to true. Otherwise set the variable to false. Also handle the
MouseDown event for the combo box, and set the variable to false there. Then
the Focus event handler for the combo box can check the variable to find out
if the Tab key was just pressed, or if it was entered via a Mouse Click or
some other Key event.

--
HTH,

Kevin Spencer
Microsoft MVP
Ministry of Software Development
http://unclechutney.blogspot.com

I just flew in from Chicago with
a man with a wooden leg named Smith
who shot an elephant in my pajamas.
So I bit him.
 
J

Jani Järvinen [MVP]

Hi Fred,
I will want to do an action only if the combo box received focus after a
tab key press.

You can use the solution that Kevin has given. However, I started to think
your application and the requirement you have, as to me it fights against
the normal Windows user interface conventions.

What kind of activity would you want to do when the user presses the Tab key
to move to another control instead of using for example the mouse? How about
the Alt hotkeys that you can assign to a label control?

--
Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
(e-mail address removed)
http://www.saunalahti.fi/janij/
 
F

Fred

Hi,

First : thanks for responses.
I have a form with a lot of input fields (text and combo boxes). I want
that filling these fields be easy and quick for user. So if it navigates
through controls with the tab key, when he arrives on a combo box, i
want to automatically open the combo box. For this I created a handler
for the Enter event, as it :
private void cb_Enter(object sender, EventArgs e)
{
ComboBox cb = (ComboBox)sender;
if (cb.SelectionIndex == -1) cb.DroppedDown = true;
}
This works fine when the user uses the tab key; but when he clicks with
the mouse, then the combo box is opened and closed immediatly : I will
want to prevent this.

Fred
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top