In reference to this post http://www.daniweb.com/forums/thread43763.html

I have another more specific question about passing data between forms in VB.net
I have a form with over 50 variables that I need to access on a second form and if i followed the method above, my form 2 will end up way too long and time consuming.

I'm making a complex calculation software that prompts user inputs values and calculates all those unknown variables. I need to display all the resulting values in the 2nd form. and from the 2nd form , i want to give the users an option to export all those values to an ms Excel sheet.

Here is an example of what I did in reference to the guide from the above link, Is there anyway to group the multiple properties into one for form2?
Feel free to copy and paste the code.

'form1 with a button and textbox
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Obj1, Obj2 As New Form2
        Dim TDX, TBX, a As Decimal
        a = TextBox1.Text 'user input value
        b = 1 + a
        c = 2 + a
        Obj1.TDX = b
        Obj2.TBX = c

          Obj1.Show()
          Obj2.Show()
    End Sub

End Class
'form2: 2 labels
Public Class Form2

    Public _b, _c As Decimal

    Public Property [b]() As Decimal

        Get
            Return _b
        End Get
        Set(ByVal b As Decimal)
            _b = b
        End Set
    End Property

    Public Property [c]() As Decimal
        Get
            Return _c
        End Get
        Set(ByVal c As Decimal)
            _c = c
        End Set
    End Property


    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Label1.Text = _b
        Label2.Text = _c
    End Sub
End Class

Thank you

Sip

See if this helps.

Public Class Form1
    Public a, b, c As Decimal
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Not IsNumeric(TextBox1.Text) Then Exit Sub
        Dim Obj1, Obj2 As New Form2
        a = CDec(TextBox1.Text)
        b = 1 + a
        c = 2 + a
        Obj1.Label1.Text = CStr(b)
        Obj2.Label2.Text = CStr(c)
        Obj1.Show()
        Obj2.Show()
    End Sub
End Class
Public Class Form2

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        With Form1
            Label1.Text = CStr(.a)
            Label2.Text = CStr(.b)
        End With
    End Sub
End Class

Thanks! it works. but how do i prevent form 2 from loading twice?

Dim Obj1 As New Form2
        '// other code here.
        Obj1.Show()

Why you don't just pass the first form as a parameter to the second form. it's much simpler and more "clean" and basically you use the forms as object which is the whole point of OO

Declare a property on form2
Private mForm1 As form1
Public Property objForm1 () As Form1
Get
Return mForm1
End Get
Set(ByVal value As Form1 )
mForm1 = value
End Set
End Property

in your second form's constructor set
Public Sub new( ByVal tempForm1 as Form1)

'''''''
''''''
objForm1= tempForm1

end sub

Thanks for the suggestion. Could you be more elaborate as in a full example would be ideal as I'm really new to vb.net programming.

Form2 coding

'Property
Private mForm1 As form1
Public Property objForm1 () As Form1
Get
Return mForm1
End Get
Set(ByVal value As Form1 )
mForm1 = value
End Set
End Property

' constructor
Public Sub new( ByVal tempForm1 as Form1)

'''''''
''''''
objForm1= tempForm1

end sub

To use the variables you need you simple use the objForm1 property like this:

textbox.text=objForm1.variable1

Form1 coding

dim Form2Instance as new Form2(me)

form2Instance.show()
me.hide()
.....

in words you create a copy of your form1 and place it as a property to your second form.
this way you don't need to reference form1 to get anything from that form(e.g form1.variable1) and you don't need to pass all 50 variables to form2 one by one.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.