Passing variable between parent/child forms
-
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.
-
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.
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 SubI 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#
-
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 SubI 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#
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.
-
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.
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
-
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
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..