Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. passing variables between windows forms

passing variables between windows forms

Scheduled Pinned Locked Moved Visual Basic
questionwinformstutorial
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mpiotro
    wrote on last edited by
    #1

    How can I do this? For example: let's say on form1.vb, i have the following code: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click product_code = TextBox1.Text.Substring(0, 4) 'switch over to next window Dim form2 As New form2 form2.Show() Endsub Now, how can I get the value in product_code in Form2.vb ?

    P J 2 Replies Last reply
    0
    • M mpiotro

      How can I do this? For example: let's say on form1.vb, i have the following code: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click product_code = TextBox1.Text.Substring(0, 4) 'switch over to next window Dim form2 As New form2 form2.Show() Endsub Now, how can I get the value in product_code in Form2.vb ?

      P Offline
      P Offline
      Pablo ar
      wrote on last edited by
      #2

      I was told by the mods that this forum is primarly .Net intended, and so I feel compelled to think that you are trying to do this with it, but just in case this hasn't changed since VB6 or you are indeed doing it in VB6, this is (or should I say was) the way to do it: Declare the variable product_code on a separate .bas 'Module' object, with the Public word, insted of the good ol' 'Dim': Public product_code As String in this way you can access product_code from whereever in your project (different Subs, on different Forms, Modules, Class Modules, whatever!) Hope that helps (and that you ARE doing it in VB6!) Cheers, Pablo.ar

      M 1 Reply Last reply
      0
      • P Pablo ar

        I was told by the mods that this forum is primarly .Net intended, and so I feel compelled to think that you are trying to do this with it, but just in case this hasn't changed since VB6 or you are indeed doing it in VB6, this is (or should I say was) the way to do it: Declare the variable product_code on a separate .bas 'Module' object, with the Public word, insted of the good ol' 'Dim': Public product_code As String in this way you can access product_code from whereever in your project (different Subs, on different Forms, Modules, Class Modules, whatever!) Hope that helps (and that you ARE doing it in VB6!) Cheers, Pablo.ar

        M Offline
        M Offline
        mpiotro
        wrote on last edited by
        #3

        Pablo, Sorry, I should have clarified. Yes, I am attempting this in vb.net. I am just starting in programming in .net, with no experience in vb6. I did try declaring as Public, but with no luck. I'm sure there's a way of declaring variables globally so that this is possible. By the way, I'm not sure what a .bas module object is. Is this vb6 terminology. Thanks again for your help, Mike

        D 1 Reply Last reply
        0
        • M mpiotro

          How can I do this? For example: let's say on form1.vb, i have the following code: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click product_code = TextBox1.Text.Substring(0, 4) 'switch over to next window Dim form2 As New form2 form2.Show() Endsub Now, how can I get the value in product_code in Form2.vb ?

          J Offline
          J Offline
          Jim Matthews
          wrote on last edited by
          #4

          hi, looks to me like you're using .net based on the "handles" keyword in your method signature as well as a few other things, so i'll suggest another way for you. (no offense intended pablo! :) ) the best way to do this would be to set up a friend property on form1 one that gives access to a module level variable in form1. you'll also need a reference to the actual form1 object in form2. some thing like this...

          public class form1
          inherits system.windows.forms.form

          'initialization code

          private m_productCode as String

          friend property ProductCode() as String
          Get
          return me.m_productCode
          End Get
          Set(byval value as string)
          me.m_productcode = value
          End Set
          end property

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

          me.ProductCode = textbox1.text.substring(0, 4)

          dim frmTwo as new form2
          frmTwo.FormOne = me
          frmTwo.Show

          end sub

          end class

          *'--- in a separate code file ---'*

          class form2
          Inherits System.Windows.Forms.Form

          private m_formOne as Windows.Forms.Form

          Friend Property Form1 as Windows.Forms.Form
          Get
          return me.m_formOne
          End Get
          Set(byval value as Windows.Forms.Form)
          me.m_formOne = value
          End Set
          End Property

          Private Sub form2_Load(byval sender as object, byval e as system.eventargs) handles form2.Load

          '--- this line gives you access to the product code from form1 ---'
          dim productCode as string = me.FormOne.ProductCode

          End Sub

          end class

          the freind property in form2 gives form1 access to safely set a field in form2, and vice versa for the friend property in form1. this code isn't tested and wasn't typed into my editor, so beware of syntaxtual errors. just a warning... hope this helps


          -jim

          P 1 Reply Last reply
          0
          • M mpiotro

            Pablo, Sorry, I should have clarified. Yes, I am attempting this in vb.net. I am just starting in programming in .net, with no experience in vb6. I did try declaring as Public, but with no luck. I'm sure there's a way of declaring variables globally so that this is possible. By the way, I'm not sure what a .bas module object is. Is this vb6 terminology. Thanks again for your help, Mike

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            There's no such thing as a "global" variable in the .NET Framework. This goes for every language that targets .NET, not just VB. The best way to do it is not to share a variable in the form, but to emulate the method Pablo mentioned. For an example of how to do this, check out Working with Multiple Forms in Visual Basic .NET: Upgrading to .NET[^] on MSDN. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

            1 Reply Last reply
            0
            • J Jim Matthews

              hi, looks to me like you're using .net based on the "handles" keyword in your method signature as well as a few other things, so i'll suggest another way for you. (no offense intended pablo! :) ) the best way to do this would be to set up a friend property on form1 one that gives access to a module level variable in form1. you'll also need a reference to the actual form1 object in form2. some thing like this...

              public class form1
              inherits system.windows.forms.form

              'initialization code

              private m_productCode as String

              friend property ProductCode() as String
              Get
              return me.m_productCode
              End Get
              Set(byval value as string)
              me.m_productcode = value
              End Set
              end property

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

              me.ProductCode = textbox1.text.substring(0, 4)

              dim frmTwo as new form2
              frmTwo.FormOne = me
              frmTwo.Show

              end sub

              end class

              *'--- in a separate code file ---'*

              class form2
              Inherits System.Windows.Forms.Form

              private m_formOne as Windows.Forms.Form

              Friend Property Form1 as Windows.Forms.Form
              Get
              return me.m_formOne
              End Get
              Set(byval value as Windows.Forms.Form)
              me.m_formOne = value
              End Set
              End Property

              Private Sub form2_Load(byval sender as object, byval e as system.eventargs) handles form2.Load

              '--- this line gives you access to the product code from form1 ---'
              dim productCode as string = me.FormOne.ProductCode

              End Sub

              end class

              the freind property in form2 gives form1 access to safely set a field in form2, and vice versa for the friend property in form1. this code isn't tested and wasn't typed into my editor, so beware of syntaxtual errors. just a warning... hope this helps


              -jim

              P Offline
              P Offline
              Pablo ar
              wrote on last edited by
              #6

              yeah, I guess I should've noticed that .Substring thingy and all the args on the event... it passed me by, sorry. One more idea... do forms still have that .tag property? That was an amazingly useful property, which I worked with many many times! Pablo.ar PS: I feel old, hehe.

              J 1 Reply Last reply
              0
              • P Pablo ar

                yeah, I guess I should've noticed that .Substring thingy and all the args on the event... it passed me by, sorry. One more idea... do forms still have that .tag property? That was an amazingly useful property, which I worked with many many times! Pablo.ar PS: I feel old, hehe.

                J Offline
                J Offline
                Jim Matthews
                wrote on last edited by
                #7

                sure do. :)


                -jim

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups