Question Share data between forms (as opposed to passing data)

Cobalt

Member
Joined
May 28, 2008
Messages
21
Programming Experience
3-5
Quite a few threads on passing data from one form to another, but I'm struggling to work out how to share data between the two - back and forth.

For example:

Class1 is a class with 10 string properties

Form1 has 10 labels ("Label1", "Label2" etc) and a [Configure] button.

Form2 has 10 textboxes and a [Apply] button.

I want to load with Form1, and create an instance of Class1. Click the Configure button and load Form2, passing Class1. On Form2 I want to manually fill in the 10 textboxes and hit Apply. The Apply should update the Class1 instance and close the form.

Form1's Label controls should then be updated with the data from the Class1 instance.

I'm fine creating the forms and classes, as well as passing the class into Form2 with a custom Sub New(EmptyClass as Class1) constructor. This gets the empty class into Form2, but how can I get the populated class back to Form1?

As Form1 already exists I don't want to create a new instance with another custom Sub New(PopulatedClass as Class1)

It's also key to be able to use the Form2 multiple times, basically ending up with a Data class being displayed as part of Form1 but updated using Form2? (For example if the Class has already been created and populated with data when it's passed back to Form2 for the second time I would pre-load the Textboxes with the values already in the class).

Thanks for any suggestions!
 
if I understood your post try this*
At the apply button event must write "form1.labelname.text=textbox1.text" but for each label of 1 with its textbox of form2
 
Here is an example of one form creating another, passing data in, displaying it as a dialogue and then getting data back:
Using dialogue As New Form2
    dialogue.SomeProperty = someValue

    If dialogue.ShowDialog() = Windows.Forms.DialogResult.OK Then
        someOtherValue = dialogue.SomeOtherProperty
    End If
End Using
It's up to you to define properties in the second form in the number and type you require.
 
Thank you for the advice jmcilhinney, this solution worked very well for me. This is a useful piece of information, and not something I had found in my searches up until now.

Cheers
 

Latest posts

Back
Top