I want to create event for composite control

seco

Well-known member
Joined
Mar 4, 2007
Messages
66
Programming Experience
Beginner
hi
i drop a button and combobox and i want to make them one control and i want to raise events for the button and for the comobox at the runtime how can i do that?
my idea is to raise custom event at the click event for the button maybe this is true maybe not !!

any help.

thanks in advance.
 
You declare events like this:
VB.NET:
Expand Collapse Copy
Public Event myevent
Public Event myevent2(parameter As String)
'etc
and raise them like this:
VB.NET:
Expand Collapse Copy
RaiseEvent myevent
RaiseEvent myevent2("some string")
In the UserControl that had the button and other controls you wanted to raise button event you could do this:
VB.NET:
Expand Collapse Copy
Public Event ButtonClick(ByVal sender As Object, ByVal e As EventArgs)
 
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click
    RaiseEvent ButtonClick(Me, e)
End Sub
Remember that it is the composite control that is exposed to user, and user may add several instances of this control to a form, so the event should be an event of this control and 'sender' should identify this instance.
 
thanks

ok but what if i want to use all of the button events and combobox events in the composite cotrol ?

i mean is there is a way to expose all of the individual events of each control to use it at runtime

thanks in advance.
 
The button itself is already exposed by the usercontrol as Friend Withevents, and you can use AddHandler to dynamically assign event handler to it, or you can assign the usercontrol button reference to a Private WithEvents variable that lets you select its events in the forms events dropdownlist as with the forms other WithEvents objects.

In code, either:
VB.NET:
Expand Collapse Copy
    Private Sub Forms_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        AddHandler UserControl11.Button1.Click, AddressOf Ubutton_Click
    End Sub
 
    Private Sub Ubutton_Click(ByVal sender As Object, ByVal e As System.EventArgs) 
        MsgBox("Hello usercontrol button")
    End Sub
or:
VB.NET:
Expand Collapse Copy
    Private WithEvents Ubutton As Button
 
    Private Sub Forms_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Ubutton = UserControl11.Button1
    End Sub
 
    Private Sub Ubutton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Ubutton.Click
        MsgBox("Hello usercontrol button")
    End Sub
 
Back
Top