0

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:

enter image description here

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:

enter image description here

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:

enter image description here


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.

ElektroStudios
  • 19,105
  • 33
  • 200
  • 417
  • Have you tried declaring your textbox public withevents instead of friend withevents? – Paul Ishak May 03 '15 at 08:43
  • Why are you trying to add event handlers to properties? Add them to the TextBox1 control and all will be fine. The UC is sort of like a mini-form complete with designer code. Drop a TextBox on the canvas and it will work just like a form. The ctor code is superflous too - since the US is a container there is no need to create one. – Ňɏssa Pøngjǣrdenlarp May 03 '15 at 10:49
  • Because the same problem happens with the "Textbox1" reference (when set as Public Withevents, and browseable), that was the first thing that I tried before asking. thanks for comment. – ElektroStudios May 03 '15 at 10:51
  • I am guessing that what you want is to add handlers *in the form* for the TextBox? If so, that is not how they work. Typically a UC is used to encapsulate several controls which sort of act as one thing and the related implementation details. – Ňɏssa Pøngjǣrdenlarp May 03 '15 at 13:40

1 Answers1

2

I am going to suggest you are not using the UserControl as intended. These are a bit like a mini-form which consumes the events of the controls on them.

Based on Handles MyUserControl.TextBox.TextChanged it appears you want the form to get involved with text changes

Typically, these allow you to create a control made of several releated controls and encapsulate the logic to perfom some task. An example would be a UserControl to define a new Employee or Product. The UserControl would consume all the events for the different steps involved and cough up a finished object at the end. A task based example would be a Search function with several controls for filters and parameters.

What they are not, are glorified Container Controls. One reason for using a UserControl form is isolate the form from those implementation details and encapsulate them for reuse.

That said, you simply have to bubble up any events you wish to expose. Using an actual UserControl, drop your TextBox onto it:

Public Class UserControl1

    Public Event MyTBTextChanged(sender As Object, e As EventArgs)

    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub

    Private Sub TextBox1_TextChanged(sender As Object, 
               e As EventArgs) Handles TextBox1.TextChanged
        RaiseEvent MyTBTextChanged(sender, e)
    End Sub
    ...

If you find that you are having to bubble up more than one or two events (or perhaps any), then you might want to rethink where the code logic is handled or whether a UserControl is the right choice (if lots of events for lots of controls are being exposed, why is there a UC at all?).

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
  • The reason why Im using an UC is because whatever I put inside the OnPaint event of a default TextBox will be skiped since is Windows who draws its borders,then I need to use an UC to draw the borders around a default TextBox ...the child TextBox. Anyways your suggestion of exposing events is one of the things that I clarified Im aware of,because exposing manually each of the child control events are A LOT of events that will require an insane XML docs of thousands of lines to to do it "perfect" following good practices, just is not an option for me, i consider it a "patch". thankyou anyways – ElektroStudios May 04 '15 at 12:37
  • The point of the answer is that exposing child control events ("bubbling up") is a sign of the wrong direction. In order to be complete, an answer sometimes needs to cover things "already mentioned" in the question. Otherwise driveby voters who dont read the Q will DV the A because it doesnt mention this or that. You should ask the *real* question about the TextBox and border etc. – Ňɏssa Pøngjǣrdenlarp May 05 '15 at 13:04
  • `You should ask the real question about the TextBox and border etc` Why?, after a long months of researchs and seeying the same solutions I know the answer is "**An UC is required**" then I come asking a question about the UC and the different problem that I have with it..anyways I asked the question you mentionded,here http://stackoverflow.com/questions/27235587/replace-redraw-a-textbox-3d-border-using-a-custom-color and here http://stackoverflow.com/questions/27230168/override-the-drawing-of-some-net-framework-controls-to-change-its-border-color with no luck.thanks again for your help! – ElektroStudios May 05 '15 at 13:27