Hello Visual Basic Coding Experts,
I have other application that I have not made myself, but can do what I am looking to do. I want a code to keep a form on top of all the forms in my application. Not Topmost, which keeps it open onto of all open application. I have tried TopLevel, but have no luck.

Thanks so much in advance,

Kanyonb38

Attachments Help_with_Visual_Basic_2.png 103.32 KB

I think you want to use the vbmodal parameter with the form.Show statement.
A modal form has focus and does not let you interact with other forms in the application until it has been closed.

form1.show

opens a non-modal form, you can set focus to other forms in the app.

form1.show vbmodal

opens a modal form, must be closed before you can access other forms.

Edited 4 Years Ago by LesF: n/a

Yes, but you need to use the following Windows API to do that.

BOOL SetWindowPos(
HWND hWnd, // handle to window
HWND hWndInsertAfter, // placement-order handle
int X, // horizontal position
int Y, // vertical position
int cx, // width
int cy, // height
UINT uFlags // window-positioning flags
);
(MSDN)

Public Const HWND_TOPMOST = -1
Public Const SWP_SHOWWINDOW = &H40

Public Declare Function SetWindowPos Lib "user32" Alias "SetWindowPos" (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 Function SetMyWindowPos(ByVal WndHdl as long, ByVal ByPosition as Long, ByVal Left as long, ByVal Top as Long, ByVal WinWidth as long, ByVal WinHght as Long) as Long
   Dim lngReturn as Long
   lngReturn = SetWindowPos(WndHdl, ByPosition, WinLeft, WinTop, WinWidth, WinHght)
   ' lngReturn should return a  nonzero value if successful, 0 if it fails

End Function()

Use the HWND_TOPMOST Constant for the second parameter, and the SWP_SHOWWINDOW Constant for the last parameter.
Something like the above should work. Ask, if you have further questions. If you don't it be on top when you leave the application, then you'll have to write extra code to deal with that situation: e.g. when your application loses focus, and again when your application returns focus to put the back on top again.

Edited 4 Years Ago by hkdani: n/a

This article has been dead for over six months. Start a new discussion instead.