Passing variable between 2 forms

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • babaze
    New Member
    • Aug 2007
    • 2

    Passing variable between 2 forms

    I have problem .
    Ihave 2 forms , iwant to enter variables on form1 and show them in form2
    This what i have soo far
    form1
    =====
    Private Sub Button1_Click(v al blala)
    Fnamesender = TextBox2.Text
    Me.Hide()
    Form2.Show()

    End Sub

    form2
    =====
    Private Sub Form2_Load(ByVa l blabla bla)

    Label.text = Fnamesender
    end sub

    i have a mudule
    ===========
    Module one
    Public Fnamesender As String

    end module


    The Problem is when i enter a text in TextBox2 in form1 and view it in Label its ok , but if you go back to form1 and enter a name in textBox2 it doesnt change in label . how can i clear label
  • hariharanmca
    Top Contributor
    • Dec 2006
    • 1977

    #2
    Originally posted by babaze
    I have problem .
    Ihave 2 forms , iwant to enter variables on form1 and show them in form2
    This what i have soo far
    form1
    =====
    Private Sub Button1_Click(v al blala)
    Fnamesender = TextBox2.Text
    Me.Hide()
    Form2.Show()

    End Sub

    form2
    =====
    Private Sub Form2_Load(ByVa l blabla bla)

    Label.text = Fnamesender
    end sub

    i have a mudule
    ===========
    Module one
    Public Fnamesender As String

    end module


    The Problem is when i enter a text in TextBox2 in form1 and view it in Label its ok , but if you go back to form1 and enter a name in textBox2 it doesnt change in label . how can i clear label

    you never explain what is your problem and you can also use like this


    Form1
    ==========
    [CODE = vb]
    Private Sub Button1_Click(v al blala)
    Form2.Label.tex t = TextBox2.Text
    Me.Hide()
    Form2.Show()

    End Sub[/CODE]

    Comment

    • babaze
      New Member
      • Aug 2007
      • 2

      #3
      Form1
      ==========
      [CODE = vb]
      Private Sub Button1_Click(v al blala)
      Form2.Label.tex t = TextBox2.Text
      Me.Hide()
      Form2.Show()

      End Sub[/CODE]

      I still have the same problem even after using your code
      Here is my problem
      I have two forms form1 and form2
      I want to pass variable TextBox2.Text to Label.text in form2 after doing this
      i want to go back to form1 and ready to do the proccess again

      Problem
      Say i entered MIKE in TextBox2.Text in Form2.Label.tex t i get MIKE , thats good. but if i go back to form1 and enter JOHN in TextBox2.Text
      i still get MIKE in Form2.Label.tex t , Is there a way to clear Form2.Label.tex t
      before reciving the next variable i have tried all , Form2.Label.tex t="", and clear()
      i still cant get it to work . Thanks for your time

      Comment

      • hariharanmca
        Top Contributor
        • Dec 2006
        • 1977

        #4
        Originally posted by babaze
        Form1
        ==========
        [CODE = vb]
        Private Sub Button1_Click(v al blala)
        Form2.Label.tex t = TextBox2.Text
        Me.Hide()
        Form2.Show()

        End Sub[/CODE]

        I still have the same problem even after using your code
        Here is my problem
        I have two forms form1 and form2
        I want to pass variable TextBox2.Text to Label.text in form2 after doing this
        i want to go back to form1 and ready to do the proccess again

        Problem
        Say i entered MIKE in TextBox2.Text in Form2.Label.tex t i get MIKE , thats good. but if i go back to form1 and enter JOHN in TextBox2.Text
        i still get MIKE in Form2.Label.tex t , Is there a way to clear Form2.Label.tex t
        before reciving the next variable i have tried all , Form2.Label.tex t="", and clear()
        i still cant get it to work . Thanks for your time

        did you click the command button?

        Comment

        • hariharanmca
          Top Contributor
          • Dec 2006
          • 1977

          #5
          Originally posted by hariharanmca
          did you click the command button?

          Just throw what is the error that you are getting?

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            The textbox won't get updated by magic. If you put a value in it in the Form_Load event procedure, then this will only happen once when the form is loaded. If you want the value to change later, you have to change it.

            It's worth noting that hiding a form does not unload it. So unless you explicitly unload Form2 somewhere along the line, using Show on it will not run Form_Load again. It just makes it visible.

            Comment

            • pureenhanoi
              New Member
              • Mar 2007
              • 175

              #7
              Originally posted by babaze
              I have problem .
              Ihave 2 forms , iwant to enter variables on form1 and show them in form2
              This what i have soo far
              form1
              =====
              Private Sub Button1_Click(v al blala)
              Fnamesender = TextBox2.Text
              Me.Hide()
              Form2.Show()

              End Sub

              form2
              =====
              Private Sub Form2_Load(ByVa l blabla bla)

              Label.text = Fnamesender
              end sub

              i have a mudule
              ===========
              Module one
              Public Fnamesender As String

              end module


              The Problem is when i enter a text in TextBox2 in form1 and view it in Label its ok , but if you go back to form1 and enter a name in textBox2 it doesnt change in label . how can i clear label
              [CODE=asp]
              Private Sub Command1_Click( )
              Dim frm2 As Form2
              FNameSender = Text2.Text
              Me.Hide
              Load frm2
              frm2.Show
              End Sub
              [/CODE]
              or
              [CODE=asp]
              Private Sub Command1_Click( )
              Dim frm2 As Form2
              FNameSender = Text2.Text
              Set frm2 = New Form2
              Me.Hide
              frm2.Show
              End Sub
              [/CODE]

              Comment

              • Killer42
                Recognized Expert Expert
                • Oct 2006
                • 8429

                #8
                I don't know about ASP, but in VB6, the Load Form2 will have no effect if Form2 is already loaded.

                Comment

                • pureenhanoi
                  New Member
                  • Mar 2007
                  • 175

                  #9
                  Originally posted by Killer42
                  I don't know about ASP, but in VB6, the Load Form2 will have no effect if Form2 is already loaded.
                  My code was Quoted by ASP tag, That dont mean my code was written by ASP (i like the color of ASP-tag only).
                  If you take a look again, you can see that i didnt call "Load Form2". My code was "Load frm2". Here, "frm2" is an entitiy of class Form2. (i must say sorry about the first declare. It must be: Dim frm2 As New Form2)

                  Comment

                  • Killer42
                    Recognized Expert Expert
                    • Oct 2006
                    • 8429

                    #10
                    Originally posted by pureenhanoi
                    My code was Quoted by ASP tag, That dont mean my code was written by ASP (i like the color of ASP-tag only).
                    It certainly makes the keywords stand out.

                    Originally posted by pureenhanoi
                    If you take a look again, you can see that i didnt call "Load Form2". My code was "Load frm2". Here, "frm2" is an entitiy of class Form2. (i must say sorry about the first declare. It must be: Dim frm2 As New Form2)
                    Good point.

                    However, this seems like a lot of unnecessary complexity just to show a value on the form when something is clicked. Surely the code should just set the appropriate control on Form2.

                    Comment

                    • pureenhanoi
                      New Member
                      • Mar 2007
                      • 175

                      #11
                      Originally posted by Killer42
                      It certainly makes the keywords stand out.

                      Good point.

                      However, this seems like a lot of unnecessary complexity just to show a value on the form when something is clicked. Surely the code should just set the appropriate control on Form2.
                      I dont think it bring up complexity in my code. If you approach programing from Object Oriented, you will see thats the common thing. (if you've ever seen some code was written by elder version of VB (4.0 or lest than) , you can see much code like me. Because, in elder version of Vb, programer cannot call Form2.Show directly. Form2 is a class. Cannot calll <ClassName>.<Me thodName>.

                      Comment

                      Working...