Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to subclass control?
Message
From
23/04/2008 13:29:27
 
 
To
23/04/2008 13:26:09
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:
01312675
Views:
22
>Would it be foolish to use the UserControl as a wrapper around just a single textbox, so that I could have the benefits of the Desiger surface instead of coding out the class in the way the we have previously discussed?
>
>I could do it for my label controls also.


I wouldn't, but maybe that's just me. <g>

Why do you want to visually sub-class a TextBox anyway? There aren't a whole lot of things that you really need to see visually to design the class. It's just a lousy TextBox ...

~~Bonnie



>
>
>
>
>>Yes, UserControls *are* designed visually. Start by creating a class that is sub-classed from UserControl (actually, you should have your own MyUserControl class to sub-class it from). Then drag/drop your TextBoxes, Buttons, etc. onto it visually in the IDE.
>>
>>~~Bonnie
>>
>>
>>
>>
>>>So next I have started playing with building a custom "User Control" to create a custom control that would have, say, a label, a textbox, and a command button surronded by a rectangle? A single control that is all one object that I can drag-and-drop onto a form. I used a lot of these in FoxPro. It seems like there is still a place for this in .NET.
>>>
>>>It seems to be based on a designer basis, right (please say yes), at least for the visual stuff, and it gives me drag-and-drop design of the control components, properties list, etc. It's much more like what I know (and like) from VFP.
>>>
>>>
>>>Still don't understand why they wouldn't work have made it work this way for single class component design.
>>>
>>>Wish me luck...
>>>
>>>
>>>>>Okay. Okay. Okay. I'll say it... "You were right!"
>>>>
>>>>LOL ... well, I'm not one to say "I told you so" ... <g>
>>>>
>>>>>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.
>>>>
>>>>I know it's a pain to have to do it this way, but once you know this "trick", then it's not really all that bad. I'm glad that you see now why you have to do it this way.
>>>>
>>>>~~Bonnie
>>>>
>>>>
>>>>>
>>>>>
>>>>>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
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform