Usercontrols. Are they worth the effort?

  • Thread starter Thread starter Josh Newman
  • Start date Start date
J

Josh Newman

I have a form that is approximatly 5000 lines of code. I estimate that about
70% of this is designer generated code for my controls on the form.

Would it be worth it to group some of the controls on the form into user
controls to reduce the size of the form?

What else can/should be done to keep the size of forms reasonable?

I have heard that user contorls cause all kind of weird behaviour in the
IDE?
 
Josh Newman said:
I have a form that is approximatly 5000 lines of code. I estimate that
about 70% of this is designer generated code for my controls on the form.

Would it be worth it to group some of the controls on the form into user
controls to reduce the size of the form?

What else can/should be done to keep the size of forms reasonable?

Is the size of the form creating a specific problem? If so, what problem is
it causing? You would then want to make sure that your usercontrol solution
solved this particular problem.

If it isn't causing a specific problem, then you've already got your
solution! :-)

John Saunders
 
I would never create a user control simply to group things on a form.

User controls certainly are worth the hassle. In fact, they're a
wonderful innovation. However, I would use them only when a group of
controls performs a single, simple function that I could state in one
sentence.

For example, I have a complicated user control that allows the user to
enter a normalized or denormalized fraction. It consists of a text box
and three combo boxes, but all work in concert to do a single, simple
thing. I use it wherever I need to have the user enter a fraction that
could be denormalized.

On the other hand, I would never (for example) create a user control to
enter dimensional information for lumber products. That's far too vague
a requirement, and it would be used in too few places.

I also have inherited text boxes that expect only integers, or only
decimal values, or only monetary amounts. Again, they offer
functionality that I will need often in the future, so I put it in a
user control rather than recreating it every time (and possibly getting
it wrong).
 
What do you mean by "size of the form"? Is it a display real estate problem,
a memory problem, a source code problem, or other?
 
I also have inherited text boxes that expect only integers, or only
decimal values, or only monetary amounts. Again, they offer
functionality that I will need often in the future, so I put it in a
user control rather than recreating it every time (and possibly getting
it wrong).

On a related note: is it possible to subclass an existing control to add
functionality to it, _and_ get the forms designer to play nice with your
version of the control, without going to the trouble of creating a whole
user control? For instance, I want to subclass PictureBox to give it more
display modes (that is, I really dislike the default clip-or-stretch
behaviors for images that exceed the dimensions of the PictureBox), but
that's all. I'd like it if, when I drag a PictureBox into my app, I get an
instance of a myPictureBox class, rather than of a
System.Windows.Forms.PictureBox.

Can I do this without explicitly creating a user control just to hold my
subclassed control?
 
Marcos Stefanakopolus said:
On a related note: is it possible to subclass an existing control to add
functionality to it, _and_ get the forms designer to play nice with your
version of the control, without going to the trouble of creating a whole
user control? For instance, I want to subclass PictureBox to give it more
display modes (that is, I really dislike the default clip-or-stretch
behaviors for images that exceed the dimensions of the PictureBox), but
that's all. I'd like it if, when I drag a PictureBox into my app, I get
an instance of a myPictureBox class, rather than of a
System.Windows.Forms.PictureBox.

Can I do this without explicitly creating a user control just to hold my
subclassed control?

How would VS.NET know to substitute your class for the one in the Toolbox?

Is there some difficulty putting your class into the toolbox?

John Saunders
 
The short answer is yes. In fact, that's what I did for my specialized
TextBoxes, but I didn't want to muddy the discussion on this thread
with that detail.

So, you can subclass PictureBox and insert your custom PictureBox on
forms just as you would a normal PictureBox. The only detail is that
you need to place your new PictureBox class in the tool box in Visual
Studio. VS will not "know" to use your PictureBox specialization
whenever you add a PictureBox to your Form, particularly since there
could be multiple specializations (just as I have multiple
specializations of TextBox).

Or, you could cheat like I do and simply insert a normal PictureBox and
then modify the code that the Designer inserts into your Form class.
You have to change it in only two spots: the declaration and the
instantiation. :)
 
Back
Top