Link to home
Start Free TrialLog in
Avatar of MikeMCSD
MikeMCSDFlag for United States of America

asked on

Passing variables with forms

What is the easiest way to pass a variable to another form?

Public Class Form1
   Inherits System.Windows.Forms.Form

   Dim prtName As String   <<<<< need this variable for Form2
   ................

   Private Sub btnReport_Click
      Dim form2 As New Form2
      form2.ShowDialog()
      .......

At form2, I need the value of "prtName".   thanks
Avatar of GivenRandy
GivenRandy

The easiest way is probably to use the Tag property. Something like the following, but remember to cast it to a string:

Button1 on Form1
---
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim prtName As String = "Something"

        Dim form2 As New Form2
        form2.Tag = prtName
        form2.ShowDialog()
    End Sub
---

Button1 on Form2
---
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MessageBox.Show(CStr(Me.Tag))
    End Sub
---
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oops you wanted to create it like so.

        Dim f2 As New Form2(prtName)
        f2.ShowDialog()
personally i like to keep all variables in a module that way you can use them between forms easily.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MikeMCSD

ASKER

thanks . . . I just realized I need to pass 2 variables . . .
Can I make Global variables somewhere?
Shittash    .  . you read my mind . .
Do I use a regular module . . like I did in VB 6?
I just add a Module1.vb and put public variables in it?
yes you can do that....

theres nothing wrong with fernandosoto's way either.
What you're looking is the better way to do it, like FernandoSoto pointed out (I gave the quick and very dirty approach). From his code, just add another New that has two parameters and sets two variables.
thanks for the info . .
I'm not really that good with constructors, but I'll give it a try.
The other contructor has this:
Public Sub New()
      MyBase.New()
      'This call is required by the Windows Form Designer.
      InitializeComponent()  <<<<<<<<<<<<<<<<<<<
      'Add any initialization after the InitializeComponent() call
   End Sub

Do I need the    InitializeComponent()  for my new constructor?
Yes you do it initializes all of the controls on the form, without it your controls will not be show.
The best way to do it is call the original New like I did in the above code

    Public Sub New(ByVal prtName As String)

        ' First init the form and controls with the original constructor
        Me.New()  ' This constructor gets called first
        ' Then store the variable in the form
        _prtName = prtName

    End Sub
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It's hard working when you're really tired but I got it to work and would like
to thank everyone for their help.
What is the reason for using an underscore for the names?
Private _prtName As String
That is common usage for private members within a class (it is not accessible outside the class).
Some people use the uderscore before the variable name like _prtName to distinguish it as a Class level variable.
Of course there are numerous solutions. Newbies are often advised to generate a module where they can instantiate ALL of there forms with named instances. See http://www.devcity.net/Articles/100/multipleforms2.aspx and related pages (4 total to date) for an excellent discussion of this beginner's hurdle.

Yet for all his diligence, the author skipped over the total solution to one of the easiest methods that applies in the case of the original question of this thread:
If you are using the modal .ShowDialog method to display your second form, there is a built-in overload to the constructor that will send a pointer to second form with an instance of the original form. You can then back-reference to variables in the first form using the .Owner property

#Example:
'Form1 has a single button to modally open the second form
'Form2 has two textboxes, one to show the name of itself, the other to show the name of the owner

'Original form who's variables you need to reference
Public Class Form1
    Inherits System.Windows.Forms.Form

'#Region " Windows Form Designer generated code "
'This is unmodified from the designer provides

    Private Sub OpenButton1_Click(ByVal sender As System.Object, _
                                               ByVal e As System.EventArgs) Handles OpenButton1.Click
        Dim form2 As New Form2
        form2.ShowDialog(Me)
    End Sub
End Class

'*****************************

Public Class Form2
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "
'Again, unmodified

    Private Sub Form2_Load(ByVal sender As System.Object, _
                                        ByVal e As System.EventArgs) Handles MyBase.Load
        tbSelf.Text = Me.Name
        tbOwner.Text = Me.Owner.Name
    End Sub
End Class

#EndExample

There are also options if you are not using a modally displayed form via the ShowDialog method.
If you open form2 (of the above example) via Show(), you have two easy methods available other than that explained in the article mentioned above where you overload the New constructor for Form2. Both options involved the Owner property concept of Windows Forms:
1. Add the instantiated form2 to the OwnedForms array of form1 (actually unnamed* instantiation of Form1) via the AddOwnedForm method:
    Private Sub OpenButton1_Click(ByVal sender As System.Object, _
                                               ByVal e As System.EventArgs) Handles OpenButton1.Click
        Dim form2 As New Form2
        Me.AddOwnedForm(modalForm)
        form2.Show()
    End Sub

2. Add the original form as the Owner of form2 before calling its Show method. (There can be only one Owner for a form)
    Private Sub OpenButton1_Click(ByVal sender As System.Object, _
                                               ByVal e As System.EventArgs) Handles OpenButton1.Click
        Dim form2 As New Form2
        modalForm.Owner = Me
        form2.Show()
    End Sub

*The fact that you create a project with a Windows Form and that is the starting point of code execution is the source of confusion on this issue, coupled with the sometimes confusion difference between an instantiated Object versus its abstract Class definition. I am very conscious of case: when it comes to the above, "form2" is the instantiated Object of Class "Form2"; while the class Form1 has no instantiated variable name (although this is what I just called "form1"  :) - the closest you can get to referencing the instantiated object of Form1 is to use the keyword Me when in the scope of Form1 code.

One last note: I struggled with this concept partly because I lead myself down the wrong line of thinking by trying to use the MDI Child and Parent relationship. I of course learned that this is invalid, unless you use an MDI form... which takes a few other changes to invoke properly and is still not what most people actually want to accomplish (speaking generically)

Hope that helps straighten things out a bit. As always, create a test project to prove these concepts to yourself, and be sure to look at the MSDN Class definitions and the associated Members.

Now to get back to what I was doing...
Barnaby Arnott
www.computingimprovements.com