使用VB将一个用户控件中的值传递给另一个用户控件

时间:2011-06-15 15:38:29

标签: controls

我有一个带文本框的用户控件,我需要访问另一个用户控件中的标签中此文本框中输入的值。我如何在vb.Thanks提前做到这一点。

1 个答案:

答案 0 :(得分:0)

在第一个用户控件(UserControl1)中创建一个共享事件:

Friend Shared Event GetTextBoxText(ByVal myString As String)

然后您可以使用(UserControl1)

上的按钮引发此事件
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

    'raise the event with the text from the text box
    RaiseEvent GetTextBoxText(TextBox1.Text)

End Sub
第二个用户控件(UserControl2)上的

在构造函数中为事件添加处理程序:

Public Sub New()

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.

    'this will let us handle the event from (UserControl1)
    AddHandler UserControl1.GetTextBoxText, AddressOf SetLabelText

End Sub

Private Sub SetLabelText(ByVal myString As String)
    Label1.Text = myString
End Sub

现在每当您单击(UserControl1)上的按钮时,文本将显示在UserControl2上的标签中

您还可以在任何控件上添加事件处理程序并响应GetTextBoxText事件