Two forms sharing data

  • Thread starter Thread starter DaveE
  • Start date Start date
D

DaveE

I have a project consisting of two forms and a class that
I created. The class, called Project, has several
properties and a method. Form1 instantiates my Project
class and populates the properties from textboxes. When
the user presses the Next button, Form2 should display the
data in the class properties. I'm getting an error in
Form2 because the class object hasn't been referenced.

I'm new to the .NET environment. Could someone point me
in the right direction?

Thanks much.
 
Hi Dave,
I know that you instantiated the class in Form1, but
where did you declare the variable used to instantiate the
class and what modifier did you use? Carol
 
My code looks like this:
Public Class frmMain
Inherits System.Windows.Forms.Form
Public objProject as New Project()
....
 
Hi Dave,
Declare your object variable in a module. So add a module
to your project and declare it there. You can set it as
friend if your object is only used within your project.
If you have multiple projects in your solution and you
want to make it available to all projects, make it
public.

Carol
 
Overload the constructor of Form2 to take an instance of your project class
and when you create form2, pass in the instance that you created in Form1:

Public Class frmMain
Inherits System.Windows.Forms.Form
Public objProject as New Project()

Dim f2 As New Form2(objProject)

f2.Show



'In Form2:

Public Sub New(o As Project)
'Do something with o here
End Sub

HTH
 
Back
Top