Button Event in UserControl

jamie_pattison

Well-known member
Joined
Sep 9, 2008
Messages
116
Programming Experience
Beginner
I have a user control with 3 buttons. The user control is used across a few dynamically created forms. I would like to control the button event for each form (which is using my user control) most likely doing different tasks when clicked - how could i achieve this?

Thanks
 
I would handle the click event of the button in the user control simply to raise an event. Then when a form is dynamically made use the AddHandler to connect the event your user control raises to a sub in the form. Here's an example using a TextBox:
VB.NET:
Private Sub CreateControls()
    Dim MyTextBox As New TextBox
    MyTextBox.Location = new point (10, 10)
    Me.Controls.Add(MyTextBox) 'Adds it to the form
    AddHandler MyTextBox.TextChanged, AddressOf TextChanged
End Sub

Private Sub TextChanged(ByVal Sender As Object, ByVal e As System.EventArgs)
  'Event handle code
End Sub
 

Latest posts

Back
Top