VB WindowsForm Custom Class Event Handler Issue

51 Views Asked by At

I created a custom class (DataGroupBoxControl) that is basically a GroupBox with one or more Panels inside. Each Panel holds two Labels side by side as pictured below. enter image description here

The object allows a DataTable to be passed into it, which is what determines how many rows of Panels are created and displayed. I would like the user to be able to click on one of the Panel Labels and do 'something'.

Here is the procedure inside the class that creates the Labels.

Public Class DataGroupBoxControl
Inherits GroupBox

Public DataLabels As New List(Of DataLabelControl)

Public Sub BindData(ByVal ds As DataTable)
    Dim colItem As Integer = 0
    For Each col As DataColumn In ds.Columns
        DataLabels.Add(New DataLabelControl)
        For Each row As DataRow In ds.Rows
            DataLabels(colItem).TitleLabel.Text = col.ColumnName & " " & _Separator
            DataLabels(colItem).TitleLabel.Name = col.ColumnName
            If IsNumeric(row.Item(colItem)) Then
                If row.Item(colItem).ToString.IndexOf(".") <> -1 Then
                    DataLabels(colItem).ValueLabel.Text = Decimal.Parse(row.Item(colItem)).ToString("c")
                Else
                    DataLabels(colItem).ValueLabel.Text = row.Item(colItem)
                End If
            End If
            DataLabels(colItem).ValueLabel.Name = row.Item(colItem)
            DataLabels(colItem).Top = (colItem * DataLabels(colItem).Height) + 20
            DataLabels(colItem).Left = 5
            Me.Controls.Add(DataLabels(colItem))
            Me.Height = Me.Height + DataLabels(colItem).Height
            DataLabels(colItem).Show()
        Next
        colItem += 1
    Next
End Sub

Where and how do I create a Label Click Event Handler? And then access that event from my main form with the following object:

Public AccountsGroupBox As New DataGroupBoxControl
1

There are 1 best solutions below

0
Edin Omeragic On

Before creating DataLabelControl use AddHandler to attach event handler eg. to TitleLabel control, then you can listen for events and raise new event that can be handled in parent control.

Public Class SomeForm
    Private WithEvents AccountsGroupBox As New DataGroupBoxControl

    Private Sub AccountsGroupBox_ItemClick(
              sender As Object, 
              args As ItemClickEventArgs) Handles AccountsGroupBox.ItemClick

    End Sub
End Class

Public Class ItemClickEventArgs
    Inherits EventArgs
    Public Property Control As Object
End Class

Public Class DataGroupBoxControl
    Inherits GroupBox

    Public Event ItemClick(sender As Object, args As ItemClickEventArgs)

    Public DataLabels As New List(Of DataLabelControl)

    Private Sub OnLabelClick(sender As Object, args As EventArgs)
        RaiseEvent ItemClick(Me, New ItemClickEventArgs With {.Control = object})
    End Sub

    Public Sub BindData(ByVal ds As DataTable)

        For Each col As DataColumn In ds.Columns

            Dim control = New DataLabelControl
            AddHandler control.TitleLabel.Click, AddressOf OnLabelClick

            DataLabels.Add(control)

            ' ...
        Next
    End Sub
End Class