I am trying to send data from Form2 to Form1 but it doesn't work. I will just show the image so that you could understand what the problem is all about.
The problem here is it doesn't work all. I cannot send the data to the Form1. How to make it work?
Here's the VB code for AddLesseeForm class
Public Class AddLesseeForm 'Form2
'This is the Select Button
Public Sub Button4_SelectLessee_Click(sender As Object, e As EventArgs) Handles Button4_SelectLessee.Click
'Send data to AddData Form. The problem is it doesn't work
Dim OBJ As New AddData
OBJ.LesseeId = TextBox1_LesseeID.Text
OBJ.LesseeName = TextBox2_LesseeName.Text
End Sub
End Class
VB code for AddData class
Public Class AddData 'Form1
Private Sub Button1_AddLesseeForm_Click(sender As Object, e As EventArgs) Handles Button1_AddLesseeForm.Click
AddLesseeForm.Show()
End Sub
Public Property LesseeId As String
Public Property LesseeName As String
Public Sub AddData_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Receive data from AddLesseeForm
TextBox1_LesseeId.Text = LesseeId
TextBox2_LesseeNm.Text = LesseeName
End Sub
End Class
you do not need to create an object of AddData
, in VB.Net you can call with only name of Form, like that :
Public Sub Button4_SelectLessee_Click(sender As Object, e As EventArgs) Handles Button4_SelectLessee.Click
AddData.LesseeId = TextBox1_LesseeID.Text
AddData.LesseeName = TextBox2_LesseeName.Text
AddData.UpdateData()
End Sub
and you can create a method for update data in TextBox
in AddData
Form like this :
Public Sub UpdateData()
TextBox1_LesseeId.Text = LesseeId
TextBox2_LesseeNm.Text = LesseeName
End Sub
First of all, setting your LesseeId
and LesseeName
properties won't update anything. Just because you're assigning them to somethig in the Form Load
event it doesn't mean that they are bound to those controls.
Secondly, Dim OBJ As New AddData
creates a completely new AddData
form with it's own references. So you're setting the properties for a form that you never even show.
To solve this, you can show AddLesseeForm
with it's owner set to AddData
:
Private Sub Button1_AddLesseeForm_Click(sender As Object, e As EventArgs) Handles Button1_AddLesseeForm.Click
AddLesseeForm.Show(Me)
End Sub
Now that AddLesseeForm
's owner is your AddData
form, you can do like this:
Public Sub Button4_SelectLessee_Click(sender As Object, e As EventArgs) Handles Button4_SelectLessee.Click
If Me.Owner IsNot Nothing AndAlso Me.Owner.GetType() Is GetType(AddData) Then
Dim AddDataFrm As AddData = DirectCast(Me.Owner, AddData)
AddDataFrm.LesseeId = TextBox1_LesseeID.Text
AddDataFrm.LesseeName = TextBox2_LesseeName.Text
End If
End Sub
Which will get the owner (the current instance of AddData
) and be able to modify it.
Lastly you should change the properties in AddData
so that they actually update your text boxes:
Private _lesseeid As Integer = 0
Private _lesseename As String = ""
Public Property LesseeId As Integer
Get
Return _lesseeid
End Get
Set(value As Integer)
_lesseeid = value
TextBox1_LesseeId.Text = value.ToString()
End Set
End Property
Public Property LesseeName As String
Get
Return _lesseename
End Get
Set(value As String)
_lesseename = value
TextBox2_LesseeNm.Text = value
End Set
End Property
VB code for AddLesseeForm class
Public Class AddLesseeForm 'Form2
'This is the Select Button
Public Sub Button4_SelectLessee_Click(sender As Object, e As EventArgs) Handles Button4_SelectLessee.Click
AddData.loadLessee(TextBox1_LesseeID.Text, TextBox2_LesseeName.Text)
End Sub
End Class
VB code for AddData class
Public Class AddData 'Form1
Private Sub Button1_AddLesseeForm_Click(sender As Object, e As EventArgs) Handles Button1_AddLesseeForm.Click
AddLesseeForm.Show()
End Sub
Public Sub loadLessee(LesseeId As String, LesseeName As String)
TextBox1_LesseeId.Text = LesseeId
TextBox2_LesseeNm.Text = LesseeName
End Sub
Public Sub AddData_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
End Class
First you place below code on send button click event:
Private Sub button1_Click(sender As Object, e As EventArgs)
Dim objtest As New test2(txtLesseeid.Text, txtLesseename.Text)
objtest.ShowDialog()
End Sub
On Second form where you want to get value you should place this code:
Public Partial Class test2
Inherits Form
Public Sub New(strtext1 As String, strtext2 As String)
InitializeComponent()
txtLesseeid.Text = strtext1
txtLesseename.Text = strtext2
End Sub
End Class
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.