Pass data betwee winform and usercontrols

Hi,
I have created a 'wizard'. Each section is a UserControl being shown or hiden by the main form. Now I need the main form to collect computed values from UserControlA and pass it on to the next UserControlB to do its own computations. eg:

UserControlA does                      UCA.TextBox1.Text + UCA.TextBox2.Text = ResultA
UserControlB should do             UCB.TextBox1.Text * ResultA = ResultB

When done, Form should display ResultB

How can I achieve this?

VB.NET/C#

Thanks
mambo2015Asked:
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.

ChloesDadCommented:
Use properties of the forms

In formA

Private _ResultA as Double
Public Property ResultA as Double
Get 
Return _ResultA
End
Set (value as double)
_ResultA = value
End

Open in new window



In FormB

Private _ResultA as double
Public Property ResultA as Double
Get 
Return _ResultA
End
Set (Value as double)
_ResultA = Value
End

To CalculateResultB
ResultB=convert.Todouble(UCB.TextBox1.Text) * ResultA
end

Open in new window



In your main Code

FormA.showDialog
FormB.ResultA = FormA.ResultA
FormB.ShowDialog

Open in new window


You will also need to set ResultA before from within formA and call CalculateREsultB as a suitable place in FormB
mambo2015Author Commented:
Thanks, ChloesDad,

This is what I have been trying but the problem is:

When I am in UserControlB, the method/variable declared in the property clause is not visible to UserControlA and vice-versa.

In the first UserControl:

Public Class SubjectUserControl

    Private UCA As String
    Public Property _UCA() As String
        Get
            Return UCA
        End Get
        Set(ByVal value As String)
            UCA = value
        End Set
    End Property

    Private Sub FillSubjects()

        UCA = "Math"

    End Sub

End Class

In second UserControl:

Public Class ClassUserControl

    Private UCB As String
    Public Property _UCB() As String
        Get
            Return UCB
        End Get
        Set(ByVal value As String)
            UCB = value
        End Set
    End Property

    Private Sub FillClass()

       UCB = SubjectUserControl._UCA '-this is not visible!
       UCB &= " " & "Class 2"

    End Sub

End Class

In the main form:

Private Sub FinalResult()
    Result = _UCB '- This is not visible!
End Sub

This is where I am stuck!

Also note these are UserControls, being loaded into/shown in a Form
ChloesDadCommented:
You missed the point that A does not need to know B

you do it in the main form, set a property on B equal to a property on A

And also you got the _ wrong

It is convention to use _propertyName as a holding variable for PropertyName

So
Public Class SubjectUserControl

     Private _UCA As String
     Public Property UCA() As String
         Get
             Return _UCA
         End Get
         Set(ByVal value As String)
             _UCA = value
         End Set
     End Property

     Private Sub FillSubjects()

         UCA = "Math"

     End Sub

 End Class

Open in new window


and

Public Class ClassUserControl

     Private _UCB As String
     Public Property UCB() As String
         Get
             Return _UCB
         End Get
         Set(ByVal value As String)
             _UCB = value
         End Set
     End Property

     Private Sub FillClass()

         UCB = SubjectUserControl.UCA '-this should now be visible
        UCB &= " " & "Class 2"

     End Sub

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
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

Jacques Bourgeois (James Burger)PresidentCommented:
A simpler way would be to repeat the operation in UCB.

UCB.TextBox1.Text * (UCA.TextBox1.Text + UCA.TextBox2.Text) = ResultB

Be sure to put a comment before the line that makes the calculation in A and in B, to indicate that if you change it in one control, you should also change it in the other one.
mambo2015Author Commented:
Thanks, Jacques. I only gave a brief sample but the procedures are a lot and complex, so it will be preferable for me to handle them independently in the UserControls.
thanks, good observation, though.
mambo2015Author Commented:
Hi
This got going but unfortunately the values are not persisted in the form!

I am hiding and showing the UserControls with every click of the next button on the main form. So I am collecting the values inside the DockChanged procedure as follows:

in the ClassesUserControl:

    Private Sub ClassesUserControl_DockChanged(sender As Object, e As EventArgs) Handles Me.DockChanged
        If Me.Dock = DockStyle.Fill Then
            shown = True
        Else
            mainForm.ClassesID = ClassIDs 'the values is properly assigned here when I have the break points
            Exit Sub
        End If
    End Sub

This is the declaration in the main form:

    Private _ClassesID As String = String.Empty
    Public Property ClassesID() As String
        Get
            Return _ClassesID
        End Get
        Set(ByVal value As String)
            _ClassesID = value
        End Set
    End Property

The procedure that manages the wizard in the main form is as follows:

    Private Sub MoveControls(ByVal direction As Integer)

        With Me.mainSplitContainer
            .Panel2.Controls(pos).Hide()
           .Panel2.Controls(pos).Dock = DockStyle.None '- when i break the code here ClassesID has values; but as soon as the code goes past this line, the values is reset to empty string
        End With

End Sub

After all the UserControls are closed, I try to use the values in the final procedure in the main form as follows:

Private Sub UseTheValues()
    For Each s As String in _ClassesID ' this is empty
           Dim first as string = s
    Next
End Sub
mambo2015Author Commented:
I eventually had to save the values to the application settings in order to persist them. across the form.
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
.NET Programming

From novice to tech pro — start learning today.