Hi :)
My app creates several forms in runtime and I want to set focus on it.
How can I make this?
Thank you
Printable View
Hi :)
My app creates several forms in runtime and I want to set focus on it.
How can I make this?
Thank you
Only one form can have focus at a time and loading the form after it is created should establish focus.
Yes, I understand that, but my real problem is a little diferent.Let me explain:
I've got a TreeView with multiple nodes, each node creates a form in runtime.
Meanwhile, the user opens another app windows, and then, clicks again in the TreeView.
I'm validating if the forms created in runtime are open or close (using a array , type boolean).
So, when the user clicks for the 2nd, 3rd, ... time in the treeview, the window should get focus,if it's open already. If not, then the app will create the form in runtime and, off-course, the new window will get focus.
What is your code for your validation array?
The var (List) is an array.
This var is based uppon the node.Key of TreeView.
When I create the form:Code:List= CInt(Replace(Node.Key, "n", ""))
When I close the form:Code:FormInUse(List) = True
Could I use API's?Code:FormInUse(List) = False
Doesn't .Showing the form set focus to it even if it's already been shown?
No, dude, because when I do "formList.show" it only show my basic form, not the runtime form created.
I'm not understanding your problem then, because testing it out both .Show and .SetFocus seem to do what you're looking for.
Create a new project with two forms. (Form1 & Form2) Add two command buttons to Form1. (Command1 & Command2) Then copy this code to Form1's module. Then run it.
Clicking Command1 will create new forms. Clicking Form2 will cause the first created form to get the focus using the .Show method. .SetFocus works as well.Code:Option Explicit
Private Sub Command1_Click()
Static lngNumber As Long
Dim frm As Form
lngNumber = lngNumber + 1
Set frm = New Form2
frm.Caption = lngNumber
frm.Show
Set frm = Nothing
End Sub
Private Sub Command2_Click()
Dim frm As Form
For Each frm In Forms
If frm.Caption = "1" Then frm.Show
Next
Set frm = Nothing
End Sub
Ok, that should work, but not in my specific case. Some forms created in runtime must have the same caption.
Forms must have an unique identifier internally, right?
If so, how can I determine it? Is there any API function to do it?
Thank you
Using API and determining the form's internal ID number is way overkill. Just make up your own unique identifier for the forms, (like, say, the treeview node's .Key property) and stuff that into the forms' .Tag property.Code:Option Explicit
Private Sub Command1_Click()
Dim frm As Form
If Not Treeview1.SelectedItem Is Nothing Then
Set frm = New Form2
frm.Tag = Treeview1.SelectedItem.Key
frm.Show
Set frm = Nothing
End If
End Sub
Private Sub Command2_Click()
Dim frm As Form
For Each frm In Forms
If frm.Tag = [some value] Then frm.Show
Next
Set frm = Nothing
End Sub
Thanks for the suggestion. I've use the tag property to track my forms.
Here it goes the code:
Thank you allCode:Private Sub Navigator_NodeClick(ByVal Node As MSComctlLib.Node)
Dim NodeClick As Integer
Dim formul As Form
On Error Resume Next
If FormInUse(NodeClick) Then
For Each formul In Forms
If formul.Tag = NodeClick Then formul.SetFocus
Next
Exit Sub
Else
Set NewForm = New Lista
NewForm.Show
NewForm.Tag = NodeClick
NewForm.load_Grid (NodeClick)
Set NewForm = Nothing
End If
End Sub
:wave:
That can't possibly work, or it is incomplete. NodeClick is always zero; you never set it to anything.