Within A Windows Desktop App - How To Interchange Data Between Multiple Windows

Greetings:

I am new to Windows desktop development but not new to programming. I'm looking for some pointers please.

I am writing a Windows desktop application using Visual Studio 2015 Pro and Visual Basic .NET. There is currently 1 window the application uses. I would like to create additional windows for the application, however, I need help on how windows interchange data with other windows in the same application.

Much thanks in advance for any advice
David BachAsked:
Who is Participating?
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.

Jacques Bourgeois (James Burger)PresidentCommented:
There are many ways to do that.

First, a few techniques that you should not use. I take the time to speak about them, because otherwise, you would get answer telling you that these are the easiest ones. Yes, they are the easiest... during development. But they are a pain in maintenance and debugging.

A bad one, but the easiest is to declare public variables in a module. Public variables in a module are visible everywhere in the application. Why is it a bad one? Because the variables are visible everywhere in the application. Changing the value anywhere could have a dramatic impact somewhere else. They are a pain to debug, because the bug can come from anywhere in the applications.

Another bad one is that some programmers use application settings. Settings are data saved in an XML file and usable everywhere in the application throught classes provided by the framework. This has the same negative impact as Public variables. The role of application settings is to keep information between runs of the application, not as a centralized point for values.

Are there good ways to pass information between Forms (or any other class; forms are only a class)? You bet. But they require more work on your part, and work only if one of the forms holds a variable to the other one.

The first one is to add a constructor on the "secondary" form, a New method that accepts parameters. The "primary" form can thus initialize the "secondary" by passing these parameters when it instantiates it:  Dim secondForm as New SecondForm(param1, param2, param3...). By recording these parameters in form level variables, they can be used anywere in the "secondary" form.

The second technique is to create properties in the "secondary" form. The first form can then use these properties to pass data to the second form:

     Dim secondForm as New SecondForm
     secondForm.TheProperty = TheValue

If you want to do the thing in reverse, having the secondary form send information to the "primary", you can use a combination of both techniques, passing the "primary" to the "secondary" constructor, so that it have a reference to the "primary" and thus access to its properties, something like the following:

In the "primary":
     Dim secondForm as New SecondForm(Me) 'Me is the form in which the code is running

In the "secondary"
     Public Sub New (primary as FirstForm)
     primary.TheProperty = TheValue
1

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
David BachAuthor Commented:
Thank you!!!

That was an excellent write up! I appreciate your time in responding to me. I shall take what you wrote and endeavor to use it wisely.


Thank you again!
0
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
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.