Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to subclass control?
Message
From
22/04/2008 23:22:46
 
 
To
22/04/2008 12:35:50
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
C# 2.0
OS:
Windows XP SP2
Network:
Windows 2000 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
01184867
Message ID:
01312541
Views:
23
Okay. Okay. Okay. I'll say it... "You were right!"

It took me a LONG time to 'get' this (no pun intented), but I owe you all the credit for helping me figure this out. Everything you said is exactly correct, but I just had to try it my way, and learn why it would not work.

One word of caution to others... You've got to make sure your private field setiings EXACTLY match the [DefaultValue] attributes or you'll get code in the form's InitializeComponent() method, and then it won't work correctly.


Here's my final code for anyone else who goes down this path:
public class MyTextBox : TextBox
{
    private Font _font = new Font("Microsoft Sans Serif", 12.0F, FontStyle.Bold);
    private Color _forecolor = Color.Blue;
    private Color _backcolor = Color.White;
    
    public MyTextBox() //-- Constructor 
    {
        Font = _font;
        ForeColor = _forecolor;
        BackColor = _backcolor;
    }
    [DefaultValue(typeof(Font), "Microsoft Sans Serif, 12pt, style=Bold")]
    public override Font Font
        {
            get { return base.Font; }
            set { base.Font = value; }
        }
    [DefaultValue(typeof(Color),"Blue")]
    public override Color ForeColor
        {
            get { return base.ForeColor; }
            set { base.ForeColor = value; }
        }
    [DefaultValue(typeof(Color), "White")]
    public override Color BackColor
        {
            get {return base.BackColor;}
	    set {base.BackColor = value;}
	}
>>I'm sorry to be so persisent about this, but I want to tell you that I made the class without the Get, Set and DefaultValue stuff, and it works just fine... When I change the font color or size in the class, and then re-build project and run the form, it comes out just like the class changes. No Get. No Set. No DefaultValue. It just works.
>>
>>Now, to see the changes in the form while in the IDE, you will have to close and re-open the form. But at any time, if I change the font in the class, the textbox on the form automatically updated to match the form, when you run it, or re-open it. Again, No Get. No Set. No DefaultValue.

>
>No problem, Matt. Persistence is a good thing. =0)
>
>OK, so now ... drag another TextBox to your Form in the IDE. Then go look at the designer-generated code and tell me what you see. All of a sudden, all those properties will now be showing up there. OK, well, that still looks ok ... until you go and change a property in your TextBox base class and now it will *not* be propagated into that Form. Try it, you'll see immediately what I mean. Sorry to be such a spoil-sport. <g>
>
>~~Bonnie
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform