|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Give Focus to Background FormHow 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 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. > Ive tried changing the flags on SetWindowsPos but that doesnt seem to do If it didn't work as expected, then you are doing something wrong, like > it. 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. > > On Sat, 15 Oct 2005 13:17:02 +0100, "FM" <n***@none.com> wrote: Start a new Project>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. Add Form2 - make it smallish Add Commandbutton Private Sub Command1_Click() Form2.Show vbModeless, Me End Sub 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 On Sat, 15 Oct 2005 14:01:24 +0100, "FM" <n***@none.com> wrote: Form1 losing focus is natural Windows behaviour>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). - 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. 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 > Thanks J. I tried playing with the Form Constants as well but on top (asthat doesnt > seem to do it either. With your sample Form2 loads permanently > 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 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 > 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 > > On Sun, 16 Oct 2005 13:28:54 -0400, "Someone" <nob***@cox.net> wrote: But the SetFocus API (normally called PutFocus) works like a dream>> 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
VB 6, SP 5 and Hyperthread
Organizing Constants for Easier Maintenance insert char in txtbox on key up? Count Numerous Matches Only Once VB6: What's the command that creates an ARRAY out of a STRING??? XP Styles and Web Browser control GDI API question X 2 word hyperlink + command line args Sharing violation when trying to delete a file. VB6: Toolbar button sizeing... |
|||||||||||||||||||||||