Pass data between forms

H

hugh

Hi,

I thought this is very basic and I still can not get it
right. I am confused.

I want to pass the textbox1.text on form1 (mainform) to
textbox1.text on form2. I was able to do it sometimes not
always. Sometime, it works for some textbox.text not all
textbox if there are more than one textbox on the form.
What is right way or reliable way to do it?

My sample code:

Sub GetValueFromAnothForm()
Dim frm1 as New Form1
me.textbox1.text=frm1.textbox1.text
me.textbox2.text=frm1.textbox2.text
End sub

Thanks in advance.
 
H

Hugh

Hi,
Please read another post in the same title. this is a
duplicate message, I thought it was not posted. Sorry for
confusion.
 
N

norton

In form 2

Private _TxtBoxValue As String
Public WriteOnly Property TxtBoxValue() As String
Set(ByVal Value As String)
_TxtBoxValue = Value
End Set
End Property

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.textbox2.text = _TxtBoxValue
End Sub

in form1
Sub GetValueFromAnothForm()
Dim frm2 as New Form2
frm2 .TxtBoxValue = me.textbox1.text
.....
End sub


Hope it helps
 
H

Hugh

Hi Norton,

Thanks for your help. I copy/paste your code and it did
not work. I mean, the textbox on form 2 is empty.

Hugh
 
A

Adam

Hi hugh,

On form2:

Public Sub GetValueFromForm1(ByVal textOnForm1 As String)
TextBox1.Text = textOnForm1
End Sub

On form1:

Private Sub btnOpenForm2_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnOpenForm2.Click
Dim form2 As New Form2()
Dim textInTextBoxOnForm1 As String
textInTextBoxOnForm1 = TextBox1.Text
form2.GetValueFromForm1(textInTextBoxOnForm1)
End Sub

Hope this helps,
Adam.
 
G

Guest

Hi, Adam,

Your code worked well. It is not stright forward. I guess
I would rather to use global variable if I need to pass
many variables between forms. Thanks for your tip.

Hugh
 
A

Ayaz Ahmed

Hello,

use array of textboxes and use loop to pass form1 textboxes values in
form2 textboxes,

i hope u will understand .

Thanks,


Warm Regards,

Ayaz Ahmed
Software Engineer & Web Developer
Creative Chaos (Pvt.) Ltd.
"Managing Your Digital Risk"
http://www.csquareonline.com
Karachi, Pakistan
Mobile +92 300 2280950
Office +92 21 455 2414
 
T

Terry Olsen

I'm having trouble with this example also... Here's what I got:

[Form1]

Public Function GetTextBox1()
Return txtTextBox1.Text
End Function

[Form2]

Dim f as New Form1
Msgbox f.GetTextBox1

Why doesn't this work?
 
T

Terry Olsen

oops...the line below should read:

Public Function GetTextBox1() as String

Terry Olsen said:
I'm having trouble with this example also... Here's what I got:

[Form1]

Public Function GetTextBox1()
Return txtTextBox1.Text
End Function

[Form2]

Dim f as New Form1
Msgbox f.GetTextBox1

Why doesn't this work?



Adam said:
Hi hugh,

On form2:

Public Sub GetValueFromForm1(ByVal textOnForm1 As String)
TextBox1.Text = textOnForm1
End Sub

On form1:

Private Sub btnOpenForm2_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnOpenForm2.Click
Dim form2 As New Form2()
Dim textInTextBoxOnForm1 As String
textInTextBoxOnForm1 = TextBox1.Text
form2.GetValueFromForm1(textInTextBoxOnForm1)
End Sub

Hope this helps,
Adam.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top