Home All Groups Group Topic Archive Search About

Give Focus to Background Form

Author
15 Oct 2005 12:17 PM
FM
How do I make a form underlying another form have focus without sending the
uppermost form to the back? When the uppermost form is loaded I want the
underlying form to get the focus with the second form still on top.

FM

Author
15 Oct 2005 12:24 PM
FM
PS

Ive tried changing the flags on SetWindowsPos but that doesnt seem to do it.

FM

Show quoteHide quote
"FM" <n***@none.com> wrote in message
news:u8XfAJY0FHA.2348@TK2MSFTNGP15.phx.gbl...
> How do I make a form underlying another form have focus without sending
the
> uppermost form to the back? When the uppermost form is loaded I want the
> underlying form to get the focus with the second form still on top.
Author
16 Oct 2005 5:13 PM
Someone
> Ive tried changing the flags on SetWindowsPos but that doesnt seem to do
> it.

If it didn't work as expected, then you are doing something wrong, like
missing constants when "Option Explicit" is not in use. From another
newsgroup poster(modified it a little):

Source:
http://groups.google.com/group/microsoft.public.vb.general.discussion/browse_thread/thread/5576852c78bea3f4/6f26e3c4caab306c


Public Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal
hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long,
ByVal cy As Long, ByVal wFlags As Long) As Long

Public Const HWND_TOPMOST = -1
Public Const HWND_NOTOPMOST = -2
Public Const SWP_NOMOVE = &H2
Public Const SWP_NOSIZE = &H1

Sub SetWindowOnTop(ByVal handle As Long, Optional ontop As Boolean = True)

If ontop Then
    Call SetWindowPos(handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or
SWP_NOSIZE)
Else
    Call SetWindowPos(handle, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or
SWP_NOSIZE)
End If

End Sub




Show quoteHide quote
"FM" <n***@none.com> wrote in message
news:ODKd7MY0FHA.268@TK2MSFTNGP09.phx.gbl...
> PS
>
> Ive tried changing the flags on SetWindowsPos but that doesnt seem to do
> it.
>
> FM
>
> "FM" <n***@none.com> wrote in message
> news:u8XfAJY0FHA.2348@TK2MSFTNGP15.phx.gbl...
>> How do I make a form underlying another form have focus without sending
> the
>> uppermost form to the back? When the uppermost form is loaded I want the
>> underlying form to get the focus with the second form still on top.
>
>
Author
15 Oct 2005 12:38 PM
J French
On Sat, 15 Oct 2005 13:17:02 +0100, "FM" <n***@none.com> wrote:

>How do I make a form underlying another form have focus without sending the
>uppermost form to the back? When the uppermost form is loaded I want the
>underlying form to get the focus with the second form still on top.

Start a new Project
Add Form2 - make it smallish
Add Commandbutton


Private Sub Command1_Click()
   Form2.Show vbModeless, Me
End Sub
Author
15 Oct 2005 1:01 PM
FM
Thanks J. I tried playing with the Form Constants as well but that doesnt
seem to do it either. With your sample Form2 loads permanently on top (as
wanted) but Form1 loses focus (not wanted).

FM
Author
15 Oct 2005 1:35 PM
J French
On Sat, 15 Oct 2005 14:01:24 +0100, "FM" <n***@none.com> wrote:

>Thanks J. I tried playing with the Form Constants as well but that doesnt
>seem to do it either. With your sample Form2 loads permanently on top (as
>wanted) but Form1 loses focus (not wanted).

Form1 losing focus is natural Windows behaviour
- I can appreciate that it is annoying, but working around that is
painful

You might be able to get round it with burning off WM_ACTIVATE, but
from memory, the last time I tried that it was not successful.

An alternative method might be to 'fake' Form2 from a Picturebox or
even better a UserControl
- there is a neat API for painting the TitleBar and one can pull
around the control pretending to be a form - but only within the
Parent Form

This rather reminds me of the time I had a passionate desire to have a
MsgBox with a red titlebar
- it took me days of fiddling and looked naff on some screen
resolutions.
Author
15 Oct 2005 1:56 PM
FM
If I dont find a straightforward way to do this I may just 'fake' it using a
picbox like you said. Occasionally Form2 may unavoidably overhang form1
slightly thats why i was trying to use a Form2. I'll get round it somehow.
Thanks.

FM
Author
15 Oct 2005 2:19 PM
Rick Rothstein [MVP - Visual Basic]
> Thanks J. I tried playing with the Form Constants as well but
that doesnt
> seem to do it either. With your sample Form2 loads permanently
on top (as
> wanted) but Form1 loses focus (not wanted).

If I understand you correctly, try this instead...

Private Sub Command1_Click()
   Form2.Show vbModeless, Me
   Me.SetFocus
End Sub

If you have a specific control on Form1 that you want to always
get focus, say a TextBox named Text1, then do this instead...

Private Sub Command1_Click()
   Form2.Show vbModeless, Me
   Text1.SetFocus
End Sub

Rick
Author
15 Oct 2005 3:42 PM
FM
Bless you Rick. Thats my kind of coding :) works perfectly. Thanks.

FM

Show quoteHide quote
> If I understand you correctly, try this instead...
>
> Private Sub Command1_Click()
>    Form2.Show vbModeless, Me
>    Me.SetFocus
> End Sub
>
> If you have a specific control on Form1 that you want to always
> get focus, say a TextBox named Text1, then do this instead...
>
> Private Sub Command1_Click()
>    Form2.Show vbModeless, Me
>    Text1.SetFocus
> End Sub
>
> Rick
Author
16 Oct 2005 5:28 PM
Someone
>   Text1.SetFocus

While on the subject of SetFocus, it's important to note that SetFocus
cannot be used with a disabled control, I think you already know that. If
the control was disabled, you get a run time error. To protect from this,
one could check if the control/form was enabled:

If Text1.Enabled Then
    Text1.SetFocus
End If

Situations when a Form is auto disabled/reenabled implicitly:

- A MsgBox is shown.
- A "FormX.Show vbModal" was used.

When a form is disabled, all controls within it are disabled too. In these
cases a Timer or other event like Winsock's DataArrival event might fire. So
if you use SetFocus in these, make sure that you check for Enabled property.
This obviously depends on the design and what you want to accomplish.


Show quoteHide quote
"Rick Rothstein [MVP - Visual Basic]" <rickNOSPAMnews@NOSPAMcomcast.net>
wrote in message news:ejLS$NZ0FHA.1032@TK2MSFTNGP12.phx.gbl...
>> Thanks J. I tried playing with the Form Constants as well but
> that doesnt
>> seem to do it either. With your sample Form2 loads permanently
> on top (as
>> wanted) but Form1 loses focus (not wanted).
>
> If I understand you correctly, try this instead...
>
> Private Sub Command1_Click()
>   Form2.Show vbModeless, Me
>   Me.SetFocus
> End Sub
>
> If you have a specific control on Form1 that you want to always
> get focus, say a TextBox named Text1, then do this instead...
>
> Private Sub Command1_Click()
>   Form2.Show vbModeless, Me
>   Text1.SetFocus
> End Sub
>
> Rick
>
>
Author
17 Oct 2005 12:12 PM
J French
On Sun, 16 Oct 2005 13:28:54 -0400, "Someone" <nob***@cox.net> wrote:

>>   Text1.SetFocus
>
>While on the subject of SetFocus, it's important to note that SetFocus
>cannot be used with a disabled control, I think you already know that. If
>the control was disabled, you get a run time error. To protect from this,
>one could check if the control/form was enabled:
>
>If Text1.Enabled Then
>    Text1.SetFocus
>End If

But the SetFocus API (normally called PutFocus) works like a dream