Click here to Skip to main content
15,894,297 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

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

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.
 
Share this answer
 
Comments
Espen Harlinn 6-Feb-11 9:39am    
Nicely spotted as usual :)
Add a public property to form b. Then you can do something like:
VB
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
VB
txtAto.Text = Value


Hope this helps.
 
Share this answer
 
v2
Comments
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 :)
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:
VB
Public Property ToAddress() As String
    Get
        Return tbToAddress.Text
    End Get
    Set
        tbToAddress.Text = value
    End Set
End Property
And access that instead.
 
Share this answer
 
Comments
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 http://en.wikipedia.org/wiki/Object-oriented_programming
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

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900