0

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
    Firstly, I would create a user control rather than using `Panels`. I would then have the user control handle the `Click` events of the `Labels` and then raise its own event. That event would then be handled by the `DataGroupBoxControl` and it would raise its own event. – jmcilhinney Oct 26 '19 at 01:05
  • By the way, notice how the class you're inheriting is named `GroupBox` rather than `GroupBoxControl`? So why is your class named `DataGroupBoxControl`? You're not using `PanelControl` or `LabelControl` classes are you? – jmcilhinney Oct 26 '19 at 01:06
  • I am not. Fairly new to this. Looking into user controls. – Johnny O Oct 26 '19 at 12:15

1 Answers1

0

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
Edin Omeragic
  • 1,948
  • 1
  • 23
  • 26