开发者

How to update a textbox on Form from a click on a button on a User Control

I need update the text of a textb开发者_StackOverflowox when I do click on a button on a user control.

How I can do that?


Declare an event in the user control and have the button's Click event raise it:

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

Now the form that hosts the user control can subscribe to the event and update the text box as needed:

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


And how I send a string from the user control to the form?

I need send data from user control to thr form and read it in the textbox. For example:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜