Creating a User Control - Peter Duniho

20-Mar-08 12:54:27
What do you mean "this fails"?  Are you saying that the Designer is  =

actually inserting code that either fails to compile, or fails to execut=
e  =

(i.e. throws an exception)?

As far as the line itself goes, it's just setting the AutoScaleMode  =

property.  If you don't want it set to Font, you can change it back in t=
he  =

Designer itself (in the Properties pane), rather than commenting out the=
=

line.

Finally, please don't use the phrase "user control" to describe a custom=
=

control not inherited from the UserControl class.  I agree that the name=
=

k  =

with and it'd be better to not use that name or anything similar to it t=
o  =

describe something that's not actually a UserControl.

Pete
reply
 
 

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
reply
 

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
reply
 

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
reply
 

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
reply
 

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
)
reply
 

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
reply
 
promotion
Silverlight    WPF    WCF    WWF    LINQ   
JavaScript    AJAX    ASP.NET    XAML   
C#    VB.NET    VB 6.0    GDI+    IIS    XML   
.NET Generics    Anonymous Methods    Delegate   
Visual Studio .NET    Expression Blend    Virus   
Windows Vista    Windows XP    Windows Update   
Windows 2003 Server    Windows 2008 Server   
SQL Server    Microsoft Excel    Microsoft Word   
SharePoint    BizTalk    Virtual Earth   
.NET Compact Framework    Web Service   

"Everything" RSS / ATOM Feed Parser
How to send and receive messages through message queuing in .Net
How to Read text file as database
SQL Server 2005 Paging Performance Tip
Display code of web page.
Fully Scalable Excel File Importer class for .net using Microsoft Jet driver
Generic Chart Color Manager class that can be used for any charts
Helper class to style the infragistics wingrid
Using Reflection to detemine as Assembly Info in and out.
Helper class to play with Window (Owners and position)
Resolving displayname from the culture using the XmlLanguage and LanguageSpecificStringDictionary class