?
Solved

Forms focus

Posted on 2002-03-04
7
Medium Priority
?
358 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
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!

 
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 400 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
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…
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…
Suggested Courses

771 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