Click here to Skip to main content
12,884,951 members (29,997 online)
Rate this:
Please Sign up or sign in to vote.
See more: VB VB.NET

How do you pass text from one textbox on a form (forma) to another textbox on another form (formb).

i am not getting an eror but the text just disappear and doesnt appear on the other form.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
     txtAto.Text = formmail.txtto.Text
 End Sub

is ther somethings i am not doing correctly :S
Posted 6-Feb-11 3:30am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Your code's doing the reverse of what you say you need to do. Your code assigns the text from the other form's textbox to your current form's textbox (but you are trying to do the opposite if I understand it right).

In general it's not a good idea to access another form's control variables directly. It may be better to expose a public method, something like SetSomeText(string text) in this other form that will internally assign the text to the right textbox.
Espen Harlinn 6-Feb-11 9:39am
Nicely spotted as usual :)
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Add a public property to form b. Then you can do something like:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
     formB.PassedText = formmail.txtto.Text
 End Sub

This assumes that the property on Form b is called PassedText and that Form b is created by Form A. If it is not then please describe the structure of your application.

Then in the Setter of your property
txtAto.Text = Value

Hope this helps.
SIFNOk 6-Feb-11 9:45am
Sorry my programming skils are very poor.
i dont reallly understand.
Henry Minute 6-Feb-11 9:53am
In your question you talk about form a and form b, then in your code we learn that one form is called 'formmail' but we don't know if that is form a or form b. So that we can both talk about the same thing, edit your question to use the proper form names.

OriginalGriff's answer has the code for the property I described. See if you can use that.
Espen Harlinn 6-Feb-11 10:40am
Another good answer :)
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

This is more complex that you think.

In order to do this, you must have access to both form instances: not the definitions, but the actual instance of the form that is running, and holds the information you want.

Using your names:

Assuming that your Button "button1" is on "formb", then if "formmail" contains the instance of "forma" then you can do it.

The first thing to check is: are you copying the right way? Is the text you want to copy in the TextBox "txtto" and the place you want to put it "txtAto" on the other form?

Put a breakpoint on the copy instruction, and check.

It is not a good idea to try accessing the content of other form controls: it ties the two forms too tightly together. You can't change one without checking is doesn't affect the other.
Instead, use a property:
Public Property ToAddress() As String
        Return tbToAddress.Text
    End Get
        tbToAddress.Text = value
    End Set
End Property
And access that instead.
SIFNOk 6-Feb-11 9:59am
Coool Thanks for that Griff! =)I truely apperiate your outlinee! i inderstand better. =D

Why would they make it that difficult to just pass text value from one form to another. Is there a reason?
Henry Minute 6-Feb-11 10:04am
It is one of the principles of Object Oriented Programming (OOP) called Encapsulation.
You will find these in the article at
OriginalGriff 6-Feb-11 10:32am
As Henry says "encapsulation".

What it boils down to is:
If you rigidly define the interfaces between a class and the outside world, you can change the way the class works internally without affecting any other code.

It's like a car: If you can drive a manual Ford, you can drive a Ferrari. Maybe not very well, but you can engage gear, release the brake and drive to the shops. Imaging what it was like when cars were first developed: every manufacturer had their own set of controls, each in a different place. For example, a Model T Ford had a clutch you had to hold down to make the car move. Changing cars was complex: you had to learn a whole new set of controls.

If you could easily access a TextBox on a different form, then you are stuck with having a TextBox on that form for ever - you can't change it for a drop-down list, or a data-linked Cloud control if someone invents one.
Espen Harlinn 6-Feb-11 10:39am
Good effort, as usual, my 5
farout9999 18-Oct-11 2:54am
got some problem.. got the value pass to when debugging but it wont show on the textbox the value
OriginalGriff 18-Oct-11 3:57am
Sorry? That doesn't make a lot of sense - please try again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170424.1 | Last Updated 6 Feb 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100