vb.net - VB WindowsForm 自定义类事件处理程序问题
问题描述
我创建了一个自定义类 (DataGroupBoxControl),它基本上是一个 GroupBox,里面有一个或多个面板。每个面板并排放置两个标签,如下图所示。 在此处输入图像描述
该对象允许将 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
解决方案
在创建 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
推荐阅读
- google-apps-script - getWidth 和 getHeight 方法无法正常运行
- typescript - 打字稿对象可能未定义
- amazon-web-services - 是否可以使用 AWS 胶水爬虫重新分区数据?
- amazon-ec2 - Strongswan没有建立连接
- java - 使用 CompletableFuture 回调有性能优势吗?
- sql - 在批量插入期间生成随机主键
- firebase - Firebase 规则安全警告
- events - Rust - 将事件监听器添加到 webassembly 游戏
- javascript - 如何阻止css样式表在分支结帐时自动修改?
- facebook - 缺少以下必需属性:og:url、og:type、og:title、og:image、og:description、fb:app_id