Keeping forms at the front of the screen

I'm trying to write a simple calculator program in VB5 that is always on top of other windows, so that I can copy its results into other applications without the calculator disappearing into the background.

I know it's just a simple property setting with Delphi, but I'm clueless as to how this can be done with VB.
Phaedra74Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
VbmasterConnect With a Mentor Commented:
Add this to the form module...

   Private 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

   Property Let TopMost(sTopmost As Boolean)

      Const HWND_TOPMOST = -1
      Const HWND_NOTOPMOST = -2

     Select Case sTopmost
     Case Is = True
        Call SetWindowPos(Me.hWnd, HWND_TOPMOST, Me.Left \ Screen.TwipsPerPixelX, Me.Top \ Screen.TwipsPerPixelY, Me.Width \ Screen.TwipsPerPixelX, Me.Height \ Screen.TwipsPerPixelY, 0)
     Case Is = False
        Call SetWindowPos(Me.hWnd, HWND_NOTOPMOST, Me.Left \ Screen.TwipsPerPixelX, Me.Top \ Screen.TwipsPerPixelY, Me.Width \ Screen.TwipsPerPixelX, Me.Height \ Screen.TwipsPerPixelY, 0)
     End Select
 
   End Property

This will add a property to the form so you can use simple code like this to put the form on top...

   formname.TopMost = True

.... and code like this to change it back to 'standard' mode...

   formname.TopMost = False
0
All Courses

From novice to tech pro — start learning today.