Introduction
I have a Class that inherits from UserControl
, inside this class I expose a child TextBox
control:
Public NotInheritable Class MyUserControl: Inherits UserControl
...
''' <summary>
''' Gets the inner TextBox of this user control container.
''' </summary>
<Browsable(True),
EditorBrowsable(EditorBrowsableState.Always),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content)>
<Description("The textbox control.")>
Public ReadOnly Property TextBox As TextBox
Get
Return Me.textbox1
End Get
End Property
<Browsable(False),
EditorBrowsable(EditorBrowsableState.Advanced),
DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)>
Private WithEvents textbox1 As New TextBox With {...}
Public Sub New()
...
Dim container As New ContainerControl
container.Controls.Add(Me.textbox1)
Me.Controls.Add(container)
...
End Sub
...
End Class
Problem
I just would like to be able to use the Handles
statement at design time to let IntelliSense list that child TextBox events in the same way that we can use this feature to list and suscribe to the events of a default TextBox like in this example:
But when I try the same above with my user-contorl, the Handles
statement does not recognize the child TextBox neither its events because the TextBox
property seems not to be visible in that way, but note that I set the visibility of that member as Public
and it is a Withevents
member as I shown above in the introduction.
On the other hand, the TextBox
property of my user-control is accesible manually, I can type the property name and the event name so this event-handler below will compile and works ok:
Private Sub MyUserControl_TextBox_TextChanged(sender As Object, e As EventArgs) _
Handles MyUserControl.TextBox.TextChanged
End Sub
The problem is that with that event-handler added into my source-code, when I return to the Visual GUI Builder of Visual Studio it throws this error:
So I cannot acces the Visual Builder until I remove the event-handler that is handling the child TextBox of my user-control.
The most weird thing is that as I said I cannot list the events using the Handles
statement neither manually suscribe to them because it throws that error, but the events are accesible inside the control's property grid of the visual builder and if I double-click on an event name it generates the event-handler (that I cannot use as I explained), this is just ridiculous:
Question
I'm doing something wrong?, how to fix that visual builder Error?
The most important thing, how I could use the Handles
statement as normally for the child textbox control of my user-control?.
Answer requisites
To easy solve the problem about the Handles
statement and also about the Visual Builder I know that I just could add an additional reference to the child textbox of my user-control in this way:
Private WithEvents tb As TextBox = MyUserControlInstance.TextBox
Private Sub tb_TextChanged(sender As Object, e As EventArgs) _
Handles tb.TextChanged
End Sub
Or else I could expose the events of that child textbox from the "top-level" of my UserControl class, but that will be a nightmare of code-writting because I don't know if that task could be automated.
Those tricky things will not let me to understand and solve the real problem, I think that my question and my problem is clear, I will avoid "solutions" like those that are more a "patch" than a solution.