[英]VB.net custom button control doesn't work properly
I am trying to create a custom button control in VB.Net so that I can add my own text field to it and position it where I want to. 我试图在VB.Net中创建一个自定义按钮控件,以便可以向其中添加自己的文本字段并将其放置在所需的位置。 But when I do the button no longer acts like a button.
但是当我这样做时,按钮不再像按钮一样。 None of my addHandlers work and when I click the button, nothing happens.
我的addHandlers都不工作,当我单击按钮时,什么也没有发生。 But it does show my text properly.
但这确实显示了我的文字。 It just loses all of it functionality as a button.
它只是失去了按钮的所有功能。
Public Class myButton
Inherits System.Windows.Forms.Button
Public myText As New Label
Public Sub New(TextString)
myText.Text = TextString
myText.BorderStyle = BorderStyle.FixedSingle
myText.TextAlign = ContentAlignment.MiddleCenter
Me.Controls.Add(myText)
End Sub
End Class
What am i missing? 我想念什么? Thanks.
谢谢。
In your custom control you have added a label which is now on top of the button. 在自定义控件中,您已在按钮顶部添加了标签。 So you are clicking on label control, not in Button.
因此,您单击的是标签控件,而不是Button。 Please try below snippet which is working for me
请尝试以下对我有用的代码段
Public Class myButton
Inherits System.Windows.Forms.Button
Public myText As New Label
Public Event OnButtonClick As EventHandler
Public Sub New(TextString As String)
myText.Text = TextString
myText.BorderStyle = BorderStyle.FixedSingle
myText.TextAlign = ContentAlignment.MiddleCenter
AddHandler myText.Click, AddressOf OnLabelClick
Me.Controls.Add(myText)
End Sub
Private Sub OnLabelClick(sender As Object, e As EventArgs)
RaiseEvent OnButtonClick(Me, e)
End Sub
End Class
On Form load 在表格上
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim customButton As New myButton("vimal")
AddHandler customButton.OnButtonClick, AddressOf OnCustomButtonClick
Me.Controls.Add(customButton)
End Sub
Private Sub OnCustomButtonClick(sender As Object, e As EventArgs)
MsgBox("Clicked")
End Sub
I believe the problem you are running into is that the label is getting displayed over your button. 我相信您遇到的问题是标签正显示在按钮上方。 Labels have their own action handlers for click events, so when you are clicking the 'button' you are really clicking on the label.
标签具有它们自己的单击事件操作处理程序,因此,当您单击“按钮”时,您实际上是在单击标签。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.