dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Form1 only always on top on Form2, How to do...

  1. #1
    Join Date
    Aug 2001
    Posts
    60

    Talking Form1 only always on top on Form2, How to do...

    Form1 only always on top on Form2, How to do...

    Thank You.

  2. #2
    Join Date
    Feb 2001
    Location
    Stamford CT USA
    Posts
    2,167
    Depending on how you want them to behave. For example, if you want to disable user from interacting with Form2 at all, then you can show Form1 in modal mode from Form1:
    Code:
    ' in FORM2
    Sub ActivateForm1()
      ' modal mode
      call Form1.Show(me, vbModal)
      
      ' note that the lines after the previous Show will not be executed
      ' until FORM1 is hidden or unloaded
      MsgBox "Back in Form2"
    End Sub
    If you still want FORM2 to execute as normal after showing FORM1, then you can work out a logic that whenever user activates FORM2, FORM1 will get the focus. This can be done with a public boolean variable in FORM2 and ACTIVATE() event in FORM2. Code below might need some modifications in order to get it to work with your app.
    Code:
    ' In FORM2
    Public bForm1Loaded as Boolean
    
    ' call this sub to show Form1
    Sub ShowForm1()
      Form1.Show
      bForm1Loaded = True
    End Sub
    
    ' Activate event
    Private Sub Form2_Activate()
      if (bForm1Loaded) then Form2.SetFocus()
    End Sub
    Code:
    ' In FORM1
    ' reset the public variable in FORM2
    Private Form2_QueryUnload(Cancel As Integer, UnloadMode As Integer)
      Form2.bForm1Loaded = False
    End Sub
    Of course, you can also use an API SetWindowPos() to achieve the same result. But try one of the methods above first.

    Good Luck,
    -Cool Bizs

  3. #3
    Join Date
    Sep 2001
    Location
    IL, USA
    Posts
    1,090

    Re: Form1 only always on top on Form2, How to do...

    Originally posted by Phogang
    Form1 only always on top on Form2, How to do...

    Thank You.
    'Add to form1 the following code.
    Code:
    Const HWND_TOPMOST = -1
    Const SWP_NOSIZE = &H1
    Const SWP_NOMOVE = &H2
    Const SWP_NOACTIVATE = &H10
    Const SWP_SHOWWINDOW = &H40
    Private Declare Sub 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)
    Private Sub Form_Load()
       SetWindowPos Me.hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE Or SWP_SHOWWINDOW Or SWP_NOMOVE Or SWP_NOSIZE
       Form2.Show
    End Sub

  4. #4
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210
    Use the ZORDER method. It is well documented in the Help files.
    The solution proposed by SKSa will put your form on top of everything, including other applicartions. T
    His may not always be desireable.
    Here is an example of using Zorder.
    1. start a new project.
    2. Add a command button to Form1
    3. Add a second form
    4. Paste the following code into form1
    5. Run the program and click the button several times.
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
    Static blsw As Boolean
    If blsw Then
        Form2.ZOrder 1
    Else
        Form2.ZOrder 0
    End If
    blsw = Not blsw
    End Sub
    
    Private Sub Form_Load()
        Form2.Show
        Form2.Move Me.Left + 2000, Me.Top + 2000
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
        Unload Form2
    End Sub

  5. #5
    Join Date
    Feb 2001
    Location
    Stamford CT USA
    Posts
    2,167
    Excellent thinking!

    -Cool Bizs

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)