Results 1 to 4 of 4

Thread: [RESOLVED] Referring to form in class of forms

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2008
    Posts
    8

    Resolved [RESOLVED] Referring to form in class of forms

    Hello all,

    Feel like this is a silly question, but I have not been able to find the answer.

    Say, I have a form called frmClass, from which I instantiate 2 forms at run time:

    Public frm1 As New frmClass
    Public frm2 As New frmClass


    If I issue the Me. keyword in source code, it responds with "frmClass" rather than "frm1" or "frm2".

    How do I make reference to the instantiated forms within the class?
    Last edited by KeithLockhart67; Apr 20th, 2011 at 12:04 PM.

  2. #2
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Referring to form in class of forms

    Just replace 'Me' with the name of the instantiated Object
    eg
    Code:
    Option Explicit
    
    Private frm1 As New Form1
    Private frm2 As New Form1
    
    Private Sub Command_Click()
    Debug.Print Hex(frm1.hWnd)
    Debug.Print Hex(frm2.hWnd)
    
    End Sub

  3. #3
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    Re: Referring to form in class of forms

    You can also do something like putting a unique value in the form's Tag property when it is created and then iterating through the Forms collection looking for the tag with the value you want. So something like this:

    Code:
    Private frm1 As New Form1
    frm1.Tag = "frm1"
    
    Private frm2 As New Form1
    frm2.Tag = "frm2"
    
    Private Sub Command_Click()
    
    Dim frm As Form
    
    For Each frm in Forms
        If frm.Tag = "frm1" Then
            Debug.Print Hex(frm.hWnd)
            Exit For
        End If
    Next
    End Sub

  4. #4

    Thread Starter
    New Member
    Join Date
    Jan 2008
    Posts
    8

    Re: Referring to form in class of forms

    Thanks, Martin.

Tags for this Thread

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