Focus In Modeless Form (spare me the grief)
Hi,
Control's focus is effective only in its container object.
Even if a userform loses focus, active-control keeps focus
while its container(userform) is alive, so 2nd 'enter' is not raised.
I might have misunderstood what you mean, but made some code:
'exsample-1 (Excel 2000)
'assuming that UserForm1 has a commandbutton.
'Standard module
Sub ShowUserForm()
If UserForm1.Visible Then
UserForm1.Show vbModeless
UserForm1.Tag = "ResetFocus"
UserForm1.CommandButton1.SetFocus
UserForm1.Tag = ""
UserForm1.TextBox1.SetFocus
Else
UserForm1.Show vbModeless
UserForm1.TextBox1.SetFocus
End If
End Sub
'UserForm1
Private Sub TextBox1_Enter()
Me.TextBox1.BackColor = &HFFFF&
End Sub
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Me.Tag = "" Then
Cancel = True
Me.TextBox1.BackColor = &H80000005
AppActivate Application.Caption
End If
End Sub
'exsample-2
'Standard module
Sub ShowUserForm()
UserForm1.Show vbModeless
With UserForm1.TextBox1
.BackColor = &HFFFF&
.Visible = False
.Visible = True
.SetFocus
End With
End Sub
'UserForm1
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Cancel = True
Me.TextBox1.BackColor = &H80000005
AppActivate Application.Caption
End Sub
--
HTH,
okaizawa
ThinkImInTrouble wrote:
I hope someone out there can help me.
I have a modeless Form. It has 1 label and one textbox.
on exit of the textbox(by user hitting enter) It looses it focus to
Excel. However When I give focus back to the userform by
AppActivate(userform1.caption) Textbox1 has the focus, However there
is no cursor indication and I can not overwrite what is currently in
the field, it only appears to allow me to backspace over the previous
entry. Here is the code I am using :
Module 1 -- code:
Sub ShowUserForm()
If UserForm1.Visible = True Then
AppActivate (UserForm1.Caption)
Else
UserForm1.Show (vbModeless)
End If
End Sub
UserForm1 -- code:
Private Sub TextBox1_Enter()
Me.TextBox1.BackColor = &HFFFF&
End Sub
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Cancel = True
Me.TextBox1.BackColor = &H80000005
AppActivate ("Microsoft excel")
End Sub
Private Sub UserForm_Activate()
Me.TextBox1.SetFocus
End Sub
|