I have my own control:
public class newControl : Control
{
}
There is a Text
property, but there is not a TextAlign
property. For example, I need this property similar to the TextAlign
property of a Button
, but I don't want inherit it from a button class.
So can I inherit only TextAlign
property? If yes, how?
aaaa I think generally no and it is impossible dude .You can not inherit a property :D
First, consider inheriting from System.Web.UI.WebControls.WebControl, which has more properties that correspond to styling, such as
CssClass
andAttributes
.Rather than using the
TextAlign
property, I would recommend simply adding a CSS class to your page, and use setCssClass
property, which is on the base classWebControl
.Alternatively, you could set the text alignment by doing the following (but a CSS class would be cleaner):
Of course, you could always add your own property that writes the correct CSS to the
Attributes
collection as well.Yes, you can just add it. The built in enumeration is called
ContentAlignment
:What you do with this property is up to you now.
Note that I added some attributes for how the control is used in the
PropertyGrid
. TheDefaultValue
attribute does not set the value of the property, it just determines whether or not the property is displayed in bold or not.To display the text using your
TextAlign
property, you would have to override theOnPaint
method and draw it: