Link to home
Start Free TrialLog in
Avatar of netsmithcentral
netsmithcentralFlag for United States of America

asked on

Where to create child control objects - property accessors (get) or CreateChildControls

I know the .NET system is designed for custom controls to create their child control methods in the CreateChildControls method, and then call EnsureChildControls() before accessing any of their properties, like the following example:
Protected Overrides Sub CreateChildControls()
            _txtSearch = New TextBox()
            Me.Controls.Add(_txtSearch)
        End Sub

        Public Property Text As String
            Get
                EnsureChildControls()
                Return _txtSearch.Text
            End Get
            Set(ByVal value As String)
                EnsureChildControls()
                _txtSearch.Text = value
            End Set
        End Property

Open in new window


I don't see why this is necessary.  Couldn't we just use a property accessor model, create controls there, and add them to the controls collection in CreateChildControls?  That would eliminate the need for using/remembering to call EnsureChildControls() on property access!  FYI, I have tested this method and it works fine.
Private _txtSearch As TextBox = Nothing
        Private ReadOnly Property txtSearch As TextBox
            Get
                If _txtSearch Is Nothing Then
                    _txtSearch = New TextBox()
                End If

                Return _txtSearch
            End Get
        End Property

        Protected Overrides Sub CreateChildControls()
            Me.Controls.Add(txtSearch)
        End Sub

Open in new window


Is there some reason I'm missing to use the EnsureChildControls model instead of the property accessor model?
Avatar of Member_6283346
Member_6283346

Hi! CreateChildControl is not for creating controls like objects, but place for adding child controls in control tree (Controls collection). Because if you add controls to control tree too late (after load event) they can behave unexpectedly.
For example you can init your textbox just when you declare it and then add it to Controls in CreateChildControls:

Dim _txtSearch As TextBox = New TextBox

Protected Overrides Sub CreateChildControls()
            Me.Controls.Add(_txtSearch)
        End Sub

EnsureChildControls in just way to make sure that CreateChildControls was called, it is sometimes used in properties, as you mentioned, but it is not necessary (because sooner or later CreateChildControl will called automatically).
Hi,

Say if you want to set the Text of the text box from outside, how can you do that with Property accessor model? again you have to call something like this,

Public Property Text As String
         
            Set(ByVal value As String)
               Me.EnsureChildControls()
               DirectCast(Controls(1), TextBox).Text = value.ToString()
            End Set
        End Property

Avatar of netsmithcentral

ASKER

I use the property accessor model on my controls, not on their properties.  Then, if I want to access the values in code, I call the control property like so (notice the second code example in my original post has a Private modifier on the property):
'The internal control property initialized with the property
        'accessor model
        Private _txtSearch As TextBox = Nothing
        Private ReadOnly Property txtSearch As TextBox
            Get
                If _txtSearch Is Nothing Then
                    _txtSearch = New TextBox()
                End If

                Return _txtSearch
            End Get
        End Property

        'The external text property
        Public Property Text As String
            Get
                Return txtSearch.Text
            End Get
            Set(value As String)
                txtSearch.Text = value
            End Set
        End Property

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Member_6283346
Member_6283346

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial