vb.neteventsonclickraiseevent

How do I raise a button click event for my class


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.


Solution

  • 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.