Home All Groups Group Topic Archive Search About

Setting focus to the Form

Author
16 Dec 2006 12:11 PM
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.

Author
16 Dec 2006 12:29 PM
Cor Ligthert [MVP]
Pygmalion,

You mean to activate a form?

http://msdn2.microsoft.com/en-us/library/system.windows.forms.form.activate.aspx

Cor

Show quoteHide quote
"Pygmalion" <ma***@pinteric.com> schreef in bericht
news:1166271099.999383.182730@80g2000cwy.googlegroups.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.
>
Author
16 Dec 2006 1:04 PM
Pygmalion
There is no .Activate method for the Form.  Maybe the problem is I use
VisualStudio 6.0?

Marko

Cor Ligthert [MVP] je napisal:
Show quoteHide quote
> Pygmalion,
>
> You mean to activate a form?
>
> http://msdn2.microsoft.com/en-us/library/system.windows.forms.form.activate.aspx
>
> Cor
>
> "Pygmalion" <ma***@pinteric.com> schreef in bericht
> news:1166271099.999383.182730@80g2000cwy.googlegroups.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.
> >
Author
16 Dec 2006 1:50 PM
MikeD
Show quote Hide quote
"Pygmalion" <ma***@pinteric.com> wrote in message
news:1166271099.999383.182730@80g2000cwy.googlegroups.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
Author
16 Dec 2006 2:37 PM
Pygmalion
MikeD je napisal:
Show quoteHide quote
> "Pygmalion" <ma***@pinteric.com> wrote in message
> news:1166271099.999383.182730@80g2000cwy.googlegroups.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.


Show quoteHide quote
> --
> Mike
> Microsoft MVP Visual Basic
Author
18 Dec 2006 10:36 AM
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:
Show quoteHide quote
> > 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.
> >
Author
18 Dec 2006 4:51 PM
MikeD
"Pygmalion" <ma***@pinteric.com> wrote in message
news:1166438198.608551.31150@n67g2000cwd.googlegroups.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
Author
21 Dec 2006 7:07 AM
Pygmalion
MikeD je napisal:
Show quoteHide quote
> "Pygmalion" <ma***@pinteric.com> wrote in message
> news:1166438198.608551.31150@n67g2000cwd.googlegroups.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