[.net] UserControls - Overriding the Text property

Started by
14 comments, last by Barguast 19 years, 8 months ago
rofl, simple problem, I just had to look at the UserControl source.

anyways, MSDN is a great source... it's what i use 99% of the time. the other 1% is me poking around their source code.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Advertisement
This is a really minor problem, but I'd like to know why it's happening (just out of curiousity).

As you can see in the code, the Text property is meant to be set to 'Flat Button' during the control's construction. However, whenever I create a control, the text defaults to 'flatButton#' where # is a number that makes the text unique. It seems to be setting the Text property to the name of the control.

This, again, is something that goes away if I use a custom property name instead of the Text property. I've looked on MSDN, and through those fricken attributes, but I can't see why this is happening. I also tried putting the 'this.Text = "Flat Button";' line in the InitializeComponent function, but this didn't work either. Anyone?

Also, Washu said that you looked through the UserControl source - did you download this from somewhere?

Thanks

Using Visual C++ 7.0
Ahh, that's VS default method for naming items. You can't override it, so...you're stuck with it.

As far as the source goes, no. I actually used a disassembler called Reflector

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Answers to the questions that were left unanswered...

Q: Why is it working with a new property Caption and not with an overriden Text ?

A: Because the existing Text caption has attributes which prevent it to work. The new Caption property has no attributes, and the default values are what you need.

Q: Why is my property not visible ?

A: The attribute Browsable allows public properties to be visible in the property grid. It is true by default, but Text has it set to false so you have to change it explicitly.

Q: Why aren't my property changes persisted in the code ?

A: DesignerSerializationVisibility lets you decide if a property should be persisted or not (i.e. is just for viewing and side-effects of assignment). By default it's 'Visible', which means that it's persisted in the initialization code. But again, the Text property has it set to 'Hidden', which means it's not persisted. Note there's a third possibility: 'Content'. Check it on MSDN.

Q: What was that funny name then ?

A: the name you saw was the default value given in the class constructor. It goes away with a new property because the new property not assigned in the class constructor.

Q: Why didn't putting 'this.Text = "Flat Button";' in the InitializeComponent function solve the problem ?

A: The InitializeComponent function is overwritten by the IDE every time it saves your form in the code. Don't make any change there, it will be lost next time the form designer saves its work.

Q: One last piece of advice ?

A: Yes, if you want to do other stuff concerning design time, you really should dig into attributes, since they control lots of design time things. By the way, the other mentionned attributes (Bindable and EditorBrowsable) should not be related to your problem. You should be able to just ignore them.

Have a nice day,
jods
Quote:Original post by jods
Answers to the questions that were left unanswered...

Q: One last piece of advice ?

A: Yes, if you want to do other stuff concerning design time, you really should dig into attributes, since they control lots of design time things. By the way, the other mentionned attributes (Bindable and EditorBrowsable) should not be related to your problem. You should be able to just ignore them.

Have a nice day,
jods


While they are not directly related, all of the non-user controls have those properties set to values set to true. And if he decides to get into more advanced things (like DB queries) he will need to be able to bind them.

The EditorBrowsable attribute affects Intellisense.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Well, thanks for clearing that up. I'll know to look at the attributes first in future.

Thanks again
Using Visual C++ 7.0

This topic is closed to new replies.

Advertisement