Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

Passing a reference to Form1 into Form2

I am using VB.NET and would like to access the controls on Form1 from within Form2.  I have been told one way of doing this is to by passing a reference to Form1 into Form2 either in the constructor or in a public property. I understand the idea of constructors, but I am not sure how use a constructor to pass a reference of a form.

I have used this concept in C++, but do not understand the concept within VB.NET.

Any hepl would br great!!.

Stan Anderson
Tuesday, January 04, 2005

Create an app with two forms: Form1 and Form2.
Put a textbox on Form1

Create a constructor on Form2:

Public Sub New(ByVal frm1 As Form1)
    Dim s As String = frm1.TextBox1.Text
End Sub

"Public Sub New" is a VB.NET constructor for the class it appears in. Now you can have code like this:

Dim frm1 As New Form1
frm1.Show()
Dim frm2 As New Form2(frm1)
frm2.Show()

Geert-Jan Thomas
Tuesday, January 04, 2005

I think that in the last couple of issues of Visual Studio magazine you will find an article on this very topic.

Paul
Tuesday, January 18, 2005

*  Recent Topics

*  Fog Creek Home