473,700 Members | 2,692 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

UserControl vs. Standard Class

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.
Nov 20 '05 #1
9 3343
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
Nov 20 '05 #2
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 ApartmentNumber Changed As Event Handler

Public Property Suite() As String

Public Property ApartmentNumber () As String

Private WithEvents textSuite As TextBox
Private WithEvents textApartmentNu mber As TextBox

End Class

Hope this helps
Jay

"Richard Brown" <rb****@easylif t.org> wrote in message
news:uY******** ******@TK2MSFTN GP09.phx.gbl...
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.

Nov 20 '05 #3
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.

"Fergus Cooney" <fi******@tesco .net> wrote in message
news:eL******** ******@tk2msftn gp13.phx.gbl...
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

Nov 20 '05 #4
Hello,

"Fergus Cooney" <fi******@tesco .net> schrieb:
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
--
MVP ∑ VB Classic, VB.NET
http://www.mvps.org/dotnet
Nov 20 '05 #5
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 MailingAddressS tategy & MappedAddressSt ategy, 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.FirstLi ne is used in
loopups. Why not just use Address.FirstLi ne instead of
AddressControl. FirstLine?

Hope this helps
Jay
"Richard Brown" <rb****@easylif t.org> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. .. 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.

"Fergus Cooney" <fi******@tesco .net> wrote in message
news:eL******** ******@tk2msftn gp13.phx.gbl...
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


Nov 20 '05 #6
Thanks for the info, over course, we *all* change our minds sooner or
later....

But, the last part is a little confusing...
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.FirstLi ne is used

in loopups. Why not just use Address.FirstLi ne 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?
Nov 20 '05 #7
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

"Richard Brown" <rb****@easylif t.org> wrote in message
news:O5******** *****@TK2MSFTNG P09.phx.gbl... Thanks for the info, over course, we *all* change our minds sooner or
later....

But, the last part is a little confusing...
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.FirstLi ne is used

in
loopups. Why not just use Address.FirstLi ne 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?

Nov 20 '05 #8
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.
"Jay B. Harlow [MVP - Outlook]" <Ja********@ema il.msn.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
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 ApartmentNumber Changed As Event Handler

Public Property Suite() As String

Public Property ApartmentNumber () As String

Private WithEvents textSuite As TextBox
Private WithEvents textApartmentNu mber As TextBox

End Class

Hope this helps
Jay

"Richard Brown" <rb****@easylif t.org> wrote in message
news:uY******** ******@TK2MSFTN GP09.phx.gbl...
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.


Nov 20 '05 #9
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/de...eaworapps2.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.UserContr ol

Public Class UC2
Inherits System.Windows. Forms.UserContr ol

' 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" <rb****@easylif t.org> wrote in message
news:eX******** *****@TK2MSFTNG P11.phx.gbl... 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>>
Nov 20 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2034
by: DraguVaso | last post by:
Hi, I'm having some weird behaviour, and I can't find any solution to this. I made a UserControl (uclCommands) which contains a Toolbar (ToolBar1) with Modifiers = Public. I put this usercontrol on my form and call it Ucl1. In the Property Window in the Windows Forms Designer I change Ucl1.ToolBar1.ButtonSize to a bigger value.
1
3028
by: jralford | last post by:
Hi, Another question from a C# newbie: I have a usercontrol class that includes a customized control. Now, if I create an instance of the class, and click on it, a click event is raised for the customized control within the class. This is good. What I also want is to raise a click event for the class itself. For instance,
0
1529
by: N. Demos | last post by:
Hello, I have a custom usercontrol, of which I have two instances of in my aspx page. Both the usercontrol and page have codebehind. In the page codebehind, I want a member variable for each usercontrol. My problem is that I keep getting a compiler error on the usercontrol variables in my page codebehind (BC30002: Type 'CityGeoPosSelectClass' is not defined.) For whatever reason, my usercontrol class is not being declared in the page...
11
5298
by: Crirus | last post by:
I need to derive the Windows.Forms.Control 2 times so I design a class like this Public Class BMControl Inherits System.Windows.Forms.UserControl Public Class MapControl Inherits BMControl
1
239
by: sotto | last post by:
Hi, how can i make a usercontrol that inherits from the system.windows.forms.button instead of the forms.usercontrol ? I want to add a fiew features to my button and still be able to drag it on to the form with the ide i got working code, just a class that inherits from system.windows.forms.button but then i need to do all the design by code
4
1705
by: Michael Maes | last post by:
Hi, I have a UserControl with some "Children" (e.g. one ComboBox & one Label). I add the UserControl on a Form. If I perform a recursive scan through that Form's Control-Collection the ..HasChildren-Method scans the Controls within the UserControl. This is a behavior I want to ommit. * Is there a way to hide the Controls contained within a UserControl so that UserControl.HasChildren will always return False? *
41
4303
by: JohnR | last post by:
In it's simplest form, assume that I have created a usercontrol, WSToolBarButton that contains a button. I would like to eventually create copies of WSToolBarButton dynamically at run time based on some initialization information obtained elsewhere. Basically, I'm going to create my own dynamic toolbar where the toolbarbuttons can change. I'm not using the VB toolbar because of limitations in changing things like backcolor (I can't get...
12
2204
by: Joe | last post by:
Hello All: Do I have to use the LoadControl method of the Page to load a UserControl? I have a class which contains three methods (one public and two private). The class acts as a control server. It "serves" back the required control (either WebControl or UserControl) based on the contents of an xml file. The code in the webform places each control in a TableCell. My problem is that the control server works as far as returning the...
2
2265
by: Joe | last post by:
Is it possible to inherit from a UserControl? If I try my user control class is not recognized. Thanks, Joe
0
8714
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
8641
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9203
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9060
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8958
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
4396
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4650
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3082
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2379
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.