UserControl vs. Standard Class

  • Thread starter Thread starter Richard Brown
  • Start date Start date
R

Richard Brown

Can anyone give me a good argument one way or another?

I have an 'address' set of fields that are used in various situations (a
client has an address, a destination has an address, etc).
These fields are all normalized into a seperate database table from the
client and destination tables.

My question is, which is better to create, a User Control, or a generic
class that will add controls to a form directly, applying its common event
handlers, etc? Keep in mind, that things such as 'Suite' or 'Apartment
Number' are stored in the client or destination table, since multiple
clients or desitnation 'offices' may be at the same block address (this is
for a geocoding application).

A User Control only seems to be useful if you plan on placing it on a form
with the form designer, however, it seems that you would have to expose all
of the internal control objects anyway if you wanted the outside container
to react to specific events within the control.
 
Hi Richard,

A UserControl, as you say, is a design-time control. If you do not need to
place your AddressControl on a form at design-time I wouldn't use that
approach.

This leaves your create-an-AddressControl class.

However, if the control that is created is always the same (or there's
only a limited set of choices) then you could create an AddressControl which
innherits from the preferred control, eg TextBox, Label.

I couldn't say which would be better without more info.

Regards,
Fergus
 
Richard,
Generally I would have both.

An Address class that contains the Domain Logic for an Address. Client and
Destination would have an Address Property of type Address.

Then I would have an AddressControl that displays the information found in
the Address class, it would also have an Address Property of type Address.

The form would bind the current objects Address Property to the controls
Address property.

The control itself would bind individual properties of the address object to
the various controls.

Alternatively (additionally?) the AddressControl would have properties that
the form could interact with. It would not expose actual controls. It would
expose new events for any contained controls. Think encapsulation. The fact
that the suite number is a text box today, may change to an updowncontrol
tomorrow.

Public Class AddressControl

Public Event SuiteChanged As EventHandler
Public Event ApartmentNumberChanged As Event Handler

Public Property Suite() As String

Public Property ApartmentNumber() As String

Private WithEvents textSuite As TextBox
Private WithEvents textApartmentNumber As TextBox

End Class

Hope this helps
Jay
 
I am actually going to refer to comments in your post and also in Jay's
post.

Well, the address 'control' would actually be made up of several text boxes,
so inheriting from one particular control won't work. But, this 'control
class', would also have an optional map button, if the address is a mapped
address (mailing addresses are not mapped as they could be PO boxes).

For Jay, encapsulation is a good thing... BUT, the suite and apt number text
is actually crossing a slight boundary, since it is not directly related to
the mapped address. (Has to do with roof-top geocoding, only the street
number, street, etc are used). Also, one problem I did leave out, in
several areas, but not all, the first line of the address is also used as a
lookup filter, combined with other fields in passenger or destination. So
that would have to be addressed also, and if other fields are exposed for
lookups, then the exposed methods would have to be changed.

Keep in mind, this is all for internal program use, but I'd like to keep it
as readable and understandable as possible. I'm sure I can come up with a
hundred pieces of functionality to add to the control for various
circumstances, but I have a very large app to write, not just this one
control/implementation.
 
Hello,

Fergus Cooney said:
A UserControl, as you say, is a design-time control. If
you do not need to place your AddressControl on a form
at design-time I wouldn't use that approach.

I think it's a big advantage that UserControls are design time controls.
UserControls should be used to create a new control that consists of
multiple controls ("composite control").

Regards,
Herfried K. Wagner
 
Richard,
For Jay, encapsulation is a good thing... BUT, the suite and apt number text
is actually crossing a slight boundary, since it is not directly related to
the mapped address. (Has to do with roof-top geocoding, only the street
number, street, etc are used). Also, one problem I did leave out, in
So you saying you have 2 (or more) kinds of Addresses. One that includes
Suite & Apt Number and one that does not?

If that is the case I would have an Address object (that does not have Suite
& Apt Number), then I would have a second class derived from this that
includes Suite & Apt number.

I may actually have 3 address objects. AddressBase, MailingAddress &
MappedAddress. Where MailingAddress & MappedAddress inherit from
AddressBase. AddressBase would be an abstract base class (MustInherit),
while MailingAddress & MappedAddress are concrete class (offer full
implementation).

I would probably still have only a single AddressControl that used
AddressBase, AddressBase would be able to indicate when there are extra
'fields' present, such as a 'HasSuite' property. You know a tradeoff between
pure OOP & getting the job done ;-)

Instead of a HasSuite property, I would consider having AddressControl use a
Strategy Pattern (offered from AddressBase) when it is initializing &
displaying the controls, so as to know to show or hide the 'optional map
button', or show or hide the Suite & Apartment controls).

Basically with HasSuite the AddressBase is the strategy, while in the second
I would have a MailingAddressStategy & MappedAddressStategy, I would need a
better idea of the differences between the two address types before I
decided which was better. Of course with Refactoring (www.refactoring.com) I
can change my mind later.
Keep in mind, this is all for internal program use, but I'd like to keep it
as readable and understandable as possible. I'm sure I can come up with a
hundred pieces of functionality to add to the control for various
circumstances, but I have a very large app to write, not just this one
control/implementation.
Which is where I find encapsulation helps. All logic dealing with Address is
in the address object, all logic dealing with displaying an address is in
the AddressControl. All logic unique to MailingAddress is in the
MailingAddress object.

I usually favor strong OOP practices and I find my code is readable &
understandable & Maintainable.
several areas, but not all, the first line of the address is also used as a
lookup filter, combined with other fields in passenger or destination. So
that would have to be addressed also, and if other fields are exposed for
lookups, then the exposed methods would have to be changed.
I'm not following this: If the form has an Address object, this Address
object is bound to the AddressControl, and the Address.FirstLine is used in
loopups. Why not just use Address.FirstLine instead of
AddressControl.FirstLine?

Hope this helps
Jay
 
Thanks for the info, over course, we *all* change our minds sooner or
later....

But, the last part is a little confusing...
I'm not following this: If the form has an Address object, this Address
object is bound to the AddressControl, and the Address.FirstLine is used in
loopups. Why not just use Address.FirstLine instead of
AddressControl.FirstLine?

Ok, now I'm so confused that I don't remember the question....
Oh yeah... I think I understand what your saying... based on your previous
recommendation of having the address control AND the address class,
basically you are saying encapsulate the UI in the UserControl and
everything else in the Address class. So to perform the lookup
functionality, access the address class that is bound to the UserControl/UI.

Am I on the right track with your thinking?
 
Richard,
recommendation of having the address control AND the address class,
basically you are saying encapsulate the UI in the UserControl and
everything else in the Address class.
Am I on the right track with your thinking?
Yes

One small note, thinking about it, your UserControl really only needs to
expose the Address property, as the parent control will already have the
object with all the other properties, or you can get the properties off of
the usercontrols' address property...

Hope this helps
Jay
 
Ah ha! I remember the main reason I was having to figure this out....

Ok, say you have a number of fields that make up a 'form'.
Ok, those fields could be used in an MDI child, or they can be used in a tab
page, or they can be used in a modal dialog.

So, for example, I have a passenger data entry screen, with the Address
UserControl we've talked about.
Now, should I make that passenger data entry screen a form, a user control
or a class that spits out controls?
I dont *think* you can nest UserControls inside UserControls (code wise, not
container wise), so wouldn't the control 'spitting' (I like that term for
some reason, pardon me) be a better approach? That way, you could 'spit'
controls to a blank form, or to a tab page, or to an MDI child and still
have the class handle it all for you.
 
Richard,
Ok, say you have a number of fields that make up a 'form'.
Ok, those fields could be used in an MDI child, or they can be used in a tab
page, or they can be used in a modal dialog.
Now, should I make that passenger data entry screen a form, a user control
or a class that spits out controls?

I would make the passenger data entry 'screen' either a form or a user
control. If I needed it 'hosted' in different containers (form, tab page,
MDI child, modal dialog) I would lean toward user control. As the user
control can be placed any of them.

If I made the Passenger Data Entry a user control, I would consider
inheriting from a custom UserControl class, or implement a specific
interface, such that the Data Entry Control would allow letting its
container know what tool bar buttons, menu items, caption should be used in
the enclosing 'frame' (frame here is Window/Form not necessarily GroupBox)

The above is partially explained in:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwinforms/html/reaworapps2.asp

I saw partially as you can go much further than the article takes you.
I dont *think* you can nest UserControls inside UserControls (code wise, not
container wise),

The code for a user control is 'part' of that user control.

You can place user controls on user controls on user controls. In other
words user controls can be containers for other user controls.

Within your source you can have one user control nested inside another,
however I would not do that, especially here. You loose designer support,
and you are not gaining anything that I can tell. I normally only nest class
definition when the nested class is an implementation detail for the outer
class (the Node class for a LinkedList class that holds the data & link info
for each element in the LinkedList).

This is valid VB.NET:

Public Class UC1
Inherits System.Windows.Forms.UserControl

Public Class UC2
Inherits System.Windows.Forms.UserControl

' other stuff omitted.

End Class

' other stuff omitted.

End Class

For the project I am currently working on. I have a handful of user
controls, that are placed a couple of other user controls, these two user
controls are placed on a third, this third is placed on a tab page. So I
have something like:

Form
TabControl
TabPage
UC1
UC2
ExGroupBox
UC4
UC5
ExGroupBox
UC6
UC6
UC3
ExGroupBox
UC7
UC8
TabPage
UC1
UC2
ExGroupBox
UC4
UC5
ExGroupBox
UC6
UC6
UC3
ExGroupBox
UC7
UC8

Remember Encapsulation, all the logic for UC7 is in UC7, it is not co
mingled in code with UC8, ExGroupBox & UC3. Of course UC3 has code that
interacts with UC7 & UC8, but it is not UC7 or UC8 specific code, it is UC3
code.

In case you were wondering ExGroupBox derives from GroupBox and overrides
the OnLayout event to do some custom arranging of the contained controls. In
the above I use it like a standard GroupBox, not a user control.
so wouldn't the control 'spitting' (I like that term for

Having an object that 'spits' out other objects is a Builder Pattern (of
sorts), and yes that is always an option, however I suspect it will be more
work, without any real gain. The problem as I see it where are you event
handlers, are they co mingled with something else (introducing coupling and
losing cohesion).
controls to a blank form, or to a tab page, or to an MDI child and still
have the class handle it all for you.
Remember a UserControl can be placed (dynamically (aka spit out)) onto a
blank form, a tab page, a MDI child, another User Control, any other
container control). Also the code for the UserControl is encapsulated within
that user control.

Hope this helps
Jay

Richard Brown said:
Ah ha! I remember the main reason I was having to figure this out....

Ok, say you have a number of fields that make up a 'form'.
Ok, those fields could be used in an MDI child, or they can be used in a tab
page, or they can be used in a modal dialog.

So, for example, I have a passenger data entry screen, with the Address
UserControl we've talked about.
Now, should I make that passenger data entry screen a form, a user control
or a class that spits out controls?
I dont *think* you can nest UserControls inside UserControls (code wise, not
container wise), so wouldn't the control 'spitting' (I like that term for
some reason, pardon me) be a better approach? That way, you could 'spit'
controls to a blank form, or to a tab page, or to an MDI child and still
have the class handle it all for you.
<<snip>>
 
Back
Top