Properties in UserControls

mooman_fl

Centurion
Joined
Nov 3, 2002
Messages
193
Location
Florida, USA
I am wanting to add the Public property Text() to my usercontrol. It obviously can't be added as a regular property. When I try adding it the normal way I am given an error saying it Shadows an overridable method in a base class.

So I tried this:

Visual Basic:
Public Overrides Property Text() As String
    Get
        Return strHeaderText
    End Get
    Set(ByVal Value As String)
        strHeaderText = Value
        Header.Text = strHeaderText
    End Set
End Property

This seems to work fine when changing the property programmatically (i.e. It shows up in the intellisense list) but the property doesn't show up at designtime in the property list.

What am I doing wrong? Also a brief explanation as to why it is wrong will help me from making the same mistakes later.
 
Hmmm... just changed it to:

Visual Basic:
Public Shadows Property Text() As String
    Get
        Return strHeaderText
    End Get
    Set(ByVal Value As String)
        strHeaderText = Value
        Header.Text = strHeaderText
    End Set
End Property

All that seemed to do is break it. It still doesn't show at designtime in the IDE property list. It does still show in intellisense. However using it programmatically no longer changes the text at all.

Still needing help with this.
 
Maybe this will help better demonstrate the problem. I am trying to use the Text() property to change the text on the Header control. Header is just a normal label control. My project is attached below.
 

Attachments

Nevermind. Thanks for trying to help. I figured it out. Shadowing the property works but I had to save everything, build the project, then close down and start a new project. Once the control was added to that project it would show up and everything worked.

For some reason this is another one of those things that just wouldn't show in the startup project I was using while making the control.
 
The reason is that all controls already have a Text property, you really don't have to make your own. What you have to do is override it and call back to the base class to set and get it, and use a Browsable(True) attribute on it to make it show in the property grid.
 
Once again divil thank you. That is a bit different that what I am doing at the moment and I have just noticed that what I have works off and on.

Using the example code I posted about could you give me an example of what you are talking about?
 
Visual Basic:
<Browsable(True)> _
Public Overrides Property Text() As String
    Get
        Return MyBase.Text
    End Get
    Set(ByVal Value As String)
        MyBase.Text = Value
    End Set
End Property

Abolish your strHeaderText variable, and just use Me.Text instead. The Browsable attribute I used there is in System.ComponentModel.
 
Back
Top