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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing data in a text box from one form to another?

Status
Not open for further replies.

dollarbillg

Programmer
Dec 3, 2002
12
0
0
US
I have a newbie question.
I have a text box on a form and a button. When the button is clicked I need the data from form1.txtBox.Text on form1 to be inserted into form2.txtBox2.Text.

Any and all help is welcome.

Thanks
 
The Source text box (which is going to provide tha data) should be made Public Shared ...
then in the receiving form
you can say ...
SourceTextBox = recForm.rectextBox

 
harshaddesai is right... you can do so. I would say there are different scenarios which say the way how you should go...

scenarios
- Form1 creates Form2 and displayes it
Declare the Formvariable of Form2 as private in Form1
Code:
Public Class Form1
    Inherits System.Windows.Forms.Form
    Private frmForm2 as New Form2
    ...
    Private Sub Button1_Click(...
	frmForm2.Textbox2.text = me.Textbox1.text
    End Sub
- Form2 was created elsewhere...
You have to get a reference to Form2 or to the Textbox2 on Form2...
There are several possibilities to do that, which I don't even know
all of them :)
I think the most common way is to create a Property on Form1 which
holds the reference to Form2:
Code:
Public Class Form1
    Inherits System.Windows.Forms.Form
    Private frmForm2 as Form2
    ...
    Public Property Form2() As Form2
      Get
        Return frmForm2
      End Get
      Set(ByVal Value As Form2)
        frmForm2 = Value
      End Set
    End Property

    Private Sub Button1_Click(...
	frmForm2.Textbox2.text = me.Textbox1.text
    End Sub
Now you have to initialize the Form2-property of Form1
at the moment when you create Form1. This way you have
to have a reference to both Forms somewhere in your code.

hope this helps
Andreas
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top