Referencing an open form from VB.Net code

G

Guest

Hi All,

I have 2 forms in a project. I enter some values in some text boxes on the first form then open the second form using the following VB code:

Dim frmForm2 As Form2
frmForm2 = New Form2
frmForm2.Show()

Within a sub on the second form I need to get some of the values off of the first form (that's still open and visible).

It seems simple but I can't find the correct syntax anywhere in the help. I'm familiar with how to do this in VB6 but can't figure it out in VB.Net.

Thanks, MBP
 
G

Guest

This may not be the only/best way but I have done this in the past

The forms are just classes and therefore can be used just like any other class so as long as you know the calling form you can access its properties/methods and so on.

I create a public variable in the called form (Test1Form2) to hold a reference to the calling form. In the calling form (TestForm1) I set this variable before I do the .Show() and from then its easy. You can create properties for the data you want to pass out or method for the calling form to call to collect the data or just access the data directly.

Example Below


Public Class Test2Form1
Inherits System.Windows.Forms.Form

' Property to provide value of TB2
Public ReadOnly Property Form1sTextBox2AsProperty() As String
Get
Return Me.TextBox2.Text
End Get
End Property

' Method to supply value of TB3
Public Function GetMyTextbox3AsMethod() As String
Return Me.TextBox3.Text
End Function

Private Sub Test2Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.TextBox1.Text = "Value Got directly from the TextBox var"
Me.TextBox2.Text = "Value Passed as a Property"
Me.TextBox3.Text = "Value passed on a method call"

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim f As New Test2Form2
f.CallingForm = Me
f.Show()
End Sub
End Class
-------------------------------------------------------------------------------------

Public Class Test2Form2
Inherits System.Windows.Forms.Form


Public CallingForm As Test2Form1

Private Sub Test2Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Me.TextBox1.Clear()
' get the data direct from the field in form1
Me.TextBox1.Text = CallingForm.TextBox1.Text
' get the data from a property in form1
Me.TextBox2.Text = CallingForm.Form1sTextBox2AsProperty
' get the data by calling a method in form1
Me.TextBox3.Text = CallingForm.GetMyTextbox3AsMethod
End Sub
End Class

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

A better way would be to get the reference to the calling form out of the Form_Load sender parameter, but I havnt yet worked out how to do this.


Hope this helps
 
G

Guest

Hi

This may not be the best/only way to do what you want but I have used it in the past.

Forms are just classes/objects and will act like any other. Therefore if you know the form that is calling you you can access the data in it directly, via properties or methods.

In the Called form add a variable to hold a reference to the Calling form.
In the calling form set this reference before calling the .Show method
Then its up to you how you get the data from the calling form.

Example shows all 3 methods (clearly I hope)

--------------The calling form-----------------------------------------------

Public Class Test2Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "
#EndRegion

' Property to provide value of TB2
Public ReadOnly Property Form1sTextBox2AsProperty() As String
Get
Return Me.TextBox2.Text
End Get
End Property

' Method to supply value of TB3
Public Function GetMyTextbox3AsMethod() As String
Return Me.TextBox3.Text
End Function

Private Sub Test2Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.TextBox1.Text = "Value Got directly from the TextBox var"
Me.TextBox2.Text = "Value Passed as a Property"
Me.TextBox3.Text = "Value passed on a method call"

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim f As New Test2Form2
f.CallingForm = Me
f.Show()
End Sub
End Class

----------------End of calling form-----------------------------------------------


---------------The called form -----------------------------------------------------

Public Class Test2Form2
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "
#End Region

Public CallingForm As Test2Form1

Private Sub Test2Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

' get the data direct from the field in form1
Me.TextBox1.Text = CallingForm.TextBox1.Text
' get the data from a property in form1
Me.TextBox2.Text = CallingForm.Form1sTextBox2AsProperty
' get the data by calling a method in form1
Me.TextBox3.Text = CallingForm.GetMyTextbox3AsMethod
End Sub
End Class

---------End of called form----------------------------------------------------

A better way would be to use the sender parameter of the Form_Load event handler which contains a "CallingForm" property but I cant find a way to unwind sender into a usable object. If anybody gets to this line and knows how please post a HOW TO on this and Ill get mailed.


Hope this helps
 
G

Guest

Hi Chris,

Thanks for the informative answer. It was a big help. Being an old-school, BASIC programmer I tend to think in terms of traditional variables instead of objects.

Unfortunately, it didn't completely solve my problem. Your solution works great except when the called form will be processing the code in the "Sub New" method. My guess is that when the calling form does the

Dim f As New Test2Form2
f.CallingForm = Me

from your example the New method is fired before the f.CallingForm = Me takes place. I'm trying to launch the Crystal Report viewer embedded in the called (second) form and pass it parameters that are entered on the first form. I can't get the Crystal Viewer form to work right unless the code is in the "New" subroutine. I'm looking around now for a solution to this.

Thanks again, MBP
 
G

Guest

Hi again,

Ok that made me think!

I have to wonder why you are doing whatever you are doing in the New() constructor but thats another bag of worms.

You could try this though. You can overload the default constructor "sub New()" with your own constructor including the params you need on it. You can then call the default constructorin your new one as the first line in your new constructor or just move all the code from the old to the new. You can even remove the default constructor if you wish.

Public Sub New(ByVal P1 As String, ByVal P2 As String, ByVal P3 As String)
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

' ADD all your Crystal code here
Me.TextBox1.Clear()
' get the data direct from the field in form1
Me.TextBox1.Text = P1
' get the data from a property in form1
Me.TextBox2.Text = P2
' get the data by calling a method in form1
Me.TextBox3.Text = P3
End Sub


Good Luck Chris
 
G

Guest

Hi Chris,

The samples that I found for using Crystal within VB.Net had the login and parameter passing stuff in the New() constructor. That's why I had it there. I tried moving it to the .Load event handler so I could use your solution for pulling the parameter from the other form but then the Crystal stuff wouldn't work right.

Anyway, I finally figured out that I should create a Public Sub in the second form (the form with the viewer in it) and put all the Crystal stuff there and then call this method from the first form once I'd set the "form public variable" (CallingForm). I got it working now. Thanks again for all the help.

Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top