Results 1 to 12 of 12

Thread: [RESOLVED] SetFocus on form created in RunTime

  1. #1

    Thread Starter
    Hyperactive Member RS_Arm's Avatar
    Join Date
    Mar 2007
    Location
    Planet Earth
    Posts
    282

    Resolved [RESOLVED] SetFocus on form created in RunTime

    Hi

    My app creates several forms in runtime and I want to set focus on it.
    How can I make this?

    Thank you

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: SetFocus on form created in RunTime

    Only one form can have focus at a time and loading the form after it is created should establish focus.

  3. #3

    Thread Starter
    Hyperactive Member RS_Arm's Avatar
    Join Date
    Mar 2007
    Location
    Planet Earth
    Posts
    282

    Re: SetFocus on form created in RunTime

    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.

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: SetFocus on form created in RunTime

    What is your code for your validation array?

  5. #5

    Thread Starter
    Hyperactive Member RS_Arm's Avatar
    Join Date
    Mar 2007
    Location
    Planet Earth
    Posts
    282

    Re: SetFocus on form created in RunTime

    The var (List) is an array.
    This var is based uppon the node.Key of TreeView.

    Code:
    List= CInt(Replace(Node.Key, "n", ""))
    When I create the form:
    Code:
    FormInUse(List) = True
    When I close the form:
    Code:
    FormInUse(List) = False
    Could I use API's?

  6. #6
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: SetFocus on form created in RunTime

    Doesn't .Showing the form set focus to it even if it's already been shown?

  7. #7

    Thread Starter
    Hyperactive Member RS_Arm's Avatar
    Join Date
    Mar 2007
    Location
    Planet Earth
    Posts
    282

    Re: SetFocus on form created in RunTime

    No, dude, because when I do "formList.show" it only show my basic form, not the runtime form created.

  8. #8
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: SetFocus on form created in RunTime

    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

  9. #9

    Thread Starter
    Hyperactive Member RS_Arm's Avatar
    Join Date
    Mar 2007
    Location
    Planet Earth
    Posts
    282

    Re: SetFocus on form created in RunTime

    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

  10. #10
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: SetFocus on form created in RunTime

    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
    Last edited by Ellis Dee; May 31st, 2007 at 02:57 PM.

  11. #11

    Thread Starter
    Hyperactive Member RS_Arm's Avatar
    Join Date
    Mar 2007
    Location
    Planet Earth
    Posts
    282

    Resolved Re: SetFocus on form created in RunTime

    Thanks for the suggestion. I've use the tag property to track my forms.
    Here it goes the code:

    Code:
    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
    Thank you all

  12. #12
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: [RESOLVED] SetFocus on form created in RunTime

    That can't possibly work, or it is incomplete. NodeClick is always zero; you never set it to anything.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width