How to Pass Variable Values Between Forms?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dan Verssen
    New Member
    • Jul 2007
    • 9

    How to Pass Variable Values Between Forms?

    I have 2 forms that I need to go back and forth on a regular basis.

    I am using Hide/Show to only display one form at a time.

    If Form1 puts a value in a variable, then I Hide Form1 and Show Form2, how can I move the value to Form2 so Form2 can display and change it?

    Basically, how do I define a sub that runs each time its Form is Shown?
  • Clanguage
    New Member
    • Jun 2007
    • 12

    #2
    Code:
    Public Class Form1   
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim form As New Form2
            form.Show()
            Me.Label1.Text = form.Label1.Text
        End Sub
    End Class
    
    Public Class Form2
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim form As New Form1
            form.Show()
            Me.Label1.Text = form.Label1.Text
        End Sub
    End Class
    The forms keep the values of the variables, accessing them is therefore easy. Put 2 forms in a project and paste the code behind each form. You should be able to see what happens to the variables. Note that the forms are not destroyed so that you can create many instances of the forms.
    Good luck.

    Comment

    • Dan Verssen
      New Member
      • Jul 2007
      • 9

      #3
      Thank you! I will give this a try.

      Is there a way to pass the information without the need of a button click or other user action?

      For example, on Form1 the variable: username="Dan". When I Show Form2, how can I have a label's text show the value of the username variable ("Dan")?

      Comment

      • kadghar
        Recognized Expert Top Contributor
        • Apr 2007
        • 1302

        #4
        Originally posted by Dan Verssen
        Thank you! I will give this a try.

        Is there a way to pass the information without the need of a button click or other user action?

        For example, on Form1 the variable: username="Dan". When I Show Form2, how can I have a label's text show the value of the username variable ("Dan")?
        use a variable in a module and make a function to call and a sub to write.
        e.g.:

        [CODE=vb]'(in a module)
        option explicit
        dim Str1 as string

        sub WriteFn(wStr1 as string)
        str1 = wstr1
        end sub

        public function ReadFn() as string
        readfn = str1
        end function

        '(in Form1)
        sub command1_click( )
        call writefn(textbox 1.text)
        end sub

        '(in Form2)
        sub form2_load() 'or initialize, or watever
        label1.caption = readfn
        end sub[/CODE]

        HTH

        Comment

        • Dan Verssen
          New Member
          • Jul 2007
          • 9

          #5
          I like the idea of writing it to a file.

          From what I've seen, the Load contents are only executed the first time a Form is Shown. If I bounce back and forth between Forms, the Load contents do not get executed each time the Form is Shown.

          Is there a simple way to have a section of code run each time a Form is shown that does not involve a user action?

          Comment

          • kadghar
            Recognized Expert Top Contributor
            • Apr 2007
            • 1302

            #6
            Originally posted by Dan Verssen
            I like the idea of writing it to a file.

            From what I've seen, the Load contents are only executed the first time a Form is Shown. If I bounce back and forth between Forms, the Load contents do not get executed each time the Form is Shown.

            Is there a simple way to have a section of code run each time a Form is shown that does not involve a user action?
            use the Load, or the Initialize sub in the form, that actions are done when the sub is initialized or loaded. (it vary with the version of vb, but you should be able to find which sub fits you)

            Comment

            • Dan Verssen
              New Member
              • Jul 2007
              • 9

              #7
              The problem I've had with "Load" is it only gets performed the first time the Form is Shown. Each time I re-Show the Form, it isn't performed.

              Does Initialize get performed every time a Form is Shown?

              Comment

              • Dan Verssen
                New Member
                • Jul 2007
                • 9

                #8
                I'm a little confused. I would have thought that running code everytime a Form is Shown would be a casual everyday thing to do.

                Yet, everything involved with this seems to be a major hassle.

                Any ideas?

                Comment

                • Dan Verssen
                  New Member
                  • Jul 2007
                  • 9

                  #9
                  Got It!

                  I called a friend who is a programmer.

                  To get code to run each time a Form is Shown, put it inside its "Activate" event.

                  Many thanks to everyone who replied!

                  Comment

                  Working...