How do I pass data between to forms

levyuk

Well-known member
Joined
Jun 7, 2004
Messages
313
Location
Wales, UK
Programming Experience
3-5
Hi

I have 2 forms, 'NewEmployer' and 'Placement'.

'NewEmployer' has a textbox called 'TextBoxCompanyName'. It also has a button called 'Add Placement'. When the button is clicked it opens up the 'Placement' form using this code
VB.NET:
[size=2][color=#0000ff]Dim[/color][/size][size=2] placement [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]New[/color][/size][size=2] Placement[/size]
[size=2]placement.ShowDialog()[/size]
[size=2]
[/size]

The 'Placement' form also has a textbox called 'TextBoxCompanyName'. What I need to do is pass the text from the 'NewEmployer' form to the 'Placement' form.
I am trying this code but it wont work
VB.NET:
[/size]
[size=2][size=2][color=#0000ff]Dim[/color][/size][size=2] NewEmployer [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]New[/color][/size][size=2] NewEmployer2[/size][/size]
[size=2][size=2][color=#0000ff]Private[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub[/color][/size][size=2] Placement_Load([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]Object[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.EventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] [/size][size=2][color=#0000ff]MyBase[/color][/size][size=2].Load[/size][/size]
[size=2][size=2][color=#0000ff]Me[/color][/size][size=2].TextBoxCompanyName.Text = NewEmployer.TextBoxCompanyName.Text[/size][/size]
[size=2][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub[/color][/size][/size]
[size=2][size=2][color=#0000ff][color=black]
[/color][/color]
[/size]
Anyone know how I can do it
 

TPM

Well-known member
Joined
Dec 7, 2004
Messages
623
Location
CA
Programming Experience
3-5
make a public shared string, and take out the 'new' when diming newemployer.

TPM
 

mzim

Well-known member
Joined
Jun 3, 2004
Messages
187
Location
Other side of the rock
Programming Experience
1-3
levyuk said:
Hi

I have 2 forms, 'NewEmployer' and 'Placement'.

'NewEmployer' has a textbox called 'TextBoxCompanyName'. It also has abutton called 'Add Placement'. When the button is clicked it opens upthe 'Placement' form using this code
VB.NET:
[size=2][color=#0000ff]Dim[/color][/size][size=2] placement [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]New[/color][/size][size=2] Placement[/size]
[size=2]placement.ShowDialog()[/size]
[size=2]
[/size]

The 'Placement' form also has a textbox called'TextBoxCompanyName'. What I need to do is pass the text from the'NewEmployer' form to the 'Placement' form.
I am trying this code but it wont work
VB.NET:
[/size]
[size=2][size=2][color=#0000ff]Dim[/color][/size][size=2] NewEmployer [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]New[/color][/size][size=2] NewEmployer2[/size][/size]
[size=2][size=2][color=#0000ff]Private[/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2] Placement_Load([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Object[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.EventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2][color=#0000ff]MyBase[/color][/size][size=2].Load[/size][/size]
[size=2][size=2][color=#0000ff]Me[/color][/size][size=2].TextBoxCompanyName.Text = NewEmployer.TextBoxCompanyName.Text[/size][/size]
[size=2][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Sub[/color][/size][/size]
[size=2][size=2][color=#0000ff][color=black]
[/color][/color]
[/size]
Anyone know how I can do it
the formal thing in passing a value to another form in .NET is the use of property.
try this one.
VB.NET:
'in the form1
 Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Dim f As New Form2()
		f.passvalue = TextBox1.Text
		f.ShowDialog()
	End Sub

'in the form2
 Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		TextBox1.Text = s
	End Sub
Dim s As String
	Public Property passvalue() As String
		Get
			Return s
		End Get
		Set(ByVal Value As String)
			s = Value
		End Set
	End Property
or you could do like this
VB.NET:
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Dim f As New Form2()
		f.TextBox1.Text = TextBox1.Text
		f.ShowDialog()
	End Sub
 

DavidT_macktool

Well-known member
Joined
Oct 21, 2004
Messages
502
Location
Indiana
Programming Experience
3-5
Add a Module to your project...

I add a module to my projects called 'PublicVariables'
I add all variables there and can use them on any form.

looks like this:

Module PublicVariables
Public tCurrentCompany As String
Public tCurrentCustomer As String

Public iCurrentPartid As Integer
Public iCurrentPO As Integer

Public dStartdate As Date
Public dEndDate As Date

Public PartFound As Boolean
End Module


Holds values throughout project.

reference like this:
on sending form...
tCurrentCompany = Me.TextBoxCompanyName.Text

then on receiving form...
Me.TextBoxCompanyName.Text = tCurrentCompany


works really well for passing values to SQL Select Statements on new forms:
SqlSelectCommand1.Parameters("@partid").Value = iCurrentPartid

 
Last edited:
Top Bottom