【问题标题】:VB WindowsForm Custom Class Event Handler IssueVB WindowsForm 自定义类事件处理程序问题
【发布时间】:2020-02-22 05:54:20
【问题描述】:

我创建了一个自定义类 (DataGroupBoxControl),它基本上是一个包含一个或多个面板的 GroupBox。 每个面板并排放置两个标签,如下图所示。 enter image description here

该对象允许将 DataTable 传递给它,这决定了创建和显示多少行面板。 我希望用户能够单击其中一个面板标签并执行“某事”。

这是类中创建标签的过程。

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

在哪里以及如何创建标签点击事件处理程序? 然后使用以下对象从我的主窗体访问该事件:

Public AccountsGroupBox As New DataGroupBoxControl

【问题讨论】:

  • 首先,我将创建一个用户控件,而不是使用Panels。然后我会让用户控件处理LabelsClick 事件,然后引发它自己的事件。然后,该事件将由 DataGroupBoxControl 处理并引发自己的事件。
  • 顺便说一下,注意你继承的类是如何命名为GroupBox而不是GroupBoxControl?那么为什么你的班级命名为DataGroupBoxControl?您没有使用PanelControlLabelControl 课程吗?
  • 我不是。对此相当陌生。查看用户控件。

标签: vb.net onclick delegates event-handling addhandler


【解决方案1】:

在创建 DataLabelControl 之前,使用 AddHandler 附加事件处理程序,例如。到 TitleLabel 控件,然后您可以侦听事件并引发可以在父控件中处理的新事件。

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

【讨论】:

    猜你喜欢
    • 2012-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多