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 textbox text from one form to another

Status
Not open for further replies.

at51178

Technical User
Mar 25, 2002
587
US
Hi,
I looked everywhere for this and it seems to be an easy question.

I have two forms each with a textbox on it
Form1 has text1
Form2 has text2


When I click on a button on form1 to open form 2 I would like to have any text in form1 to be passed over to text2.

I have been able to have it work only if the text is hard coded to the properties portion of the textbox but I would like to have it so when I type any thing in the textbox and press the button to open form2 that anything that Ityped in text1 would be passed over to text2.


thanks

 
this is the code I have so far in the load event of form2

Dim form1 as form1= New Form1

Me.text2.text = Form1.text1.text
 
Add a sub in the Form2 class:

Code:
  Public Sub New(ByVal Text As String)
    me.new
    Text2.text = Text
  End Sub

In the btn click on form1 do this:

Code:
frm2 = new form2(text1.text)
frm2.show

-Rick

----------------------
 
I suppose this is the code in the button with wich you open form2 in form1
then add the rest.

Code:
dim tempform2 as new form2
tempform2.text2.text = me.text1.text
form2.show

Your code is never going to work and you need to learn the OOP rules before starting to program in .net.



Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
Correct. You want to push the value of the textbox, not pull it.
The information in one form's textbox is protected from the others.
 
Thanks that worked
but just one other question related to this
what if I had two forms where form1 was the container and form2 was the child

On form 1 all I have is the main menu with print capabilities.

If I wanted to print the text inside text2 How would I go about pushing the data to the printer through form1.

Would I have to push the data to a textbox on form1 so that it would print it.
 
I'm not sure how you are printing, so I can't really help much there. But if you declare frm2 at the module level (ie: not in a sub, but inside the form1 class), then from anywheres in form1 you can access frm2.text2.text. just make sure that frm2 is instantiated first.

-Rick

----------------------
 

I am using gdi+ to print
here is part of the code that I am using on form1 to get some of the attributes of a textbox on form2 the only thing I wasn't able to capture was the text.

--Code --
e.Graphics.DrawString(Form2.txtName.Text, _
New Font(Form2.txtName.Font, FontStyle.Regular), _
New SolidBrush(Color.Black), _
Form2.txtName.Location.X, _
Form2.txtName.Location.Y)
-- End Code --
 
I found the answer to my last question
thanks I think I have a much better handle on how vb.net handles data from one form to another.
 
What is the best way to handle this situation. I have form2 already open with transaction being recorded. Form1 is opened to look up or add a new service code. When you are done editing or added a new service code you want that code pasted back to form2. The problem with my code is that it needs to open a new instance of form2. (and it successfully puts the data on new instance of form2)

How would I point the data toward the already open form2?

Thanks in advance.

 
By keeping a variable that has a reference to the instance of form2, saved when you created the instance of form2.
OR by using ShowDialog so that a modal form is used.



Compare Code
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top