Event Handling with multiple User Controls in vb.net

Good morning all. I think we are in for a rough ride those of us in the IT field. I feel fortunate to be employed and in a situation that is stable. I pray for all of you who are struggling right now.

IT Slashes Budgets, Starts Layoffs: Exclusive CIO Survey

So our topic today is how to handle events across multiple user controls. I have never been fond of custom controls simply because 9 times out of 10 it can be done with a out of the box control that is well documented and supported. But there are times when we have to use or support them so it is worthwhile to discuss.  Make it a great day!

Join me on Facebook

Public Class ucMain
    Public Event MainFired(ByVal button As Integer)
    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        RaiseEvent MainFired(1)
    End Sub
    Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        RaiseEvent MainFired(2)
    End Sub
    Private Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
        RaiseEvent MainFired(3)
    End Sub
End Class

Simple sub control, containing a textbox.

Public Class ucSub
    Public WithEvents maincontrol As ucMain
    Public myNumber As Integer
    Private Sub maincontrol_MainFired(ByVal button As Integer) Handles maincontrol.MainFired
        If button = myNumber Then
            TextBox1.Text = "Main fired"
        End If
    End Sub
End Class

Loading of subcontrol onto form using this code (in loop on i to give three instances for my test)

            Dim c As New ucSub
            c.myNumber = i
            c.maincontrol = UcMain1
            c.Top = 40 * i
            c.Left = 200



  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: