tags:

views:

40

answers:

2

Hi, I need update the text of a textbox when I do click on a button on a user control.

How I can do that?

+3  A: 

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
Hans Passant
A: 

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
Sein Kraft
The string can be a parameter in UserControl11.MyButtonClick. You can use any parameter for the event, such as a string, or you can add some members to eventargs.
xpda
I updated my code sample to take care of this additional requirement.
Hans Passant