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.
[.net] UserControls - Overriding the Text property
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
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
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
As far as the source goes, no. I actually used a disassembler called Reflector
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
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement