Solved

Forms focus

Posted on 2002-03-04
7
354 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
[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
  • 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

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.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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 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…

690 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