Solved

Forms focus

Posted on 2002-03-04
7
299 Views
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.

0
Comment
Question by:DebeG
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 13

Expert Comment

by:crazyman
Comment Utility
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
0
 
LVL 3

Expert Comment

by:daffyduck14mil
Comment Utility
Hi,

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.

Example:

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

Private Sub Command2_click()
  MsgBox Form2.Visible
end sub

Grtz.©

D.
0
 
LVL 3

Expert Comment

by:daffyduck14mil
Comment Utility
Ah, yes, I forgat to mention the Api approach...
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 13

Expert Comment

by:crazyman
Comment Utility
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
0
 
LVL 18

Accepted Solution

by:
mdougan earned 100 total points
Comment Utility
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, _
SWP_NOZORDER + SWP_NOMOVE + SWP_NOSIZE

End Sub

Private Sub Form_Load()

   SetFormTopmost me.Hwnd

End Sub
0
 
LVL 18

Expert Comment

by:mdougan
Comment Utility
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.
0
 

Author Comment

by:DebeG
Comment Utility
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.

0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now