esposito Posted November 22, 2003 Posted November 22, 2003 Hello, I am struggling with a problem dealing with the interaction between forms in VB.NET. Typically, when I load a new form using the ShowDialog method, I am unable to export any value from the form on top to the one underneath. To clarify what I mean, suppose I load Form2 in the ShowDialog mode without hiding Form1, which remains visible underneath. Form2 contains a listbox from which the user is allowed to select an item which will then appear in a textbox in the underlying Form1. After the selection of the item, Form2 is unloaded automatically and the user finds himself again in Form1, which now contains the imported value in a specific textbox. In VB6 this operation was extremely easy. All you had to do was insert the following code in the Double_Click event of the listbox contained in Form2: Form1.Text1.Text = Form2.List1.Text Unload Me I'm afraid things have gotten a little more complicated in VB.NET, since everytime you make a reference to another form or to its objects, you have to create a new instance of that form. In my case, Form1 is already open and so I think it doesn't make any sense to create a new istance of it, since in that way I would show the same form twice on the screen. Does anybody know whether there is a solution to my problem? Thanks in advance. Quote Pasquale Esposito Perugia - Italy http://www.geocities.com/espositosoftware
*Experts* mutant Posted November 22, 2003 *Experts* Posted November 22, 2003 When showing you other form as a dialog simply pass in the value to the Owner argument which will then keep a reference to your original form from the dialog. Dim f2 As New Form2 f1.ShowDialog(Me) 'pass in the reference to your current form Then , from your dialog form access the owner like this: 'if you want to use objects that belong to your form class not the Form class 'then you have to cast the owner object to the wanted type DirectCast(Me.Owner, Form1).TextBox1.Text = "some text" Quote
esposito Posted November 22, 2003 Author Posted November 22, 2003 Thank you very much for your help. I have changed the code you gave me a little bit to get rid of the DirectCast statement, so the code I used in my project runs as follows: 'Launches Form2: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim f As New Form2 f.Owner = Me f.ShowDialog() End Sub 'In Form2, to make a reference to Form1: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim f As Form1 f = Me.Owner f.Textbox1.Text = ListBox1.Text End Sub Thanks again. When showing you other form as a dialog simply pass in the value to the Owner argument which will then keep a reference to your original form from the dialog. Dim f2 As New Form2 f1.ShowDialog(Me) 'pass in the reference to your current form Then , from your dialog form access the owner like this: 'if you want to use objects that belong to your form class not the Form class 'then you have to cast the owner object to the wanted type DirectCast(Me.Owner, Form1).TextBox1.Text = "some text" Quote Pasquale Esposito Perugia - Italy http://www.geocities.com/espositosoftware
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.