User Control in TableLayoutPanel Won't Handle Mouse Clicks

When I click on a user control located in a tablelayoutpanel, it appears that the event is never raised.

The program creates multiple workingblockUCs and displays them correctly, but when I put a breakpoint on sub blockUC_mouseclick, the breakpoint never gets hit.
class form2
 Friend WithEvents WorkingBlockUC As BlockUC

Sub drawblock(ByVal coordx As Integer, ByVal coordy As Integer, ByVal gridnum As Integer, ByVal wbc As BlockCollection)
        wbc.getblock(coordx, coordy)
WorkingBlockUC = New BlockUC
        TableLayoutPanel1.Controls.Add(WorkingBlockUC, coordx, coordy)

Private Sub blockUC_Mouseclick(ByVal sender As Object, ByVal e As  _
       System.Windows.Forms.MouseEventArgs) Handles WorkingBlockUC.MouseClick
        Dim row As Integer = 0
        Dim verticalOffset As Integer = 0

        For Each h As Integer In TableLayoutPanel1.GetRowHeights()
            Dim column As Integer = 0
            Dim horizontalOffset As Integer = 0

            For Each w As Integer In TableLayoutPanel1.GetColumnWidths()
                Dim rectangle As New Rectangle(horizontalOffset, verticalOffset, w, h)

                'If rectangle.Contains(e.) Then
                '    CMSBlock.Show(Me, pt)
                '    clearselected()
                '    drawcollection(thisgridnum, wbc)
                '    'Trace.WriteLine([String].Format("row {0}, column {1} was clicked", row, column))
                '    Exit Sub
                'End If

                horizontalOffset += w
                column += 1

            verticalOffset += h

            row += 1

    End Sub

Open in new window

Who is Participating?
Mike TomlinsonMiddle School Assistant TeacherCommented:
You have:

    Friend WithEvents WorkingBlockUC As BlockUC

The "WithEvents" paradigm can only handle ONE object (whatever "WorkingBluckUC" was last assigned).

To handle multiple instances you'd have to use AddHandler() instead (and get rid of the WithEvents declaration).
alukesAuthor Commented:
Dead on.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.