Groups | Blog | Home
all groups > asp.net building controls > march 2007 >

asp.net building controls : Removing a property from a custom control derived from TextBox


mc
3/10/2007 5:33:31 PM
I have created a Custom control which automatically grows a TextBox to
fit the text that is being entered, consequently this control must be a
MultiLine TextBox. I would like to remove the "TextMode" Property, the
closest I have achieved so far is to change the property to a Get only one.

Is there a better solution?

TIA


MC

-------------------------

public class AutoResizeTextBox : System.Web.UI.WebControls.TextBox
{
public override TextBoxMode TextMode
{
get
{
return base.TextMode;
}
}

<snip>Rest of Code</snip>
stcheng NO[at]SPAM online.microsoft.com
3/12/2007 12:00:00 AM
Hello MC,

As for hidding a property(function) in base class, so far the .net
framework programming language (C# and VB.NET) doesn't provide a direct
means to do this. What you can get is use a "new" keyword (in C#) to
redefine a property(method) and them use some .NET specific attribute to
decorate it.

For example ,you can use the "BrowsableAttribute" to make your control's
"Text" property invisible in IDE designer's property windows. You can also
use "ObsoleteAttribute" to make the property unusable(report error at
compile time if you try using it). e.g.

======================

public class MyTextBox :TextBox
{
public MyTextBox()
{

}


[Obsolete("This property is no longer used in this control", true)]
[Browsable(false)]
public new string Text
{
get { return "not used..."; }
}


}
===========================

#ObsoleteAttribute Class
http://msdn2.microsoft.com/en-us/library/system.obsoleteattribute.aspx

#BrowsableAttribute Class
http://msdn2.microsoft.com/en-us/library/system.componentmodel.browsableattr
ibute.aspx

Hope this helps some.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.



mc
3/12/2007 12:00:00 AM
Over the weekend I'd spotted the EditorBrowsable and Browsable
attributes, but I've not come across the Obsolete attribute. That along
with the new keyword does the job nicely.

It's been pointed out elsewhere that someone can cast my control as a
TextBox and still access the property.

Thank you for not launching into a philosophical OO rant about how bad
it is to try and remove an inherited property!

Thanks


MC

[quoted text, click to view]
stcheng NO[at]SPAM online.microsoft.com
3/12/2007 10:29:33 AM
You're welcome ;-)

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
AddThis Social Bookmark Button