如何从另一个类中的事件处理程序设置属性

时间:2015-07-28 21:34:15

标签: vb.net winforms class event-handling

使用link中列出的代码填充表单,因为通常有30-60个标签和相应的按钮,这些按钮填充了列表中存储的文本。执行此操作的代码示例如下,其中包含" stock"事件处理程序包括在内。

Public Class clsControlArray

Public Sub New(ByVal host)
    HostForm = host
End Sub

Public Sub AddNewControls(ByVal TextList As List(Of String))
  ' Calculate number of Rows and Columns that fit on the form using
  ' size and offset information not shown here.
  For Col = 0 To Cols - 1
    For Row = 0 To Rows - 1
      If TextList.Count = 0 Then
        Exit Sub
      End If
      ' Create object and add event handler
      Dim aButton As New System.Windows.Forms.Button()
      AddHandler aButton.Click, AddressOf ButtonClickHandler
      ' Add the control to the internal list
      ButtonList.Add(aButton)
      ' Calculate the top and left of the current control to be added
      CtrlTop = Top + OffsetHeight * Row
      CtrlLeft = Left + OffsetWidth * Col
      ' Set Properties of the control (subset shown here)
      aButton.Top = CtrlTop
      aButton.Left = CtrlLeft
      aButton.Text = TextList.First.ToString 
      aButton.Name = "Button" & ButtonList.Count
      HostForm.Controls.Add(aButton)
      TextList.Remove(TextList.First.ToString)
    Next Row
  Next Col
End Sub

Public Sub ButtonClickHandler(ByVal sender As Object, ByVal e As  _
  System.EventArgs)
    MessageBox.Show("you have Button with Tag: " & _
                    CType(CType(sender, System.Windows.Forms.Button).Tag, String) & _
                    Chr(10) & " with the Text: " & _
                    CType(CType(sender, System.Windows.Forms.Button).Text, String))
End Sub
End Class

调用代码如下。

Public Class frmCategories
  Dim Cat As New List(Of String)
  Dim Rec As New clsStatusRecord
  Dim Buttons As New clsControlArray(Me)

  ' Cat is populated here (not shown)
  Public Sub New()
    Buttons.AddNewControls(TextList:=Cat)
  End Sub
End Class

我想在Button.Name属性中存储Button.Name属性并调用下一个表单,而不是仅仅在单击按钮时显示带有信息的消息框。我从未使用过控制数组,也从未编写自定义事件处理程序。我已经读了几天了,我还是不知道如何完成这项工作。请帮帮忙?

0 个答案:

没有答案
相关问题