Component Text Property
-
I have a custom component that is inheriting from System.Windows.Forms.UserControl. I would like to have the nice and consistent property Text rather than the property Caption. Defining
**public string Caption**
works fine and I can view the property in design mode as expected. But if I replace the property definition with**public override string Text**
, it doesn’t allow me to see the property. What is the nuance that I’m missing? Thanks db -
I have a custom component that is inheriting from System.Windows.Forms.UserControl. I would like to have the nice and consistent property Text rather than the property Caption. Defining
**public string Caption**
works fine and I can view the property in design mode as expected. But if I replace the property definition with**public override string Text**
, it doesn’t allow me to see the property. What is the nuance that I’m missing? Thanks dbtry public new string Text...
-
try public new string Text...
That's a bad idea. There is no reason to hide the
Text
property of the parent class and this will screw-up a lot of the implementation if the variables, fields, or properties to not specifically reference the derivativeUserControl
Type.Microsoft MVP, Visual C# My Articles
-
I have a custom component that is inheriting from System.Windows.Forms.UserControl. I would like to have the nice and consistent property Text rather than the property Caption. Defining
**public string Caption**
works fine and I can view the property in design mode as expected. But if I replace the property definition with**public override string Text**
, it doesn’t allow me to see the property. What is the nuance that I’m missing? Thanks dbIt looks like you're declaring a field, not a property. There is a difference. Also,
UserControl
in .NET 1.1 attributes the override with theBrowsableAttribute
and theEditorBrowsableAttribute
to merely hide it from designers and source code editors. It's still there and you can still call it. They removed it only from view becauseUserControl
s are typically container in whichText
make little sense. To show it again, declare your property like so:[Browsable(true)]
[EditorBrowsable(EditorBrowsableState.Always)]
public override string Text
{
get { return base.Text; }
set { base.Text = value; }
}You'll then see it again in both the designer and source code editor.
Microsoft MVP, Visual C# My Articles
-
That's a bad idea. There is no reason to hide the
Text
property of the parent class and this will screw-up a lot of the implementation if the variables, fields, or properties to not specifically reference the derivativeUserControl
Type.Microsoft MVP, Visual C# My Articles
And the world will end... Nice to hear from you again!
-
And the world will end... Nice to hear from you again!
It may. The massive power outage on US's eastern seaboard was caused by a simple bug. The solution is very simple and examining the
UserControl
class using something like ildasm.exe would resolve the problem. The member is being overridden simply to hide it from designers and code editors. It was still available all along even without having to override it in order to show it again.Microsoft MVP, Visual C# My Articles
-
It may. The massive power outage on US's eastern seaboard was caused by a simple bug. The solution is very simple and examining the
UserControl
class using something like ildasm.exe would resolve the problem. The member is being overridden simply to hide it from designers and code editors. It was still available all along even without having to override it in order to show it again.Microsoft MVP, Visual C# My Articles
Probably written by a Californian. Heath, I sincerely think that you need to RELAX. It may be the way I type, but you seem to treat things as way too important. There is a radio show here in L.A. (yes, Lala land, home of Arnold the guvernor) where there is a guy who complains about everything and the question that everyone keeps asking him, and I ask you now, is "Who hurt you?". PS: I forgot to admit that the bug in the electrical grid was mine, but being a Californian it was meant to run in the electical grid of Uzbekistan, and make the lights that were left on read out, "HS rules...", damn if I got it wrong again.
-
Probably written by a Californian. Heath, I sincerely think that you need to RELAX. It may be the way I type, but you seem to treat things as way too important. There is a radio show here in L.A. (yes, Lala land, home of Arnold the guvernor) where there is a guy who complains about everything and the question that everyone keeps asking him, and I ask you now, is "Who hurt you?". PS: I forgot to admit that the bug in the electrical grid was mine, but being a Californian it was meant to run in the electical grid of Uzbekistan, and make the lights that were left on read out, "HS rules...", damn if I got it wrong again.
je_gonzalez wrote: Heath, I sincerely think that you need to RELAX I'm not the only one. I wasn't offended by your remark. A lot of things I post in this forum are for posterity in case some n00b ever considers searching this forum first. :laugh: That made me laugh. Oh well, here's to hoping... :beer:
Microsoft MVP, Visual C# My Articles
-
je_gonzalez wrote: Heath, I sincerely think that you need to RELAX I'm not the only one. I wasn't offended by your remark. A lot of things I post in this forum are for posterity in case some n00b ever considers searching this forum first. :laugh: That made me laugh. Oh well, here's to hoping... :beer:
Microsoft MVP, Visual C# My Articles
Let's see I was taught not to say RTFM to clients anymore two years before you were born. Yeap, things have changed, now is RTFF, as manuals have given way to forums.
-
It looks like you're declaring a field, not a property. There is a difference. Also,
UserControl
in .NET 1.1 attributes the override with theBrowsableAttribute
and theEditorBrowsableAttribute
to merely hide it from designers and source code editors. It's still there and you can still call it. They removed it only from view becauseUserControl
s are typically container in whichText
make little sense. To show it again, declare your property like so:[Browsable(true)]
[EditorBrowsable(EditorBrowsableState.Always)]
public override string Text
{
get { return base.Text; }
set { base.Text = value; }
}You'll then see it again in both the designer and source code editor.
Microsoft MVP, Visual C# My Articles
Thanks. I was only using the Category, Description and Default attributes. Adding the Browsable and EditorBrowsable attributes did the trick. Now that I've got it showing, testing has shown a couple other nuances. 1. Using Default doesn't do anything, it always contains the default object name as the default Text, regardless of the Default attribute. Even setting a line after
InitializeComponent();
asText="";
, didn't do anything. 2. I would've been able to live with the Text property not being blank upon dropping the component on the form but not remembering its value just ain't cool. Initially I tried to use base.Text as the storage for the Text value (seemed the appropriate thing to do). Close the form and open it, the Text value is gone. Tried using a private variable instead, got the same result. Tried using_new_
rather than_override_
in both scenarios of base.Text and a private variable. The component still will not remember the value of Text. None of this affected Default either. While answering this, can you tell me if there is a trick to finding info in the Help and/or SDKs? I suspect the answer you provided would've been there but, while there is probably everything I need to know at my finger tips, asking the right question seems to be the issue (thus RTFF seems to be a better solution than RTFM). Unfortunately, due to corporate bankrupcies I no longer have the team with which to collaborate (previously coded in Delphi) and some of the nuances of .Net have been somewhat frustrating to decipher. Thanks again. -
Thanks. I was only using the Category, Description and Default attributes. Adding the Browsable and EditorBrowsable attributes did the trick. Now that I've got it showing, testing has shown a couple other nuances. 1. Using Default doesn't do anything, it always contains the default object name as the default Text, regardless of the Default attribute. Even setting a line after
InitializeComponent();
asText="";
, didn't do anything. 2. I would've been able to live with the Text property not being blank upon dropping the component on the form but not remembering its value just ain't cool. Initially I tried to use base.Text as the storage for the Text value (seemed the appropriate thing to do). Close the form and open it, the Text value is gone. Tried using a private variable instead, got the same result. Tried using_new_
rather than_override_
in both scenarios of base.Text and a private variable. The component still will not remember the value of Text. None of this affected Default either. While answering this, can you tell me if there is a trick to finding info in the Help and/or SDKs? I suspect the answer you provided would've been there but, while there is probably everything I need to know at my finger tips, asking the right question seems to be the issue (thus RTFF seems to be a better solution than RTFM). Unfortunately, due to corporate bankrupcies I no longer have the team with which to collaborate (previously coded in Delphi) and some of the nuances of .Net have been somewhat frustrating to decipher. Thanks again.These are all design-time attributes. The
DefaultAttribute
is there to facilitate resetting the property value. Using a method that returns abool
calledShouldSerialize_PropertyName_
also facilitates this goal. Again, though, don't usenew
. Only use this when you want to hide a member. For instance, if you usenew
, any code that refers to your class as aControl
(whereText
is defined as a virtual method) or any other derivative ofControl
(before your class), then the virtualText
is used. Only when a variable is declared as your Type will yourText
be used. Since much of the Windows Forms implementation refers to all child controls asControl
types, yourText
accessors wouldn't be called. To note (since you obviously read the little flame war that shouldn't have been), I don't completely advocate RTFM. RTFM goes well with RTFF, but I'm a strong believer in teaching a man to fish, if you know what I mean. Reading the docs and understanding them (like the mostly consistent naming conventions, how C# relations to .NET and IL relates to the CLR, etc.) is very important, but there's also times when people get stuck - even the best have brain farts now and then. The best thing to do is just skim the class library docs some time. Get to know where everything is. Also, whenever you have a question about a specific class, the help Index is your friend. Just type it in. Don't specify certain properties, though, likeScrollableControl.Text
because it doesn't exist as a defined method ofScrollableControl
- it's inherited. It will be in the member documentation forScrollableControl
but not in the index. It's just the way their help built system works (and even those like NDoc[^] which I occassionally contribute to and is very popular). Once you get to know where all the classes are roughly and how everything is named, it's not too hard finding what you need if you can't remember or even if you don't know exactly what you're looking for.Microsoft MVP, Visual C#