如何通过单击用户控件上的按钮更新窗体上的文本框

时间:2009-11-20 01:05:06

标签: vb.net

当我点击用户控件上的按钮时,我需要更新文本框的文本。

我怎么能这样做?

2 个答案:

答案 0 :(得分:5)

在用户控件中声明一个事件,让按钮的Click事件引发它:

Public Class UserControl1
  Public Event MyButtonClick As EventHandler(Of MyButtonClickEventArgs)

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    RaiseEvent MyButtonClick(Me, New MyButtonClickEventArgs("nobugz"))
  End Sub

  Public Class MyButtonClickEventArgs
    Inherits EventArgs
    Private mText As String
    Public Sub New(ByVal text As String)
      mText = text
    End Sub
    Public ReadOnly Property Text() As String
      Get
        Return mText
      End Get
    End Property
  End Class

End Class

现在,托管用户控件的表单可以订阅该事件并根据需要更新文本框:

  Private Sub UserControl11_MyButtonClick(ByVal sender As System.Object, ByVal e As WindowsApplication1.UserControl1.MyButtonClickEventArgs) Handles UserControl11.MyButtonClick
    TextBox1.Text = e.Text
  End Sub

答案 1 :(得分:0)

我如何从用户控件向表单发送字符串?

我需要将数据从用户控件发送到thr表单并在文本框中读取它。例如:

Private Sub UserControl11_MyButtonClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles UserControl11.MyButtonClick
    TextBox1.Text = e.mystring
End Sub