Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How to subclass control?
Message
De
17/02/2007 00:43:23
 
 
À
16/02/2007 14:31:30
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
C# 2.0
OS:
Windows XP SP2
Network:
Windows 2000 Server
Database:
MS SQL Server
Divers
Thread ID:
01184867
Message ID:
01196770
Vues:
32
This message has been marked as the solution to the initial question of the thread.
J'aime (1)
Holger,

Here's the syntax for overriding properties and giving them the ability to be re-set to a Default in the Property Sheet:
public class MyTextBox : TextBox
{
    private string m_MyProperty;

    public MyTextBox
    {
        this.m_MyProperty = "";
    }

    [DefaultValue("")]
    public string MyProperty
    {
        get {return this.m_MyProperty;}
        set {this.m_MyProperty = value;}
    }
    [DefaultValue(typeof(Color), "Window")]
    public override Color BackColor
    {
        get {return base.BackColor;}
        set {base.BackColor = value;}
    }
    [DefaultValue(typeof(Color), "WindowText")]
    public override Color ForeColor
    {
        get {return base.ForeColor;}
        set {base.ForeColor= value;}
    }
}
Setting some of these properties doesn't always work as you would expect them to ... one that comes to mind is "Text" ... you may or may not have problems setting a default for that one, depending on which control you're sub-classing. Also, some properties aren't virtual and can't be overridden. For those properties, you can use "new" instead of "override".

>And another question: Would the changes, made to these properties, appear in the formdesigner, if you drop the control there?

Yes!!

~~Bonnie
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform