Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Creating a User Control

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dom - 20 Mar 2008 16:25 GMT
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
Peter Duniho - 20 Mar 2008 17:54 GMT
> Everytime I create a user control, (such as extending another control,
> like TextBox), the IDE sticks into the Designer file, the following:
[quoted text clipped - 3 lines]
> In momst cases, this fails.  Why is it there?  I'm in the habit of
> just commenting it out.

What do you mean "this fails"?  Are you saying that the Designer is  
actually inserting code that either fails to compile, or fails to execute  
(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 the  
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  
"UserControl" for that class was poorly chosen, but it's what we're stuck  
with and it'd be better to not use that name or anything similar to it to  
describe something that's not actually a UserControl.

Pete
zacks@construction-imaging.com - 20 Mar 2008 18:27 GMT
> > Everytime I create a user control, (such as extending another control,
> > like TextBox), the IDE sticks into the Designer file, the following:
[quoted text clipped - 7 lines]
> actually inserting code that either fails to compile, or fails to execute  
> (i.e. throws an exception)?

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.

> 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 the  
[quoted text clipped - 8 lines]
>
> Pete
Peter Duniho - 20 Mar 2008 18:42 GMT
>> > In momst cases, this fails.  Why is it there?  I'm in the habit of
>> > just commenting it out.
[quoted text clipped - 6 lines]
> 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.

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
Dom - 20 Mar 2008 21:43 GMT
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
Peter Duniho - 20 Mar 2008 21:57 GMT
> 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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.