za got it exactly right. When you build, the error message says that
"AutoScaleMode property is not a member of the object", but the
Designer placed it in there.
Here are the steps I take if I want to extend a TextBox control:
1. I add a UserControl (i checked the designer.cs file, and you can
already see the offending line)
2. In the code window, I change "class MyTextBox : UserControl" to
"class MyTextBox : TextBox"
3. I build a constructor: "public MyTextBox () : base () {...}
As I said, right after step 1, I get the line about AutoScaleMode.
What am I doing wrong?
> >> > In momst cases, this fails. Why is it there? I'm in the habit of
> >> > just commenting it out.
[quoted text clipped - 23 lines]
>
> Pete
> za got it exactly right. When you build, the error message says that
> "AutoScaleMode property is not a member of the object", but the
[quoted text clipped - 4 lines]
> 1. I add a UserControl (i checked the designer.cs file, and you can
> already see the offending line)
Here's your problem. As I mentioned before, the name "UserControl" is
poorly chosen. It is definitely not what you want to do if you want to
extend some other control class. Choose "Custom Control" instead when you
are adding your new class to your project. This will create a class that
inherits the Control class rather than the UserControl class.
The UserControl class is appropriate when you want to create a new control
by compositing a bunch of other controls, using the designer to drag and
drop those other controls into your UserControl. It is _not_ a
general-purpose "custom control" base class and it's an error to use it
that way.
> 2. In the code window, I change "class MyTextBox : UserControl" to
> "class MyTextBox : TextBox"
Right. The property exists in the UserControl class (actually, an
intermediate base class for that class) and so is valid in the Designer
.cs file when the Designer wrote that file. But when you change the
inheritance so that your class now inherits TextBox instead, the property
is no longer part of the inherited class and thus you get the compiler
error.
Basically, you're telling the Designer one thing to start with, and then
changing that thing later without any way for the Designer to know it's
been changed.
> 3. I build a constructor: "public MyTextBox () : base () {...}
>
> As I said, right after step 1, I get the line about AutoScaleMode.
>
> What am I doing wrong?
You're using the UserControl class as your base class when in fact you
just want the Control class as your base class. By specifying the Control
class, you can then safely modify the class declaration to inherit a
different class (such as TextBox) that itself inherits Control.
I wish the Designer had some way to specify the actual base class you
want. IMHO it would make this whole process simpler and safer. But the
fundamental error here is yours, in misunderstanding the purpose of the
UserControl class. Hopefully the above explains that in a useful way.
Maybe Zack was making the same mistake when he ran across the compiler
error.
Pete
Dom - 24 Mar 2008 16:10 GMT
Thanks. That took care of everything. I'd like to get my hands on
the guy who came up with the name "UserControl".
Dom
> > za got it exactly right. When you build, the error message says that
> > "AutoScaleMode property is not a member of the object", but the
[quoted text clipped - 51 lines]
>
> Pete