We help IT Professionals succeed at work.

Form Focus

mravell
mravell asked
on
I have a prog with two forms.

What code/events do I use to put the focus back onto form1 if the user clicks on form2? I need form1 to be the topmost, visible, having focus form.

This will be dependent upon a variable i.e.

Dim forceFocus As Boolean

If forceFocus do the code that forces form1 to have focus even if the user clicks onto form2.


Regards
Marty
Comment
Watch Question

you can use class module if you want to set a flag globally,
for eg. Public flg as boolean

  now you can check in either of the forms
  if flg is true then show form.

 Happy programming
 
Ryan ChongSoftware Tead Lead / Business Analyst / System Analyst / Data Engineer
CERTIFIED EXPERT

Commented:
Form1.SetFocus

To make always on top:

Public Const HWND_TOPMOST = -1
Public Const HWND_NOTOPMOST = -2
Public Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long
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 Sub StayOnTop(frmForm As Form, fOnTop As Boolean)
    Dim lState As Long
    Dim iLeft As Integer, iTop As Integer, iWidth As Integer, iHeight As Integer
    With frmForm
        iLeft = .Left / Screen.TwipsPerPixelX
        iTop = .Top / Screen.TwipsPerPixelY
        iWidth = .Width / Screen.TwipsPerPixelX
        iHeight = .Height / Screen.TwipsPerPixelY
    End With
    If fOnTop Then
        lState = HWND_TOPMOST
    Else
        lState = HWND_NOTOPMOST
    End If
    Call SetWindowPos(frmForm.hwnd, lState, iLeft, iTop, iWidth, iHeight, 0)
End Sub

And use Form1.Show VbModal

regards.

Author

Commented:
Alright, I think that the Form1.SetFocus is what I want (from what I read in MSDN as well as ryancys's comment) but what event do I put it in? i.e. Do I put it in an event that is fired off when form1 looses focus? Or when form2 gets the focus?

If so what is the name of the event? I've tried what I though was obvious. No joy.


Regards
Marty
Ryan ChongSoftware Tead Lead / Business Analyst / System Analyst / Data Engineer
CERTIFIED EXPERT

Commented:
Hi,

quite not sure about it but you can try:

Form_LostFocus() Event, or Form_GotFocus()..

regards.

Author

Commented:
Tried this in Form1:

Private Sub Form_LostFocus()
  Form1.setFocus
End Sub


Doesn't seem to work

M

Author

Commented:
Tried in Form2 :

Private Sub Form_GotFocus()
  Form1.SetFocus
End Sub


Also didn't work. If I click on Form2 it comes to the foreground and as form1 is smaller it gets hidden behind form2.  :(


M
Software Tead Lead / Business Analyst / System Analyst / Data Engineer
CERTIFIED EXPERT
Commented:
Hi mravell,

Here is the estimated procedures:

So if the forceFocus is True, Focus always set to Form1 even Form2 is Clicked?

So in Form2's Activate() Event, you can write this:

Private Sub Form_Activate()
    If forceFocus = True Then Form1.SetFocus
End Sub

In form1:

Private Sub Command1_Click()
forceFocus = True
Form2.Show
End Sub

?

And I really aspect to help you out bcos i think it's not difficult! :)

regards.
Ryan ChongSoftware Tead Lead / Business Analyst / System Analyst / Data Engineer
CERTIFIED EXPERT

Commented:
And don't forget to add this line in your Module:

Public forceFocus As Boolean

'Hope will help.

Author

Commented:
Ryan,

Excellent. Just needed to know that Activate was the event I needed! Having used VB for a total of 3 days I still don't know exactly what everything does.

Thanks to everyone else as well!


M
Ryan ChongSoftware Tead Lead / Business Analyst / System Analyst / Data Engineer
CERTIFIED EXPERT

Commented:
Hi mravell,

You're welcome and glad i could help you solve your problem :)
At last what i said is the answer accepted.

At last what i said is the answer accepted.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.