简体   繁体   English

如何在VB.NET中单击按钮时将数据从Form2发送到Form1到Form1 TextBox中?

[英]How to send data from Form2 to Form1 into the Form1 TextBox when the button is clicked in VB.NET?

I am trying to send data from Form2 to Form1 but it doesn't work.我正在尝试将数据从 Form2 发送到 Form1,但它不起作用。 I will just show the image so that you could understand what the problem is all about.我将只显示图像,以便您了解问题的全部内容。

Here's the image:这是图片: 问题的视觉场景

The problem here is it doesn't work all.这里的问题是它不起作用。 I cannot send the data to the Form1.我无法将数据发送到 Form1。 How to make it work?如何使它工作?

Here's the VB code for AddLesseeForm class这是AddLesseeForm class的 VB 代码

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 AddData class VB 代码

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 :您不需要创建AddData对象,在 VB.Net 中您可以只使用 Form 名称调用,如下所示:

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 :您可以在AddData表单中创建一个更新TextBox数据的方法,如下所示:

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.首先,设置您的LesseeIdLesseeName属性不会更新任何内容。 Just because you're assigning them to somethig in the Form Load event it doesn't mean that they are bound to those controls.仅仅因为您在Form Load事件中将它们分配给某些东西,并不意味着它们被绑定到这些控件。

Secondly, Dim OBJ As New AddData creates a completely new AddData form with it's own references.其次, Dim OBJ As New AddData创建了一个全新的AddData表单,并使用它自己的引用。 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 :要解决此问题,您可以显示AddLesseeForm并将其所有者设置为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:现在AddLesseeForm的所有者是您的AddData表单,您可以这样做:

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.这将获得所有者( AddData的当前实例)并能够对其进行修改。

Lastly you should change the properties in AddData so that they actually update your text boxes:最后,您应该更改AddData的属性,以便它们实际更新您的文本框:

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 AddLesseeForm class VB 代码

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 AddData class VB 代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM