I want to set focus on a textfield on one form, from 开发者_StackOverflow中文版another form. How do I accomplish this in VB6?
Assuming you have Form1
and Form2
with a TextBox
on Form2
,
In the Command1_Click
of Form1
's Module:
Private Sub Command1_Click()
Load Form2
Form2.Show
Form2.SetFocus
If Form2.Text1.Visible And Form2.Text1.Enabled Then
Form2.Text1.SetFocus
End If
End Sub
You can not set focus to a text field on another form if that form is not visible. So you should check first if the form is visible
If (form2.Visible)
begin
txtBox2.SetFocus()
end
Assuming that FormA wants to set the focus on Text1 on FormB I think you could just do something like:
In FormB create a sub called FocusOnText1
and in that sub have the code Text1.SetFocus
and then just call that sub from FormA.
As Jangwenyi says though, you need to make sure it's visible, so I'd suggest adding something like his if statement in the FocusOnText1
sub since otherwise you might get errors (Error 5 I think).
精彩评论