Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 不同控件之间的事件检测_Vb.net_Oop - Fatal编程技术网

Vb.net 不同控件之间的事件检测

Vb.net 不同控件之间的事件检测,vb.net,oop,Vb.net,Oop,我有一个类a,它包含2个声明为 Friend WithEvents CustInfo1 As WindowsApplication1.CustInfo Friend WithEvents ServiceLocation1 As WindowsApplication1.ServiceLocation 两者都有文本框。如果custInfo1中textBoxA的值发生变化,那么如何使SeviceLocation1中textBoxB的值也发生变化 如果有人能帮助我,我将非常感激 谢谢您需要执行以下操作

我有一个类a,它包含2个声明为

Friend WithEvents CustInfo1 As WindowsApplication1.CustInfo
Friend WithEvents ServiceLocation1 As WindowsApplication1.ServiceLocation
两者都有文本框。如果custInfo1中textBoxA的值发生变化,那么如何使SeviceLocation1中textBoxB的值也发生变化

如果有人能帮助我,我将非常感激


谢谢

您需要执行以下操作:

  • 在CustInfo用户控件内,您需要在TextBoxChanged事件中编写代码,该事件会引发来自CustInfo用户控件的事件(例如TextBoxChanged事件)

  • 在ServiceLocation用户控件内,为textBoxB.Text是什么创建公共属性getter和setter

  • 在包含两个用户控件的窗体上,在new CustInfo TextBoxChanged事件中创建代码,并在ServiceLocation1用户控件上设置new属性

  • 所有控件(也是自定义控件)都具有该属性,通过该属性可以访问该控件的(子)控件。现在,您可以通过调用文本框上的
    .Item(key)
    方法来检索文本框。然后可以在窗体或类中为其分配事件处理程序

    Dim key As String = "textBoxA" 'Or simply the name of that TextBox in your CustInfo
    Dim textboxA As TextBox = CustInfo1.Controls.Item(key)
    AddHandler textBoxA.TextChanged, AddressOf mytextchangedhandler
    
    其中,
    mytextchangedhandler
    处理该文本框的TextChanged事件

    就我个人而言,我不太喜欢这种方法,因为您需要知道文本框的名称或usercontrol控件列表中的索引

    我肯定会选择在usercontrol上创建自己的事件。这是很容易做到甚至!下面是如何做的。在usercontrol的代码隐藏中,您必须添加一个事件声明:

    Event MyTextBoxChanged(sender As Object, e As EventArgs)
    
    现在我们必须提出它,我们通过在用户控件中实现
    TextBoxA
    TextChanged
    事件来实现这一点(正如您所解释的那样):

    现在,我们可以简单地在您的表单中实现此事件(
    MyTextBoxChanged
    ),如下所示:

    Private Sub CustInfo1_MyTextBoxChanged(sender As System.Object, e As System.EventArgs) Handles CustInfo1.MyTextBoxChanged
        ' Do something
    End Sub
    
    显然,我们仍然需要获得更新的文本,现在我们可以创建自己的EventArgs,它将为我们提供您想要的新(和/或旧)值。我们只需继承
    System.EventArgs
    类并添加一些属性(例如,保存旧文本值的属性
    OldText
    ,以及保存新文本值的属性
    NewText
    ):

    现在我们必须更改事件定义并使用
    MyEventArgs

    Event MyTextBoxChanged(sender As Object, e As MyEventArgs)
    
    Private Sub TextBoxA_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBoxA.TextChanged
        RaiseEvent MyTextBoxChanged(Me, New MyEventArgs(TextBoxA.Text))
    End Sub
    
    并更改表单中的实现:

    Private Sub CustInfo1_MyTextBoxChanged(sender As System.Object, e As MyEventArgs) Handles CustInfo1.MyTextBoxChanged
        MessageBox.Show(e.Text)
    End Sub
    

    有关事件的更多信息可以在我们最喜欢的地点找到。

    您必须从CustInfo控件捕获事件,然后调用ServiceLocation控件上的某些方法。(您可能必须在控件中创建自己的“已更改”事件。)是否可以从custInfo控件调用类a中的函数
    Event MyTextBoxChanged(sender As Object, e As MyEventArgs)
    
    Private Sub TextBoxA_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBoxA.TextChanged
        RaiseEvent MyTextBoxChanged(Me, New MyEventArgs(TextBoxA.Text))
    End Sub
    
    Private Sub CustInfo1_MyTextBoxChanged(sender As System.Object, e As MyEventArgs) Handles CustInfo1.MyTextBoxChanged
        MessageBox.Show(e.Text)
    End Sub