Passing a value from form to form

What is the best way to pass a value from form to form Do I use a global variable or is there a parameter style way to do it Is there some other way

Thanks for helping a newbie.




Answer this question

Passing a value from form to form

  • Dave Foderick

    Just what I need. Thanks for the help.

  • Jared B

    Heres some code passing a couple of values as an array list from form1 to form2.

    Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim y As New ArrayList

    y.Add("xxxx")
    y.Add("zzzzz")

    Dim frm As New Form2
    frm.foo = y
    frm.Show()

    End Sub
    End Class

    Public Class Form2
    Dim x As ArrayList

    Public Property foo() As ArrayList
    Get
    Return x
    End Get
    Set(ByVal value As ArrayList)
    x = value
    End Set
    End Property

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    MsgBox(x.Count)
    End Sub
    End Class


  • Christian Kleinerman - MS

    Hmm...tell a little more about your problem. Do you close the first form (the one that contains the information) or do you have two forms and just want to share information between them


  • Slaru

    If you use the properties to pass the variables around or use an overloaded constructor to initialize the object upon creation they are both valid approaches.

    You may overload the Constructor and use this to set the properties. This provides you with a simple method to create and initialize on the same line by using the overloaded constructor.

    A new feature coming in next version of VB (codename Orcas) is called Aggregate Initializers and will enable you to create objects and set properties on same line.

    But either method is valid.


  • DanWoerner

    I have a simple read-only program in VB 2005 that will be retrieving info from several different SQL Server views based on a single case number.  It will have 3 to 4 forms and each will be closing when the next or previous one is opened.

    The way I am setting it up now, each form will get info form its own view.  Each view pulls from a different table or tables with the case number being the common field in all of the tables.  I want to pass the case number from form to form.



  • zbc

    Sure you could do it as a collection,array, List or individual properties - it really depends upon the individual scenario.


  • Designs

    Thanks for all of your help, spotty. I can finally make some progress on my app!



  • mfarace

    How would I pass several values as a collection/array

  • RonO2006

    Exactly what i would have suggested you. I don't think there is a much better way... :)

    If you have lot of values to pass, think about passing one big collection / array instead of creating a lot of different properties...but i think you would have done.


  • Jonleeofbj

    Create a Property on the second form and then when you create an instance of the second form from the first form - set this property.   In this case it takes a value from the Textbox1 text property and passes it to the instance of form2 when it creates it and uses this to set the caption.

    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim Instanceform2 As New Form2
            Instanceform2.XValue = textbox1.text
            Instanceform2.Show()
        End Sub
    End Class


    Public Class Form2
        Private x As Integer


        Public Property XValue() As Integer
            Get
                Return x
            End Get
            Set(ByVal value As Integer)
                x = value
            End Set
        End Property

        Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
          Me.Text = x.ToString
        End Sub


    End Class

     

    So in each of your forms you will have a CaseNo property which you set when you create an instance of the form.   The forms can then have pretty self contained functionality.

     


     


  • Vladimir Nikitin

    I need to pass 4 values. I tried passing an array where the variables were arrays but I couldn't get the syntax right. In this scenario, what would be the best way and what is the correct syntax

    Thanks.



  • Dhivager Rathakrishnan

    Wouldn't the OO way be to create a new class object that has getters/setters for the variables stored within it and pass it around from form to form


  • slippery

    Another manner of passing values from Form1 to Form2 is to Overload the New() Constructor of Form2. This Creates Form2 that accepts/requires Parameters from Form1.

    In Form2 you would place similar code in the New() Constructor in the "Windows Form Designer generated code".

    Form2:

    Public Sub New(ByVal frmCurrentCallingForm As Form, ByVal strCurrentSQLStatement As String, ByVal strCurrentTableName As String)

    MyBase.New()

    'This call is required by the Windows Form Designer.

    InitializeComponent()

    'Add any initialization after the InitializeComponent() call

    frmCallingForm = frmCurrentCallingForm

    strSQLStatement = strCurrentSQLStatement

    strTableName = strCurrentTableName

    End Sub

    In Form1 you would call Form2 with the following code:

    'Pass the Required Parameters to Form2.

    Dim frmNewForm2 As New frmForm2(Me, strSQLProcedure, strTableName)

    'Show Form2.

    frmNewForm2.Show


  • Passing a value from form to form