Forms focus

Posted on 2002-03-04
Last Modified: 2010-05-02
In VB, I have a main form where many functions can be performed.  The user has an option to bring up a very small form.  I am trying to determine a way to make this form visible even when it loses focus.  When the user clicks on the main form, I still want this little form to show.  Originally, I attempted to code the program to show the little form whenever the main form received focus.  This doesn't work because the user can't do anything else on the form.  Does anyone have any ideas how to accomplish this?  

Thanks very much.

Question by:DebeG
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
  • 2
  • 2
  • 2
  • +1
LVL 13

Expert Comment

ID: 6839338
Private Declare Function SetWindowLong& Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal
nIndex As Long, ByVal dwNewLong As Long)
Private Const GWL_HWNDPARENT = (-8)

Private Sub Form_Load()
SetWindowLong(SmallForm.Hwnd, GWL_HWNDPARENT, MainForm.Hwnd)
End Sub

Expert Comment

ID: 6839342

The most simple solution is to use the .Show method of the form you want to display. Set it's modal parameter to vbModeless and it's ownerform to the variable "Me".

This will yield the effect that the main form is completely accessible, and the second (small) form is and stays visible on top of the main form.


Private Sub Command1_click()
  Form2.Show vbModeless, Me
End sub

Private Sub Command2_click()
  MsgBox Form2.Visible
end sub



Expert Comment

ID: 6839344
Ah, yes, I forgat to mention the Api approach...
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

LVL 13

Expert Comment

ID: 6839352
Option Explicit

Private Declare Function SetWindowLong& Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long)
Private Const GWL_HWNDPARENT = (-8)

Private Sub Form_Load()
SetWindowLong Form1.hwnd, GWL_HWNDPARENT, Form2.hwnd
End Sub
LVL 18

Accepted Solution

mdougan earned 100 total points
ID: 6839365
Include this following code in your little form:

Public 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 Const HWND_TOPMOST = -1
Public Const SWP_NOMOVE = &H2
Public Const SWP_NOSIZE = &H1
Public Const SWP_NOZORDER = &H8

Public Sub SetFormTopmost(hwnd As Long)

SetWindowPos hwnd, HWND_TOPMOST, 0, 0, 0, 0, _

End Sub

Private Sub Form_Load()

   SetFormTopmost me.Hwnd

End Sub
LVL 18

Expert Comment

ID: 6839371
By the way, forms will only ever receive a GotFocus or LostFocus events if they have absolutely no controls on them.  If you are ever trying to code something to happen when a form gets or loses the focus then use the form's Activate or DeActivate events.

Author Comment

ID: 6839732
I tried all three suggestions and yours worked as I was expecting it to work on the first try without me having to play with it or ask any further questions.  Therefore, I am awarding you the points.  Thank you for the help and I appreciate everyone's suggestions.


Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
String manipulation in Visual Basic 7 84
using web browser with BING 40 136
Add and format columns in vb6 7 72
VB6 - Scroll Mouse wheel on Picturebox 13 97
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

726 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