Creating a User Control - Peter Duniho |
20-Mar-08 01:42:17
|
Well, I'd say that if the Designer is doing that all on its own, that's
definitely a bug.
I've never seen it do that, and I've definitely subclassed existing
controls as well as written my own. So I think it's possible that it's
either a bug that specifically happens only in certain cases, or possibly
in a situation where the control hasn't been subclassed in a way that's
visible or usable by the Designer (though how that might happen exactly
I'm not sure).
If someone can post specific, reliable steps that can be taken in the IDE
to cause that to happen (along with the version of VS used, of course),
that would be useful and interesting information. I personally can't do
anything about it obviously, but I'm curious to see it happen anyway. :)
Pete |
 |
| |
Creating a User Control - Peter Duniho |
20-Mar-08 04:57:55
|
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.
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.
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 |
 |
| |
Creating a User Control - Dom |
21-Mar-08 06:04:15
|
Everytime I create a user control, (such as extending another control,
like TextBox), the IDE sticks into the Designer file, the following:
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
In momst cases, this fails. Why is it there? I'm in the habit of
just commenting it out.
Dom |
 |
| |
Creating a User Control - zack |
21-Mar-08 06:04:27
|
On Mar 20, 12:54=A0pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
=A0
I've also seen this. The project fails to build with an error that
says the AutoScaleMode property is not a member of the object.
he =A0
=A0
=A0
=A0
=A0
=A0 |
 |
| |
Creating a User Control - Dom |
21-Mar-08 06:04:49
|
za got it exactly right. When you build, the error message says that
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
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?
On Mar 20, 1:42=A0pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
f
=A0
=A0
=A0
=A0
=A0
=A0
=A0
=A0
=A0
) |
 |
| |
Creating a User Control - Dom |
26-Mar-08 11:44:29
|
Thanks. That took care of everything. I'd like to get my hands on
the guy who came up with the name "UserControl".
Dom
On Mar 20, 4:57=A0pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
=A0
=A0
ou =A0
t =A0
=A0
=A0
=A0
=A0
=A0
=A0
=A0
ol =A0
he =A0
=A0 |
 |
| |