[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 250
  • Last Modified:

Show form on top of everything

Hello all,

I need to have a VB6 form on top of all application open on my PC, to be sure that i see it.

So when i'm calling the form, i'm using this code:
Call ShowDialogInformation(txtHeader, txtMessage)

The form as this code:
Private Sub Form_Load()
 
  ' Make this form hidden until the client
  ' informs it to display on the screen.
  Me.Hide
 
  ' Set the header color
  LabelHeader.ForeColor = vbRed
  LabelHeader.Caption = vbNullString
 
  ' Set the message color
  LabelMessage.ForeColor = vbBlack
  LabelMessage.Caption = vbNullString
 
 
End Sub


How can i do this please.

Thanks for your help
0
Wilder1626
Asked:
Wilder1626
  • 6
  • 4
  • 3
  • +2
2 Solutions
 
Death259Commented:
The attribute you are wanting is TopMost. You can set this in the form properties, or programmatically by using FormName.TopMost = true
0
 
Death259Commented:
You could probably also just do Me.TopMost = true
0
 
Wilder1626Author Commented:
Hello,
I did it, but i have a compile error: Method or Data not found

Private Sub Form_Load()
Form1.TopMost = True


  ' Make this form hidden until the client
  ' informs it to display on the screen..
  Me.Hide
 
  ' Set the header color
  LabelHeader.ForeColor = vbRed
  LabelHeader.Caption = vbNullString
 
  ' Set the message color
  LabelMessage.ForeColor = vbBlack
  LabelMessage.Caption = vbNullString
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Death259Commented:
Try it with Me.TopMost = True
0
 
Wilder1626Author Commented:
I have the same error.

It highlight: .TopMost =
0
 
Brook BraswellApplication Development ManagerCommented:
if your code is as such
Private Sub Form_Load()
Form1.TopMost = True
then should not the code be Form rather than Form1?

Private Sub Form_Load()
Form.TopMost = True

0
 
RobinSoftware EngineerCommented:
In VB6, the form does not have a .TopMost property.

Use the SetWindowPos API function for this:
Private Const SWP_NOMOVE = 2
Private Const SWP_NOSIZE = 1
Private Const Flags = SWP_NOMOVE Or SWP_NOSIZE
Private Const HWND_TOPMOST = -1
Private Const HWND_NOTOPMOST = -2
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

Public Sub SetWindowOnTop(ByVal p_hWnd As Long, Optional ByVal p_bOnTop As Boolean = True)
    If p_bOnTop Then
        Call SetWindowPos(p_hWnd, HWND_TOPMOST, 0, 0, 0, 0, Flags)
    Else
        Call SetWindowPos(p_hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, Flags)
    End If
End Sub

Open in new window

0
 
RobinSoftware EngineerCommented:
And call it like:

Call SetWindowOnTop(Me.hWnd, True)
0
 
nffvrxqgrcfqvvcCommented:

How To Create a Form That Always Stays on Top
http://support.microsoft.com/kb/184297
Add the microsoft code to a module. Then in the Form_Load() make the call..
call SetTopMostWindow(Me.hwnd, True)

0
 
nffvrxqgrcfqvvcCommented:
Sorry robin... I never refreshed.
0
 
Wilder1626Author Commented:
I have put this code under General Declaration:
Private Const SWP_NOMOVE = 2
Private Const SWP_NOSIZE = 1
Private Const Flags = SWP_NOMOVE Or SWP_NOSIZE
Private Const HWND_TOPMOST = -1
Private Const HWND_NOTOPMOST = -2
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

Public Sub SetWindowOnTop(ByVal p_hWnd As Long, Optional ByVal p_bOnTop As Boolean = True)
    If p_bOnTop Then
        Call SetWindowPos(p_hWnd, HWND_TOPMOST, 0, 0, 0, 0, Flags)
    Else
        Call SetWindowPos(p_hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, Flags)
    End If
End Sub


After, under:
Private Sub Form_Load()
Me.TopMost = True


But again, same error.
0
 
Death259Commented:
Get rid of the me.topmost
0
 
RobinSoftware EngineerCommented:
@egl1044: no problem.

Set the form topmost by calling:

Private Sub Form_Load()
   Call SetWindowOnTop(Me.hWnd, True)
End Sub

0
 
Wilder1626Author Commented:
Ok, now, no more errors but it is not on top of everything. It's always under all oder form.

I really dont understand.

I've tried both solution, and same results

0
 
nffvrxqgrcfqvvcCommented:
Make sure you test it as an executable and not from the VB IDE. Do you see the same behavior?
0
 
Wilder1626Author Commented:
Yes yes yes.

It's perfect.

Thanks to both of you.
0
 
Wilder1626Author Commented:
Thanks to both of you.
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 6
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now