0

I have created a class to hold other objects. I need each object to be clickable. The following code works when I click on the objects using the mouse. However, I would like to simulate a mouse click by raising the mouseclick event from another function but I can figure out the syntax.

This is my custom 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

I am trying to raise the mouseClick event on this class from another class but it is not working.

This is my other class that I am trying to use:

 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

I am not getting an error, but it is just ignoring my statement.

John
  • 1,310
  • 3
  • 32
  • 58
  • as far as i know it is not possible to raise an event like MouseClick. are you testing in debug mode? i've found that some errors are just irgnored in debug mode. if possible try to compile it and run it. that should get you some errorinfo. – Index Apr 21 '15 at 21:59
  • 1
    Do you need to actually simulate a mouse click or would it be ok to just call the code that is placed in the mouse click event? If that works for you then I would change your `OnLabelClick' event to be `Public` and then just call that sub instead of `item.mytext.MouseClick()`. – Joe Uhren Apr 22 '15 at 02:19
  • What I ended up doing was using your suggestion Joey. I just call a public function to simulate a mouse click. Thanks. Post your comment as an answer and I'll give you credit. – John Apr 22 '15 at 15:07

2 Answers2

0

Here's an example taken 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
Paul Ishak
  • 1,093
  • 14
  • 19
  • It is also possible to invoke the Button.PerformClick method. – Paul Ishak Apr 22 '15 at 01:43
  • This solution will work to simulate a mouse click as long as the OP doesn't mind that the mouse cursor must be positioned over the item to be clicked. You are missing code to ensure that the mouse cursor is placed on the item to click otherwise when the timer ticks the mouse cursor could be anywhere and not click the right spot. Something like `Cursor.Position = item.Location` at the top of `Timer1_Tick`. – Joe Uhren Apr 22 '15 at 02:15
0

If you don't need to actually simulate a real click but instead need to execute the code for a click event, you can do something like the following:

  1. Change your OnLabelClick event to be Public
  2. Call the OnLabelClick sub from within your highlight function instead of the item.mytext.MouseClick() that you had earlier.
Joe Uhren
  • 1,342
  • 1
  • 13
  • 27