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 Data between Windows Forms

Passing Data between Windows Forms

Scheduled Pinned Locked Moved Visual Basic
winformshelpquestion
4 Posts 2 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.
  • R Offline
    R Offline
    Robert Gronenthal
    wrote on last edited by
    #1

    I am trying to pass data between text boxes and can only get it to happen one-way. I have formA and open formB. Both forms are open and have textboxes on both. I can create a command button on formA to get the text from a text box on formB, but when I create a command button on formB to push the text back to formA it error out with no instance of object. I dim frm as formA and get to see the properties and controls of formA. Using a statement like me.frm.textbox1.text = me.formB.textbox2.text Any suggestions?? Rob

    C 1 Reply Last reply
    0
    • R Robert Gronenthal

      I am trying to pass data between text boxes and can only get it to happen one-way. I have formA and open formB. Both forms are open and have textboxes on both. I can create a command button on formA to get the text from a text box on formB, but when I create a command button on formB to push the text back to formA it error out with no instance of object. I dim frm as formA and get to see the properties and controls of formA. Using a statement like me.frm.textbox1.text = me.formB.textbox2.text Any suggestions?? Rob

      C Offline
      C Offline
      Charlie Williams
      wrote on last edited by
      #2

      You need to give formB a way to reference formA. One way of going about this is to add a private field of type formA to formB and then pass the calling instance of formA to formB in its constructor: (In formA...) dim frmB as new formB(Me) (In formB...) Private frm as formA Public Sub New(ByVal frmRef as formA) frm = frmRef End Sub Then, in formB you can say frm.TextBox1.Text = "Howdy from formB!" As an aside, you may want to reconsider the way you are designing your app. It might be a better idea to add public properties to your forms and update the display with these properties than to allow direct access to the controls from other classes. This would make it a lot easier to alter your classes in the future. For example, as it stands now, if you decide that you want to use a combo box instead of a text box you will have to change the code in both forms instead of just the form that is implementing the change. Charlie if(!curlies){ return; }

      R 1 Reply Last reply
      0
      • C Charlie Williams

        You need to give formB a way to reference formA. One way of going about this is to add a private field of type formA to formB and then pass the calling instance of formA to formB in its constructor: (In formA...) dim frmB as new formB(Me) (In formB...) Private frm as formA Public Sub New(ByVal frmRef as formA) frm = frmRef End Sub Then, in formB you can say frm.TextBox1.Text = "Howdy from formB!" As an aside, you may want to reconsider the way you are designing your app. It might be a better idea to add public properties to your forms and update the display with these properties than to allow direct access to the controls from other classes. This would make it a lot easier to alter your classes in the future. For example, as it stands now, if you decide that you want to use a combo box instead of a text box you will have to change the code in both forms instead of just the form that is implementing the change. Charlie if(!curlies){ return; }

        R Offline
        R Offline
        Robert Gronenthal
        wrote on last edited by
        #3

        Thanks Charlie. I made the Dims Public and it works that much better. Bob

        C 1 Reply Last reply
        0
        • R Robert Gronenthal

          Thanks Charlie. I made the Dims Public and it works that much better. Bob

          C Offline
          C Offline
          Charlie Williams
          wrote on last edited by
          #4

          I'm glad you got it working, but unless this is a throw away app, you may end up kicking yourself later when you decide to change the layout of your UI. A little extra coding now to encapsulate your classes will save you a lot of coding later. However, if you expect never to touch it again once it's done, public fields will work fine. Charlie if(!curlies){ return; }

          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