Solved

Making a Form Always on Top

Posted on 1998-11-13
12
280 Views
Last Modified: 2006-11-17
Hello I need a function that will make a passed in form always on top of the application, but not all windows.  Basically like a Find window.
0
Comment
Question by:cavacasp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 9

Expert Comment

by:Dalin
ID: 1444815
cavacasp
Try:
In form 1,
Form2.Show, me

This will keep form2 always on top of form1
Regards
Dalin

0
 
LVL 1

Expert Comment

by:MAVERICK
ID: 1444816
Dont you use VBmodal ????

0
 

Author Comment

by:cavacasp
ID: 1444817
That does not work it is giving me an invalid procedure call on that line.  One thing that I did not mention was that these forms are MDI Child Forms.  On thing that also is that the person needs to be able to switch between the two forms, just like they can on a Find form.
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 14

Expert Comment

by:waty
ID: 1444818
Use this :

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

Sub AlwaysOnTop(frmID As Form, OnTop As Integer)
   ' *** Pass any non-zero value to Place on top
   ' *** Pass zero to remove top-mostness

   Const SWP_NOMOVE = 2
   Const SWP_NOSIZE = 1
   Const flags = SWP_NOMOVE Or SWP_NOSIZE
   Const HWND_TOPMOST = -1
   Const HWND_NOTOPMOST = -2

   If OnTop Then
      OnTop = SetWindowPos(frmID.hWnd, HWND_TOPMOST, 0, 0, 0, 0, flags)
   Else
      OnTop = SetWindowPos(frmID.hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, flags)
   End If

End Sub

0
 

Author Comment

by:cavacasp
ID: 1444819
Two things

1) Does not work with MDI Child forms.  I can get around this if necessary even though I would prefer to make the second form a MDI Child form.

2) I only want the Always on top attribute to be application wide.  If you pull up a find box it only stays on top of the application not every program that is currently running on the computer.

0
 

Expert Comment

by:SANTOSMI
ID: 1444820
use this function

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


when load a form use:

SetWindowPos hwnd, -1, 0, 0, 113, 290, conSwpNoActivate Or conSwpShowWindow

0
 

Author Comment

by:cavacasp
ID: 1444821
What is
conSwpNoActivate Or conSwpShowWindow
I'm getting variable not defined when it gets to this point.

Also how would I go about taking the always on top attribute off.
0
 

Author Comment

by:cavacasp
ID: 1444822
I need to get and answer to this question. So I have to reject it because you are not answering my comment.  If you can answer my comment then feel free to resubmit and I'll be happy to award you the points.
0
 
LVL 1

Expert Comment

by:MAVERICK
ID: 1444823
Now If I understand this correctly.....
1. You are using a MDI form
2. You want a specific MDI Child to remain on top

then there is a simple answer
Private Sub form_lost focus
form.Zorder=0
End sub

Is this what you want??


0
 

Author Comment

by:cavacasp
ID: 1444824
Which form would I put this code on the one that I want to stay on top or the one beneath it.
0
 
LVL 1

Expert Comment

by:MAVERICK
ID: 1444825
Oh sorry.... the one you want to keep on top.. :) I guess I should have mentioned that !


0
 
LVL 1

Accepted Solution

by:
movieguy earned 50 total points
ID: 1444826
' Declare the following API function in a module
Declare Function SetWindowWord Lib "User32" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal nNewWord As Long) As Long

' Place the following code in the load event of the Mdi child
' form you want to keep on top of all the other child
' forms.
' Assuming the name of the Mdi main form is mdiMain.  You will
' have to change names as needed.
SetWindowWord Me.hWnd, -8, mdiMain.hWnd

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question