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 variable between parent/child forms

Passing variable between parent/child forms

Scheduled Pinned Locked Moved Visual Basic
helpcsharp
5 Posts 2 Posters 1 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
    MadCow
    wrote on last edited by
    #1

    ok, I'm completely new to VB and have come up with a problem on something that I think it really easy. What I have are 2 forms, form1 and form2. Form2 gets called from a context popup on a listview on form1. Now, on form2 I have a textbox variable that I want transfered back to the listview on form1 when you click on the OK button on form2. However I am unable to access the listview on form1 from form2. I figure it's a scope issue. I have found a work around on MSDN that works, but to me it seems wrong. Private Sub MenuItem6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem6.Click Dim frmAction As New fclsRoomAction frmAction.ShowDialog() lstRoomAction.Items.Add(frmAction.tbActionDesc.Text) End Sub Here I have called my 2nd form, but I set my listview with the variable AFTER I have closed the form. Just dosn't seem right to me because I have closed the file BEFORE I read my variable. I think this has something to do with the way .net does garbage upkeep. I'm not sure. Like I said I'm new to this. So... Now I ask. Is this the standard way of passing variables between forms or is there a more elegent way of doing it. I was originally hoping to be able to do it on form2 as there I can create 2 buttons, accept and cancel, which determine whether I copy the variable over or not. But as I cannot access my listview on form2 I was unable to do this. Hopefully this makes sense. Thanks. Randy.

    C 1 Reply Last reply
    0
    • M MadCow

      ok, I'm completely new to VB and have come up with a problem on something that I think it really easy. What I have are 2 forms, form1 and form2. Form2 gets called from a context popup on a listview on form1. Now, on form2 I have a textbox variable that I want transfered back to the listview on form1 when you click on the OK button on form2. However I am unable to access the listview on form1 from form2. I figure it's a scope issue. I have found a work around on MSDN that works, but to me it seems wrong. Private Sub MenuItem6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem6.Click Dim frmAction As New fclsRoomAction frmAction.ShowDialog() lstRoomAction.Items.Add(frmAction.tbActionDesc.Text) End Sub Here I have called my 2nd form, but I set my listview with the variable AFTER I have closed the form. Just dosn't seem right to me because I have closed the file BEFORE I read my variable. I think this has something to do with the way .net does garbage upkeep. I'm not sure. Like I said I'm new to this. So... Now I ask. Is this the standard way of passing variables between forms or is there a more elegent way of doing it. I was originally hoping to be able to do it on form2 as there I can create 2 buttons, accept and cancel, which determine whether I copy the variable over or not. But as I cannot access my listview on form2 I was unable to do this. Hopefully this makes sense. Thanks. Randy.

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      The general way I'd do this would be to create a property for each value that I want to expose on Form2. Create an event handler for the Closing event and populate the values that the propery will expose. e.g.

      'The value to return to the other form
      Private myValue As String
      
      ' This is the propery that exposes the value needed by 
      ' the other form
      Public ReadOnly Property ValueForOtherForm() As String
          Get
              Return Me.myValue
          End Get
      End Property
      
      ' When the Form closes ensure that the value is transered so
      ' that the property works.
      Private Sub Form2\_Closing(ByVal sender As Object, \_
                  ByVal e As System.ComponentModel.CancelEventArgs) \_
                  Handles MyBase.Closing
          myValue = Me.tbSomeControl.Text
      End Sub
      

      Then, back in Form1, you can do this:

      Private Sub MenuItem6_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles MenuItem6.Click
      Dim frmAction As New fclsRoomAction
      frmAction.ShowDialog()
      lstRoomAction.Items.Add(Form2.ValueForOtherForm)
      End Sub

      I hope this helps. --Colin Mackay--

      "In the confrontation between the stream and the rock, the stream always wins - not through strength but perseverance." (H. Jackson Brown) Enumerators in .NET: See how to customise foreach loops with C#

      M 1 Reply Last reply
      0
      • C Colin Angus Mackay

        The general way I'd do this would be to create a property for each value that I want to expose on Form2. Create an event handler for the Closing event and populate the values that the propery will expose. e.g.

        'The value to return to the other form
        Private myValue As String
        
        ' This is the propery that exposes the value needed by 
        ' the other form
        Public ReadOnly Property ValueForOtherForm() As String
            Get
                Return Me.myValue
            End Get
        End Property
        
        ' When the Form closes ensure that the value is transered so
        ' that the property works.
        Private Sub Form2\_Closing(ByVal sender As Object, \_
                    ByVal e As System.ComponentModel.CancelEventArgs) \_
                    Handles MyBase.Closing
            myValue = Me.tbSomeControl.Text
        End Sub
        

        Then, back in Form1, you can do this:

        Private Sub MenuItem6_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles MenuItem6.Click
        Dim frmAction As New fclsRoomAction
        frmAction.ShowDialog()
        lstRoomAction.Items.Add(Form2.ValueForOtherForm)
        End Sub

        I hope this helps. --Colin Mackay--

        "In the confrontation between the stream and the rock, the stream always wins - not through strength but perseverance." (H. Jackson Brown) Enumerators in .NET: See how to customise foreach loops with C#

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

        Thanks for the help. I'm pretty new to the oop aspect of VB, but I assume thats what you were describing there as I know that properties in objects have get/set. So after spending some time reading up on objects and browsing your code, I understand whats going on, but it troubles me how I'm able to reference a variable from a form that dosn't exist anymore. It's been closed after all and all variable on that form should be gone. It just feel wrong for me to code like that. Is this how VB.net is normally done? Can't you reference a control (eg. textbox) that exists on form1 from form2 and put it directly into it's text property without having to create storage variables? I can't seem to get it to work. I must be missing something... Randy.

        C 1 Reply Last reply
        0
        • M MadCow

          Thanks for the help. I'm pretty new to the oop aspect of VB, but I assume thats what you were describing there as I know that properties in objects have get/set. So after spending some time reading up on objects and browsing your code, I understand whats going on, but it troubles me how I'm able to reference a variable from a form that dosn't exist anymore. It's been closed after all and all variable on that form should be gone. It just feel wrong for me to code like that. Is this how VB.net is normally done? Can't you reference a control (eg. textbox) that exists on form1 from form2 and put it directly into it's text property without having to create storage variables? I can't seem to get it to work. I must be missing something... Randy.

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          MadCow wrote: it troubles me how I'm able to reference a variable from a form that dosn't exist anymore The form "object" exists, but the controls don't. One of the more difficult concepts to understand is that there is managed (that's .NET) and unmanaged (that's Win32 API) stuff going on. The .NET Framework hides most of the Win32 implementation but you do get the occasional glimps that it is there sitting in the background. Let me try and explain better. The WinForms part of .NET Framework is a wrapper around the Win32 API which is what drives Windows. When the form is closed .NET cleans up all the controls by disposing of them immediately rather than wait for the garbage collector to do it. [The way controls work in Win32 mean there is actually limited space for them so it is very important to let the system know when you are done with them]. The Form object still exists, but the underlying (Win32) controls on it don't because .NET has told Windows they are no longer required. The managed things, like the exposed string propery do still exist because they have no unmanaged element. MadCow wrote: It's been closed after all and all variable on that form should be gone Just because a form has been closed doesn't mean it doesn't exist. It just means that the User Interface part is, well, "Closed". MadCow wrote: Is this how VB.net is normally done? Personally, I'd create a controller object to store all that stuff and access it from the various forms that need it. I don't know if this is going to be too much information to take in at this point, but you could also read this article about the Model-View-Controller pattern[^] on Microsoft's web site. It also has links to other related articles about how to write good software. MadCow wrote: Can't you reference a control (eg. textbox) that exists on form1 from form2 and put it directly into it's text property without having to create storage variables? I can't seem to get it to work I'm guessing that you need to cast the variable referencing form1 into the correct type. You probably have the base class in the .NET framework and you need to cast it to the specific type to access anything that is added in the derived class. I'm sorr

          M 1 Reply Last reply
          0
          • C Colin Angus Mackay

            MadCow wrote: it troubles me how I'm able to reference a variable from a form that dosn't exist anymore The form "object" exists, but the controls don't. One of the more difficult concepts to understand is that there is managed (that's .NET) and unmanaged (that's Win32 API) stuff going on. The .NET Framework hides most of the Win32 implementation but you do get the occasional glimps that it is there sitting in the background. Let me try and explain better. The WinForms part of .NET Framework is a wrapper around the Win32 API which is what drives Windows. When the form is closed .NET cleans up all the controls by disposing of them immediately rather than wait for the garbage collector to do it. [The way controls work in Win32 mean there is actually limited space for them so it is very important to let the system know when you are done with them]. The Form object still exists, but the underlying (Win32) controls on it don't because .NET has told Windows they are no longer required. The managed things, like the exposed string propery do still exist because they have no unmanaged element. MadCow wrote: It's been closed after all and all variable on that form should be gone Just because a form has been closed doesn't mean it doesn't exist. It just means that the User Interface part is, well, "Closed". MadCow wrote: Is this how VB.net is normally done? Personally, I'd create a controller object to store all that stuff and access it from the various forms that need it. I don't know if this is going to be too much information to take in at this point, but you could also read this article about the Model-View-Controller pattern[^] on Microsoft's web site. It also has links to other related articles about how to write good software. MadCow wrote: Can't you reference a control (eg. textbox) that exists on form1 from form2 and put it directly into it's text property without having to create storage variables? I can't seem to get it to work I'm guessing that you need to cast the variable referencing form1 into the correct type. You probably have the base class in the .NET framework and you need to cast it to the specific type to access anything that is added in the derived class. I'm sorr

            M Offline
            M Offline
            MadCow
            wrote on last edited by
            #5

            Thanks for all of the help. I checked out those articles on Microsoft's site, but as you said, it's a bit beyond me right now. But I have it bookmarked and will go back to it in a few months. I ended up just using the method that dosn't make me feel comfortable. I figure if it works I won't worry about it. The explanation that you gave for a form being closed, but not necessarily non-existant helped. And I will look into casting my reference form object. You had it bang on when you said that I was trying to do stuff with my base class object. Right now I'm just working on other stuff and leaving that form stuff for a while. I need to work on something thats going to work for a change. :-) Thanks again for all the help! Take care. Mooooo. Grrrr.. Mooo..

            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