Question Sharing variables between forms

DominicB

Active member
Joined
May 2, 2008
Messages
30
Programming Experience
Beginner
Hi all

As part of my project I have 2 forms (Form2 and Form3). In Form2 I have set up a variable, filename, and a user types something into TextBox3 and that is assigned to the variable "filename". Easy.

But now I want to pick up this variable and use it in Form3. How do I share variables between forms?

As ever, thanks for your time.

DominicB
 
The best way to do that is to create public properties that get/set the filename on both forms. Then on your main form, retrieve the filename from the one the users types in and set it in the form the user is supposed to see it.

Think like when you use the OpenFileDialog and SaveFileDialog like this :

VB.NET:
dim digOpen as new OpenFileDialog()
dim digSave as new SaveFileDialog()

if digOpen.ShowDialog() == DialogResult.OK then
    digSave.FileName = digOpen.FileName ' this is the line that copies the file name from one form to the other.

    if digSave.ShowDialog() == DialogResult.OK then
        File.Copy(digOpen.FileName, digSave.FileName)
    end if
end if

What you do with this "digSave.FileName = digOpen.FileName" is that you access the FileName property of digOpen to set the FileName property of digSave. (I always prefix my dialog names with "dig"... ;) )

All you have to do is repeat that pattern in your code by creating the properties that allow you to pass the file name around.

To do that, you must keep a reference to both forms from your main form. That way, only your main form knows about the two forms and you can change the main form to retrieve the path elsewhere as needed.

You could directly get the filename from the form that shows it using default instances, but that creates associations between your forms so you cannot use them separately anymore. Changes will become more difficult in the future.
 
Hi Stonkie

OK. Thanks for that. I think I follow it - sure is complicated, it's loads easier when you try to do it using VBA in Excel 2003:D.

Stonkie said:
To do that, you must keep a reference to both forms from your main form.
Before I get stuck in and try this (it's late here now - I'll do it in the morning), would you mind just explaining what I need to do to set a reference to the other forms.

Thanks a lot for looking.

DominicB
 
I simply meant to keep a variable referencing them.

You can simply add an instance variable like

VB.NET:
private inputForm as new Form2()
private outputForm as new Form3()

Then whenever you need them, you don't create a new one, you simply use those.

That is just the easiest way, but if that is impossible (if you cannot show the same form instance twice) you just need to have a variable of both forms at the same time. If you don't you'll have to use a variable to store the filename during the moment only one of the forms exists.
 
Hi Stonkie

This doesn't seem like the easiest concept to grasp, but at least I've got some pointers now in the right direction _ I'm going to have to have a play around with this now to try and get the hang of it.

Thanks for the nudge in the right direction.

DominicB
 
managing forms

It is a very easy concept. You use a variable to store either a primitive value or a reference for an object. A form is an instance of the Form class (or derived), just like any other class. So to be able to input/output from that object instance you need a reference point like a variable so you know "what to talk to".

In VB 2005/2008 there is also two helpers for forms; all forms have a default instance, so if you only need one instance of a form at any time you don't need to create New Form2 to use it, it is already there and you just say Form2.Show() to show it or Form2.FileName = "c:\abc.txt" for example to set value for your Filename property. The My.Forms shortcut points to the same default instances. The other is the Application.OpenForms collection that may help you keep track of all currently open forms.

When working with MDI there exist standard properties that helps managing form instances; from a MDI child you can reach the parent with MdiParent property, and from parent you can reach any child with the MdiChildren collection property.

There is also the standard property set Owner/OwnedForms that has same functionality.

As Stonkie showed above it is also common to create a new instance of a form and store reference in a local variable, to be used only briefly as a modal dialog (ShowDialog), where you can set/get properties before/after showing it, then Dispose it immediately when finished with it.
 
Back
Top