Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing values between forms

Status
Not open for further replies.

smeyer52

Technical User
Mar 11, 2001
9
US
Please help me pass a value from form2 back to form1. I am obviously a novice.

Form1 has textbox displaying the client's order, and a button to place order. Form1.button_click pops up form2.

Form2 displays listbox with multiple choices.
Form2 also has button to accept order, which will then be displayed in form1.textbox.

Form2.button_click code:
Private Sub btnAccept_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAccept.Click
Dim strOrders As String = ""
Dim strOrder As String = ""
Dim frm1 As Form1

'create a string from the selected lines in list box
For Each strOrder In ListBox1.SelectedItems
strOrders &= strOrder
Next

'now pass the string back to form1's text box
frm1.TextBox1.Text = strOrders
'compiles but will not run
'error: Object reference (frm1) not set to an instance of an object.
End Sub

Obviously I do not know how to refer back to form1 and its textbox from form2. Thanks for any help.
 

When we create a variable, it doesn't have an object instance associated with it, you are simply identifying the type of object.

To create an instance of the object New keyword is used

 
Take the above advice.

But to do what you want to do, there are many, many ways. You really don't want to access Form1 from Form2. You can make a static variable in your Form2, or you can do something like this:

Form2:
Code:
Public Class Form2
    Inherits System.Windows.Forms.Form
    Dim txt As System.Windows.Forms.TextBox

     Public Sub SetText(ByRef text As   System.Windows.Forms.TextBox)
        txt = text
     End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        txt.Text = "This text is being set in Form2."
    End Sub
End Class

Form1:
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim f2 As New Form2()
        f2.SetText(TextBox1)
        f2.Show()
End Sub
 
Thanks, PankajBanga; but the code "Dim as New" creates a new instance of form1 when I already have the original form1 on the screen. So I wind up with two Form1 objects on my screen.

I intend for form1 to up form2, but to remain visible while passing focus to form2. Form 2 will ideally update form1 and then close, passing focus back to form1.
 
I think he is just giving you an example. Apply his idea to your Form2, not Form1.
 

use the following code on Button Click event of Form1

Dim frm As New Form2()
frm.ShowDialog(Me)
Me.TextBox4.Text = frm.ListBox1.SelectedItem

TextBox4 is the textbox on Form1 where you want to display the Item selected from ListBox

NOTE: a form will not be displayed untill u use Show or ShowDialog method even though you have Instantiated it.


 

set the DialogResult property for the Accept button on Form2 to OK.

make sure you select an Item from the ListBox before you press accept

 
Thanks, PankajBanga. That solution worked.

Instead of form2 passing the value to form1, which form2 could not see as instantiated, form1 retrieves the value from form2, which form1 instantiated. Also, I was using frm.show(), but it only works correctly with frm.showdialog().

THanks again. I hope to learn a lot more VB.net.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top