How to: Carry a value from a variable from form to form?

I am attempting to carry a value of a variable from one form to another, but when I close or hide the original form and call the value in the subsequent form, the variable looses it's value. Using VB 2005 express. Please help!!

Answer this question

How to: Carry a value from a variable from form to form?

  • Hope Hope

    There are a few ways to do this but the easiest would be to use a Public Property. The following should help:

    http://msdn.microsoft.com/library/default.asp url=/library/en-us/dv_vstechart/html/vbtchWorkingWithMultipleFormsInVisualBasicNETUpgradingToNET.asp



  • danpoolshark

    DirtyHowi wrote:
    you can also pass it to the form load event and then set the local form2 variable to that value.

    ie above the fray, in form 2

    dim myvalue2 as string

    then in the form load(byval value as string)
    myvalue2=value

    I've been trying to figure out how to modify the class constructor in 2005 like i have done in 2002/3 and can't find the public sub new() call to the class constructor.
    You don't pass anything to the Load event. You can pass it to a constructor though. The default form constructor is not included in your code. You can simply write your own constructor though and the default one will not be used. If you create a constructor without arguments it will even add the call to InitializeComponent for you.

  • sobo

    HEY J,

    thanks, that kinda dawned on me last night around 10 when i was trying to figure out permissions requests for my software.

    still trying to figure out how to search the entire "c:\" for jpg files without it throwing an exception.



  • RavensAngel

    you can also pass it to the form load event and then set the local form2 variable to that value.

    ie above the fray, in form 2

    dim myvalue2 as string

    then in the form load(byval value as string)
    myvalue2=value

    I've been trying to figure out how to modify the class constructor in 2005 like i have done in 2002/3 and can't find the public sub new() call to the class constructor.


  • dv83d

    The following is one way of doing it in OO

    Option Strict On

    Public Class Form2

    '\\Private field to hold value passed from form1

    Private m_myInteger As Integer

    '\\CONSTRUCTOR

    '\\A constructor is a method that is executed each time a new instance of

    '\\the class is created.

    '\\The following constructor is passed the value from Form1 when the new

    '\\instance of the class is created in form1

    'Public Sub New(ByVal Myvalue As Integer)

    InitializeComponent()

    m_myInteger = Myvalue '\\Sets value of m_myInteger

    End Sub

    End Class

    Within Form1

    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Create an instance of the Form2 class setting the value to be passed to form2 as 2

    Dim myform As New Form2(2)

    Me.Hide()

    End Sub

    End Class

    Hope this helps

    Ron


  • pkrish2

    hi,

    and what pclement  said ,you can pass the value to the form b4 you hide your form through properties you put a property in your second form for example

    form2

    Public Class Form2

    Private f_Myvar As String

    Public Property P_Myvar()

    Get

    Return f_Myvar

    End Get

    Set(ByVal value)

    f_Myvar = value

    End Set

    End Property

     

    and in your form 1

    Dim form2 As New form2

    form2.P_Myvar = the value you want  to pass it

    form2.show()

     

    hope this helps



  • How to: Carry a value from a variable from form to form?