Setting focus to the Form

P

Pygmalion

Hello experts,

I want to use Form_Keydown event in my program. However, it turns out
that one of the form controls is always in focus, so Form_Keydown event
is never invoked. I tried to set focus to the Form using command
frmName.SetFocus in Form_Paint event, however with no effect.

The only successful way I think of to set focus to Form is to .Enable =
False and .Enable = True all controls in Form_Paint event. This works
fine, but is resources and time consuming.

Is there any simpler way to set focus to the Form?

Thanks, Marko.
 
P

Pygmalion

There is no .Activate method for the Form. Maybe the problem is I use
VisualStudio 6.0?

Marko

Cor Ligthert [MVP] je napisal:
 
M

MikeD

Pygmalion said:
Hello experts,

I want to use Form_Keydown event in my program. However, it turns out
that one of the form controls is always in focus, so Form_Keydown event
is never invoked. I tried to set focus to the Form using command
frmName.SetFocus in Form_Paint event, however with no effect.

The only successful way I think of to set focus to Form is to .Enable =
False and .Enable = True all controls in Form_Paint event. This works
fine, but is resources and time consuming.

Is there any simpler way to set focus to the Form?


The form itself can never have "focus" when there are controls on it that
can have focus. To do what you want, set the form's KeyPreview property to
True. The form will then receive keyboard events (KeyUp, KeyDown, KeyPress)
before any controls on that form. However, there are some exceptions. For
example, if you have a command button whose Default property is True, the
form will not get the Enter key.

You also said (in another message) that you're using Visual Studio 6
(presumably VB6). Why then did you include a dotnet newsgroup? That's why
you got the answer you did from Cor. THAT was the problem, not that you're
using VB6.
 
P

Pygmalion

MikeD je napisal:
The form itself can never have "focus" when there are controls on it that
can have focus. To do what you want, set the form's KeyPreview property to
True. The form will then receive keyboard events (KeyUp, KeyDown, KeyPress)
before any controls on that form. However, there are some exceptions. For
example, if you have a command button whose Default property is True, the
form will not get the Enter key.

You also said (in another message) that you're using Visual Studio 6
(presumably VB6). Why then did you include a dotnet newsgroup? That's why
you got the answer you did from Cor. THAT was the problem, not that you're
using VB6.
Thanks, that solved the problem.

Sorry for dotnet newsgroup.

Marko.

 
P

Pygmalion

Another question. Is it possible to prevent control to obtain keyboard
event?
E.g., F8 is defined only for the form. If KeyDown catches that F8 is
pressed, it does stuff and disable others to get that key event.

Thanks, Marko.

Pygmalion je napisal:
 
M

MikeD

Pygmalion said:
Another question. Is it possible to prevent control to obtain keyboard
event?
E.g., F8 is defined only for the form. If KeyDown catches that F8 is
pressed, it does stuff and disable others to get that key event.

If I understand correctly....assign 0 to the KeyCode parameter.
 
P

Pygmalion

MikeD je napisal:
If I understand correctly....assign 0 to the KeyCode parameter.

Yes, you understood correctly. Assigning 0 to KeyCode parameter
helped.

Thanks, Marko
 

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