470,628 Members | 2,298 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,628 developers. It's quick & easy.

Setting focus to the Form


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.

Dec 16 '06 #1
7 29657
Pygmalion,

You mean to activate a form?

http://msdn2.microsoft.com/en-us/lib....activate.aspx

Cor

"Pygmalion" <ma***@pinteric.comschreef in bericht
news:11**********************@80g2000cwy.googlegro ups.com...
>
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.

Dec 16 '06 #2

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

Marko

Cor Ligthert [MVP] je napisal:
Pygmalion,

You mean to activate a form?

http://msdn2.microsoft.com/en-us/lib....activate.aspx

Cor

"Pygmalion" <ma***@pinteric.comschreef in bericht
news:11**********************@80g2000cwy.googlegro ups.com...

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.
Dec 16 '06 #3

"Pygmalion" <ma***@pinteric.comwrote in message
news:11**********************@80g2000cwy.googlegro ups.com...
>
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.

--
Mike
Microsoft MVP Visual Basic

Dec 16 '06 #4

MikeD je napisal:
"Pygmalion" <ma***@pinteric.comwrote in message
news:11**********************@80g2000cwy.googlegro ups.com...

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.
Thanks, that solved the problem.

Sorry for dotnet newsgroup.

Marko.

--
Mike
Microsoft MVP Visual Basic
Dec 16 '06 #5

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:
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.
Dec 18 '06 #6

"Pygmalion" <ma***@pinteric.comwrote in message
news:11*********************@n67g2000cwd.googlegro ups.com...
>
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.

--
Mike
Microsoft MVP Visual Basic
Dec 18 '06 #7

MikeD je napisal:
"Pygmalion" <ma***@pinteric.comwrote in message
news:11*********************@n67g2000cwd.googlegro ups.com...

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.

--
Mike
Microsoft MVP Visual Basic
Yes, you understood correctly. Assigning 0 to KeyCode parameter
helped.

Thanks, Marko

Dec 21 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Andi B | last post: by
20 posts views Thread by Arne | last post: by
2 posts views Thread by Mystery Man | last post: by
2 posts views Thread by Jesper | last post: by
12 posts views Thread by CLEAR-RCIC | last post: by
5 posts views Thread by Glenn T. Kitchen | last post: by
3 posts views Thread by kelvin.koogan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.