RL said:
I found that the user controls also grow to huge size.
Let me say this about the original question: I have never separated
parts of a unit (be it a form or a user control) from the rest just
because it was growing large. The only thing that's really growing large
here is the code that's generated automatically by the IDE, I don't see
what's particularly bad about that. In VS 2005, using partial classes,
that code will be in a different file, so you don't see it at all. In VS
2003, it's usually hidden in a region, so you don't normally see it
either. This code has to go *somewhere* - if you create a lot of UI
elements, you'll get a lot of instructions for it.
Of course, user controls are a good mechanism to cut down on the size of
a single unit, but they also produce overhead. For every control you
create, you'll have to define an interface and you'll have to define
exactly how this control will work with others. Using the Mediator
pattern, you don't get rid of these decisions or this work, you only
push them to a different place.
Please, I'm not saying it's bad to do this. I'm only saying that the
decision for each single user control should IMHO be made when there's a
clear purpose to that control - usually that it's possible to reuse that
control elsewhere - and when the additional effort is justified. Using
user controls, the number of code lines in your whole project will not
get smaller. On the contrary, it will grow, and you get to write a lot
of the additional code manually. So, don't do it just because the
InitializeComponent method is getting long.
If you have many (a dozen or so) controls grouped as a user control, and the
controls are bound to a database, do you include the data adapter and
dataset in the user control? If not, how did you bind to the individual
controls from outside the user control?
Sure, that's what you do. Generally, you can create "proxy properties"
on the user control that simply read and set their values to/from
any/all of the "inner controls". Or your user control can have its very
own properties - if you're mainly creating the control as a reusable
component in its own right, you'll probably encounter more of the latter.
How do you debug a complex user control? I developed mine in a separate
Solution file to avoid issues with the form designer. My recompile-retest
cycle required building the control in one solution, switching to the other
Solution, removing the old user control from the client form, remving the
old control from the toolkit, adding the new control to the toolkit and
adding the new control to the client form. This can't be the only way!
I have user controls in the same project as a form and in a different
project from the form, neither of which is a problem. If these projects
are parts of the same solution, it's easy to develop and debug the user
controls. I'm not aware of any specific "issues with the form designer",
though - it takes some getting used to (make sure to keep your projects
compiled, and there's this bug in VS 2005 beta 2 if you have your form
open while running the debugger), but it works for me.
Oliver Sturm