他のオブジェクトを保持するクラスを作成しました。各オブジェクトをクリック可能にする必要があります。次のコードは、マウスを使用してオブジェクトをクリックすると機能します。ただし、別の関数からmouseclickイベントを発生させてマウスクリックをシミュレートしたいのですが、構文を理解できます。
これは私のカスタムクラスです:
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
このクラスで別のクラスから mouseClick イベントを発生させようとしていますが、機能していません。
これは私が使用しようとしている私の他のクラスです:
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
エラーは発生していませんが、私のステートメントを無視しているだけです。