Setting focus to the Form

  • Thread starter Thread starter Pygmalion
  • Start date Start date
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.
 
There is no .Activate method for the Form. Maybe the problem is I use
VisualStudio 6.0?

Marko

Cor Ligthert [MVP] je napisal:
 
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.
 
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.

 
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:
 
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.
 
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
 
Back
Top