Refering to an instance of a class from a form

In VB.net, there is a method of a class that opens a form in "showdialogue".  How can I reference in the code of the form (for example, the form_Load event procedure) the instance of the class that opened the form and that class object's properties?
LVL 1
Declan_BasileITAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Éric MoreauSenior .Net ConsultantCommented:
you will to pass a reference of your class to your form (to the constructor normally)
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
If the class that is doing the opening is also a form, then pass it to ShowDialog().  It can then be retrieved with the Owner() property.  For instance, if Form1 opens Form2:
   ' ... in Form1 ...

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ' ... in Form1 ...
        Dim f2 As New Form2
        f2.ShowDialog(Me) <-- passing a reference to our Form1 instance to Form2
    End Sub

Open in new window


Now over in Form2:
    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim f1 As Form1 = DirectCast(Me.Owner, Form1) ' <-- access the Owner() property
        f1.Text = "Hello from Form2!"
    End Sub

Open in new window


----------------------------------------------------------------------------------------------------

If the thing doing the opening is simply a Class, then as Eric suggested, pass the reference to the constructor of the Form being opened:
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ' ... in Form1 ...
        Dim c1 As New Class1
        c1.Foo()
    End Sub

Open in new window


In Class1:
Public Class Class1

    Public Sub Foo()
        Dim f2 As New Form2(Me) ' <-- passing a reference to our Class1 instance to Form2
        f2.ShowDialog()
    End Sub

    Public Function Bar() As String
        Return "Hello from Class1!"
    End Function

End Class

Open in new window


...and over in Form2:
Public Class Form2

    Private c1 As Class1

    Public Sub New(ByVal c1 As Class1)
        Me.c1 = c1
        InitializeComponent()
    End Sub

    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        If Not c1 Is Nothing Then
            Me.Text = c1.Bar()
        End If
    End Sub

End Class

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Declan_BasileITAuthor Commented:
Thank you Mike.  I needed all the code to show me how to pass a reference to a constructor.  It worked great!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.