1 Variable - 2 Forms

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Miro

    #1

    1 Variable - 2 Forms

    I am banging my head around something that I think I just dont understand
    what I am reading in the helps.
    Its gotta be so simple but I just cant figure out what im reading. ( vb
    2003 )

    Lets say i have Form1 and Form2.

    Each form is blank. ( no text boxes or anything )
    Very Simple Example: ( i just need a start and then i can modify it and
    learn from it to do what i need to do )

    On form1_Load
    'I have a variable called cHello
    Dim cHello As String = "Hello World"

    ' I call form two.
    Dim frmSecondForm As Form = New Form2()
    frmSecondForm .ShowDialog()

    EndSub

    On form 2's load, I want to
    MsgBox( cHello ,MsgBoxStyle.OK Only, "Title Of Box")
    close()
    EndSub


    How do I Properly pass the variable to form2 ?


    Thanks,

    Miro






  • sweet_dreams

    #2
    Re: 1 Variable - 2 Forms


    Miro napisal(a):
    I am banging my head around something that I think I just dont understand
    what I am reading in the helps.
    Its gotta be so simple but I just cant figure out what im reading. ( vb
    2003 )
    >
    Lets say i have Form1 and Form2.
    >
    Each form is blank. ( no text boxes or anything )
    Very Simple Example: ( i just need a start and then i can modify it and
    learn from it to do what i need to do )
    >
    On form1_Load
    'I have a variable called cHello
    Dim cHello As String = "Hello World"
    >
    ' I call form two.
    Dim frmSecondForm As Form = New Form2()
    frmSecondForm .ShowDialog()
    >
    EndSub
    >
    On form 2's load, I want to
    MsgBox( cHello ,MsgBoxStyle.OK Only, "Title Of Box")
    close()
    EndSub
    >
    >
    How do I Properly pass the variable to form2 ?
    >
    >
    Thanks,
    >
    Miro
    Hi Miro,

    If you want to pass variable between multiple form you have to declare
    it as Public:

    'form1

    Public _text As String

    Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
    System.EventArg s) Handles MyBase.Load
    _text = "Hello"

    Dim f2 As New Form2
    Form2.Show()

    End Sub

    'form2

    Private Sub Form2_Load(ByVa l sender As System.Object, ByVal e As
    System.EventArg s) Handles MyBase.Load
    Dim TextFromForm1 As String

    TextFromForm1 = Form1._text

    MessageBox.Show (TextFromForm1)

    End Sub

    While working with vb.net you have to be aware that everything is
    object here. So if want access objects property, method... you have to
    specify this object like it was done here:
    Form1._text

    If you have problems working with multiple forms read this articles:
    PGBET APK 2.5.0 hadir dengan slot Android terlengkap: unduh gratis 47 MB, panel RTP real-time, QRIS kilat, ratusan game provider resmi.

    PGBET APK 2.5.0 hadir dengan slot Android terlengkap: unduh gratis 47 MB, panel RTP real-time, QRIS kilat, ratusan game provider resmi.

    PGBET APK 2.5.0 hadir dengan slot Android terlengkap: unduh gratis 47 MB, panel RTP real-time, QRIS kilat, ratusan game provider resmi.

    PGBET APK 2.5.0 hadir dengan slot Android terlengkap: unduh gratis 47 MB, panel RTP real-time, QRIS kilat, ratusan game provider resmi.


    And by the way, msgbox() is clasic vb function. In vb.net it is
    recommended to use:
    messagebox.show (text, caption, buttons, icon)

    Hope this helps.

    Regardsm
    sweet_dreams

    Comment

    • ag

      #3
      Re: 1 Variable - 2 Forms

      pass it on Form2 thru constructor parameter wen calling form2

      pseudo code:

      dim myString as string="hello world"

      dim f2 as new form2(myString)

      ----- now in form 2

      dim myvar as string

      sub new (byval x as string)
      myvar=x
      end sub

      now on load event put the msgbox n display myvar

      Comment

      • iwdu15

        #4
        Re: 1 Variable - 2 Forms

        you could also make one variable Public so everything can see it, (VB 2005) ex:

        Public str As String

        Private Sub Form1_Load(..)

        Dim frm As New Form2

        str = "Hello"

        frm.Show()

        End Sub

        '''form 2
        Private Sub Form2_Load(...)

        me.text = My.Forms.Form1. str

        end Sub


        ''''''''''''''' '''''''''

        or you could make the variable reflect a property on form1

        just my 2 cents, hope this helps
        --
        -iwdu15

        Comment

        • Miro

          #5
          Re: 1 Variable - 2 Forms

          Thank you all,

          I was soo close.
          Maybe I just needed some sleep. :)

          thanks agian,

          Miro

          "iwdu15" <jmmgoalsteraty ahoodotcomwrote in message
          news:B5ED5BC0-CF88-4629-B1DE-58720353110A@mi crosoft.com...
          you could also make one variable Public so everything can see it, (VB
          2005) ex:
          >
          Public str As String
          >
          Private Sub Form1_Load(..)
          >
          Dim frm As New Form2
          >
          str = "Hello"
          >
          frm.Show()
          >
          End Sub
          >
          '''form 2
          Private Sub Form2_Load(...)
          >
          me.text = My.Forms.Form1. str
          >
          end Sub
          >
          >
          ''''''''''''''' '''''''''
          >
          or you could make the variable reflect a property on form1
          >
          just my 2 cents, hope this helps
          --
          -iwdu15

          Comment

          • Miro

            #6
            Re: 1 Variable - 2 Forms - Needs Shared ?

            I tried your solution,

            It didnt work,
            I had my variable declared as Public myText As String

            but it didnt recognize it on the second form until I put

            Public Shared myText as String

            Is this a vb 2005 to vb 2003 difference or did you just miss that in the
            example ?


            Thank you for your example though... the links on the bottom, I actually
            read the night before,
            but in my case I was always missing the "shared" property.

            Thanks again,

            Miro


            "sweet_drea ms" <sweet_dreams@o 2.plwrote in message
            news:1154591717 .059673.46320@7 5g2000cwc.googl egroups.com...
            >
            Miro napisal(a):
            >I am banging my head around something that I think I just dont understand
            >what I am reading in the helps.
            >Its gotta be so simple but I just cant figure out what im reading. ( vb
            >2003 )
            >>
            >Lets say i have Form1 and Form2.
            >>
            >Each form is blank. ( no text boxes or anything )
            >Very Simple Example: ( i just need a start and then i can modify it and
            >learn from it to do what i need to do )
            >>
            >On form1_Load
            > 'I have a variable called cHello
            > Dim cHello As String = "Hello World"
            >>
            >' I call form two.
            > Dim frmSecondForm As Form = New Form2()
            > frmSecondForm .ShowDialog()
            >>
            >EndSub
            >>
            >On form 2's load, I want to
            >MsgBox( cHello ,MsgBoxStyle.OK Only, "Title Of Box")
            >close()
            >EndSub
            >>
            >>
            >How do I Properly pass the variable to form2 ?
            >>
            >>
            >Thanks,
            >>
            >Miro
            >
            Hi Miro,
            >
            If you want to pass variable between multiple form you have to declare
            it as Public:
            >
            'form1
            >
            Public _text As String
            >
            Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
            System.EventArg s) Handles MyBase.Load
            _text = "Hello"
            >
            Dim f2 As New Form2
            Form2.Show()
            >
            End Sub
            >
            'form2
            >
            Private Sub Form2_Load(ByVa l sender As System.Object, ByVal e As
            System.EventArg s) Handles MyBase.Load
            Dim TextFromForm1 As String
            >
            TextFromForm1 = Form1._text
            >
            MessageBox.Show (TextFromForm1)
            >
            End Sub
            >
            While working with vb.net you have to be aware that everything is
            object here. So if want access objects property, method... you have to
            specify this object like it was done here:
            Form1._text
            >
            If you have problems working with multiple forms read this articles:
            PGBET APK 2.5.0 hadir dengan slot Android terlengkap: unduh gratis 47 MB, panel RTP real-time, QRIS kilat, ratusan game provider resmi.

            PGBET APK 2.5.0 hadir dengan slot Android terlengkap: unduh gratis 47 MB, panel RTP real-time, QRIS kilat, ratusan game provider resmi.

            PGBET APK 2.5.0 hadir dengan slot Android terlengkap: unduh gratis 47 MB, panel RTP real-time, QRIS kilat, ratusan game provider resmi.

            PGBET APK 2.5.0 hadir dengan slot Android terlengkap: unduh gratis 47 MB, panel RTP real-time, QRIS kilat, ratusan game provider resmi.

            >
            And by the way, msgbox() is clasic vb function. In vb.net it is
            recommended to use:
            messagebox.show (text, caption, buttons, icon)
            >
            Hope this helps.
            >
            Regardsm
            sweet_dreams
            >

            Comment

            • sweet_dreams

              #7
              Re: 1 Variable - 2 Forms - Needs Shared ?


              Miro napisal(a):
              I tried your solution,
              >
              It didnt work,
              I had my variable declared as Public myText As String
              >
              but it didnt recognize it on the second form until I put
              >
              Public Shared myText as String
              >
              Is this a vb 2005 to vb 2003 difference or did you just miss that in the
              example ?
              >
              >
              Thank you for your example though... the links on the bottom, I actually
              read the night before,
              but in my case I was always missing the "shared" property.
              >
              Thanks again,
              >
              Miro

              Hi Miro,

              I'm using VB 2005 and it works without Shared. But using Shared is also
              good. So use Shared.

              Regards,
              sweet_dreams

              Comment

              • Miro

                #8
                Re: 1 Variable - 2 Forms - Needs Shared ?

                Im 2003 and It does not recognize the formname.value if the "Shared"
                property is not there.

                So that looks like it is a difference in 2k3 and 2k5 version.

                Thanks Sweet_Dreams
                ps. "napisal(a) " is that Czech or Slovak ? - "Miro Wrote" :)

                thanks agian.


                "sweet_drea ms" <sweet_dreams@o 2.plwrote in message
                news:1154619273 .386069.291120@ 75g2000cwc.goog legroups.com...
                >
                Miro napisal(a):
                >I tried your solution,
                >>
                >It didnt work,
                >I had my variable declared as Public myText As String
                >>
                >but it didnt recognize it on the second form until I put
                >>
                >Public Shared myText as String
                >>
                >Is this a vb 2005 to vb 2003 difference or did you just miss that in the
                >example ?
                >>
                >>
                >Thank you for your example though... the links on the bottom, I actually
                >read the night before,
                >but in my case I was always missing the "shared" property.
                >>
                >Thanks again,
                >>
                >Miro
                >
                >
                Hi Miro,
                >
                I'm using VB 2005 and it works without Shared. But using Shared is also
                good. So use Shared.
                >
                Regards,
                sweet_dreams
                >

                Comment

                • sweet_dreams

                  #9
                  Re: 1 Variable - 2 Forms - Needs Shared ?


                  Miro napisal(a):
                  Im 2003 and It does not recognize the formname.value if the "Shared"
                  property is not there.
                  >
                  So that looks like it is a difference in 2k3 and 2k5 version.
                  >
                  Thanks Sweet_Dreams
                  ps. "napisal(a) " is that Czech or Slovak ? - "Miro Wrote" :)
                  None of them. It's Polish :)

                  Comment

                  • GhostInAK

                    #10
                    Re: 1 Variable - 2 Forms

                    Hello Miro,

                    My god, it's like the blind leading the blind.. Lets set things straight.

                    First, The new form instantiation shortcuts in 05 suck ass. M$: Take this
                    crap OUT.
                    M$ made it possible to reference a "default instance" of a form without explicitly
                    instantiating it, in 2005. This is more VB6 style than .NET, and it is misleading
                    and confusing to newbies. TAKE IT OUT.

                    So.. While you can simply do Form2.Show, and Form1.SomeField in 05, in 03
                    you must have explicit instances of Form1 and Form2 (you do in 05 also, but
                    the language hides the details from you).

                    This is also why Miro noticed that adding the Shared keyword to the field
                    made things work. Ya'll need to read up on variable scope.

                    Proper way to do this:
                    in Form2.vb: Add a private field (sSomeText), and a public property (SomeText)
                    in Form1.vb: Add to the Load event handler:
                    Dim tForm as Form2 = New Form2
                    tForm.SomeText = "blah blah"
                    tForm.Show

                    in Form2.vb: Add to Load event handler:
                    msgbox(me.SomeT ext)


                    Also, as stated earlier, you could overload the ctor for Form2 and pass in
                    the text there.

                    Enjoy,
                    -Boo
                    I am banging my head around something that I think I just dont
                    understand
                    what I am reading in the helps.
                    Its gotta be so simple but I just cant figure out what im reading. (
                    vb
                    2003 )
                    Lets say i have Form1 and Form2.
                    >
                    Each form is blank. ( no text boxes or anything )
                    Very Simple Example: ( i just need a start and then i can modify it
                    and
                    learn from it to do what i need to do )
                    On form1_Load
                    'I have a variable called cHello
                    Dim cHello As String = "Hello World"
                    ' I call form two.
                    Dim frmSecondForm As Form = New Form2()
                    frmSecondForm .ShowDialog()
                    EndSub
                    >
                    On form 2's load, I want to
                    MsgBox( cHello ,MsgBoxStyle.OK Only, "Title Of Box")
                    close()
                    EndSub
                    How do I Properly pass the variable to form2 ?
                    >
                    Thanks,
                    >
                    Miro
                    >

                    Comment

                    • Miro

                      #11
                      Re: 1 Variable - 2 Forms

                      The problem wasnt with a text box
                      - If i understand your example.

                      The proplem is using a straight variable that I run some calculations on and
                      have random outcom and then pass it to another form.
                      Everywhere on the net they use a textbox.text example...

                      nowhere do they have a Dim myVariable as String = "X" and now pass that
                      between forms.

                      M.

                      "GhostInAK" <ghostinak@gmai l.comwrote in message
                      news:c71747b42b 0688c8856ab6d26 c6d@news.micros oft.com...
                      Hello Miro,
                      >
                      My god, it's like the blind leading the blind.. Lets set things straight.
                      >
                      First, The new form instantiation shortcuts in 05 suck ass. M$: Take
                      this crap OUT.
                      M$ made it possible to reference a "default instance" of a form without
                      explicitly instantiating it, in 2005. This is more VB6 style than .NET,
                      and it is misleading and confusing to newbies. TAKE IT OUT.
                      >
                      So.. While you can simply do Form2.Show, and Form1.SomeField in 05, in 03
                      you must have explicit instances of Form1 and Form2 (you do in 05 also,
                      but the language hides the details from you).
                      >
                      This is also why Miro noticed that adding the Shared keyword to the field
                      made things work. Ya'll need to read up on variable scope.
                      >
                      Proper way to do this:
                      in Form2.vb: Add a private field (sSomeText), and a public property
                      (SomeText)
                      in Form1.vb: Add to the Load event handler: Dim tForm as Form2 = New
                      Form2
                      tForm.SomeText = "blah blah"
                      tForm.Show
                      >
                      in Form2.vb: Add to Load event handler:
                      msgbox(me.SomeT ext)
                      >
                      >
                      Also, as stated earlier, you could overload the ctor for Form2 and pass in
                      the text there.
                      >
                      Enjoy,
                      -Boo
                      >
                      >I am banging my head around something that I think I just dont
                      >understand
                      >what I am reading in the helps.
                      >Its gotta be so simple but I just cant figure out what im reading. (
                      >vb
                      >2003 )
                      >Lets say i have Form1 and Form2.
                      >>
                      >Each form is blank. ( no text boxes or anything )
                      >Very Simple Example: ( i just need a start and then i can modify it
                      >and
                      >learn from it to do what i need to do )
                      >On form1_Load
                      >'I have a variable called cHello
                      >Dim cHello As String = "Hello World"
                      >' I call form two.
                      >Dim frmSecondForm As Form = New Form2()
                      >frmSecondFor m .ShowDialog()
                      >EndSub
                      >>
                      >On form 2's load, I want to
                      >MsgBox( cHello ,MsgBoxStyle.OK Only, "Title Of Box")
                      >close()
                      >EndSub
                      >How do I Properly pass the variable to form2 ?
                      >>
                      >Thanks,
                      >>
                      >Miro
                      >>
                      >
                      >

                      Comment

                      • GhostInAK

                        #12
                        Re: 1 Variable - 2 Forms

                        Hello Miro,

                        Variable, Property.. whatever.. it doesnt matter..
                        How about this example:

                        on form2 create a new sub:
                        Public Sub MyGodItsHideous (byval tCreatureName as string)
                        msgbox("When " & tCreatureName & " was a child we had to hang a chunk of
                        meat around its neck just to get the dog to play with it!")
                        End Sub


                        on Form1:

                        Dim tForm as Form2 = New Form2
                        tForm.MyGodItsH ideous("Jennife r Lopez")


                        The point is, a form is just like any other class.

                        -Boo
                        The problem wasnt with a text box
                        - If i understand your example.
                        The proplem is using a straight variable that I run some calculations
                        on and
                        have random outcom and then pass it to another form.
                        Everywhere on the net they use a textbox.text example...
                        nowhere do they have a Dim myVariable as String = "X" and now pass
                        that between forms.
                        >
                        M.
                        >
                        "GhostInAK" <ghostinak@gmai l.comwrote in message
                        news:c71747b42b 0688c8856ab6d26 c6d@news.micros oft.com...
                        >
                        >Hello Miro,
                        >>
                        >My god, it's like the blind leading the blind.. Lets set things
                        >straight.
                        >>
                        >First, The new form instantiation shortcuts in 05 suck ass. M$:
                        >Take
                        >this crap OUT.
                        >M$ made it possible to reference a "default instance" of a form
                        >without
                        >explicitly instantiating it, in 2005. This is more VB6 style than
                        >.NET,
                        >and it is misleading and confusing to newbies. TAKE IT OUT.
                        >So.. While you can simply do Form2.Show, and Form1.SomeField in 05,
                        >in 03 you must have explicit instances of Form1 and Form2 (you do in
                        >05 also, but the language hides the details from you).
                        >>
                        >This is also why Miro noticed that adding the Shared keyword to the
                        >field made things work. Ya'll need to read up on variable scope.
                        >>
                        >Proper way to do this:
                        >in Form2.vb: Add a private field (sSomeText), and a public property
                        >(SomeText)
                        >in Form1.vb: Add to the Load event handler: Dim tForm as Form2 =
                        >New
                        >Form2
                        >tForm.SomeTe xt = "blah blah"
                        >tForm.Show
                        >in Form2.vb: Add to Load event handler:
                        >msgbox(me.Some Text)
                        >Also, as stated earlier, you could overload the ctor for Form2 and
                        >pass in the text there.
                        >>
                        >Enjoy,
                        >-Boo
                        >>I am banging my head around something that I think I just dont
                        >>understand
                        >>what I am reading in the helps.
                        >>Its gotta be so simple but I just cant figure out what im reading.
                        >>(
                        >>vb
                        >>2003 )
                        >>Lets say i have Form1 and Form2.
                        >>Each form is blank. ( no text boxes or anything )
                        >>Very Simple Example: ( i just need a start and then i can modify it
                        >>and
                        >>learn from it to do what i need to do )
                        >>On form1_Load
                        >>'I have a variable called cHello
                        >>Dim cHello As String = "Hello World"
                        >>' I call form two.
                        >>Dim frmSecondForm As Form = New Form2()
                        >>frmSecondFo rm .ShowDialog()
                        >>EndSub
                        >>On form 2's load, I want to
                        >>MsgBox( cHello ,MsgBoxStyle.OK Only, "Title Of Box")
                        >>close()
                        >>EndSub
                        >>How do I Properly pass the variable to form2 ?
                        >>Thanks,
                        >>>
                        >>Miro
                        >>>

                        Comment

                        Working...