如果您的新用户控件只有一个控件并继承文本框,那么您可以像平常一样使用属性编辑器或手动使用 AddHandler 附加事件。如果事情比这更复杂,我可能会创建一个面板并将您的新文本框放入其中,那么访问事件会容易得多。
Public Class myUserControl
Inherits TextBox
End Class
Public Class Form1
Public Sub New()
InitializeComponent()
AddHandler MyUserControl1.TextChanged, AddressOf MyUserControl_textChanged
End Sub
Private Sub MyUserControl_textChanged(sender As Object, e As EventArgs)
Throw New NotImplementedException
End Sub
End Class
或者,如果您的 UserControl 中有多个自定义文本框,您可以创建一个自定义事件并在您的 UserControl 上的常见 TextChanged 事件中触发该事件,并在您的主窗体中处理它。
Public Class UserControl1
Public Event TextChange(sender As Object, e As EventArgs)
Private Sub MyUserControl_TextChanged(sender As System.Object, e As System.EventArgs) Handles MyUserControl1.TextChanged, MyUserControl2.TextChanged
RaiseEvent TextChange(sender, e)
End Sub
End Class
Public Class Form1
Public Sub New()
InitializeComponent()
End Sub
Private Sub UserControl11_TextChange(sender As System.Object, e As System.EventArgs) Handles UserControl11.TextChange
MsgBox(CType(sender, TextBox).Name, MsgBoxStyle.Information)
End Sub
End Class