Solved

Forms focus

Posted on 2002-03-04
7
332 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
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
0
 
LVL 3

Expert Comment

by:daffyduck14mil
ID: 6839342
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
ID: 6839344
Ah, yes, I forgat to mention the Api approach...
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 13

Expert Comment

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

Accepted Solution

by:
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, _
SWP_NOZORDER + SWP_NOMOVE + SWP_NOSIZE

End Sub

Private Sub Form_Load()

   SetFormTopmost me.Hwnd

End Sub
0
 
LVL 18

Expert Comment

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

Author Comment

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

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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 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…

790 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