Forms

Floating toolwindows

Many of you will have tried to create floating tool windows, that always appear on the top of the screen, like find & replace dialogs, which let you edit the text as you go. It would seem that the only way to do this is to use the SetWindowPos API call, using code like below

Public Declare Function SetWindowPos Lib "user32" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, y, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long

Public Const HWND_TOPMOST = -1
Public Const SWP_NOMOVE = &H2
Public Const SWP_NOSIZE = &H1
Public Const TOPMOST_FLAGS = SWP_NOMOVE Or SWP_NOSIZE

Public Sub MakeTopMost(hWnd As Long)
    SetWindowPos hWnd, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS
End Sub

However, if you want to create a window like a find dialog, that window must only appear on top of your own program. It's no good having a find dialog that floats on top of everything! In fact, there is a very easy way to make a window appear on top of your program, which is so obvious I have missed it for ages. What's more, the VB help doesn't tell you it either. To make a window appear on top of your program, all you need to do is:

frmFind.Show vbModeless, frmMain

where frmFind is the floating window, and frmMain is the parent window. vbModeless sets that other windows in your application can be opened at the same time. The frmMain sets the parent window, which makes the window appear on top. If you don't specify this, the window will not appear on top. Amazingly simple, and amazingly easy to miss!

You might also like...

Comments

About the author

James Crowley

James Crowley United Kingdom

James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audien...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” - Brian Kernighan