如何为 class 引发按钮点击事件

How do I raise a button click event for my class

我创建了一个 class 来容纳其他对象。我需要每个对象都可以点击。当我使用鼠标单击对象时,以下代码有效。但是,我想通过从另一个函数引发 mouseclick 事件来模拟鼠标单击,但我可以弄清楚语法。

这是我的习惯class:

Public Class MyTab

        Inherits System.Windows.Forms.Panel
        Public myText As New Label

        Public Event OnButtonClick As EventHandler

        Public Sub New(TextString)
            myText.Font = CustomFont.GetInstance(Main.main_font_size_up3, FontStyle.Regular)
            myText.ForeColor = Color.FromArgb(255, 0, 0, 0)
            myText.BackColor = Color.Transparent
            myText.Text = TextString
            Dim textSize As Size = TextRenderer.MeasureText(TextString, myText.Font)
            myText.Width = textSize.Width + 15
            myText.Height = textSize.Height + 6
            myText.UseCompatibleTextRendering = True
            myText.BorderStyle = BorderStyle.None
            myText.Name = "tab_" & TextString

            Me.Width = textSize.Width + 10
            Me.Height = 30
            Me.BackColor = Color.White

            myText.TextAlign = ContentAlignment.MiddleCenter
            AddHandler myText.Click, AddressOf OnLabelClick ' Listen for the click on the new label
            AddHandler myText.MouseClick, AddressOf OnLabelClick ' Listen for the click on the new label
            Controls.Add(myText)

       End Sub

        Private Sub OnLabelClick(sender As Object, e As EventArgs)
            RaiseEvent OnButtonClick(Me, e)
            gray_out_tabs()
            sender.BackColor = Color.FromArgb(255, 255, 255, 255)
        End Sub

        Private Sub gray_out_tabs()
            ' gray out all tabs
            For Each item As Object In tab_holder.Controls
                If TypeOf item Is MyTab Then
                    item.myText.BackColor = Color.FromArgb(255, 200, 200, 200)
                End If
            Next
        End Sub

    End Class

我正在尝试从另一个 class 引发此 class 上的 mouseClick 事件,但它不起作用。

这是我正在尝试使用的另一个 class:

 Public Class myTabHolder

        Inherits Panel

        Public Function highlight(which)

            For Each item As Object In tab_holder.Controls
                If TypeOf item Is MyTab Then
                    If item.mytext.name = "tab_" & which.ToString Then
                        item.mytext.MouseClick() ' <-- not working
                    End If
                End If
            Next
            Return Nothing
        End Function

    End Class

我没有收到错误,但它只是忽略了我的声明。

这是一个例子 from Pinvoke:

' Clicks left mouse button in current coordinates

Imports System.Runtime.InteropServices
Imports System.Threading

Public Class Form1

     Const MOUSEEVENTF_LEFTDOWN As UInteger = &H2 '0x0002
     Const MOUSEEVENTF_LEFTUP As UInteger = &H4 '0x0004

     <DllImport("user32.dll")> _
     Private Shared Sub mouse_event(ByVal dwFlags As UInteger, ByVal dx As UInteger, ByVal dy As UInteger, ByVal dwData As UInteger, ByVal dwExtraInfo As Integer)
     End Sub

     'Public Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal dwData As Long, ByVal dwExtraInfo As Long)

     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
     mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
     Thread.Sleep(100)
     mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
     End Sub

End Class

如果您不需要实际模拟真正的点击,而是需要执行点击事件的代码,您可以执行如下操作:

  1. 将您的 OnLabelClick 事件更改为 Public
  2. highlight 函数中调用 OnLabelClick 子函数,而不是之前的 item.mytext.MouseClick()