Click to See Complete Forum and Search --> : Advice Requested - Designing Controls


MCRoberts
July 14th, 2005, 08:25 PM
Greetings:

I am a little confused about the business of designing controls. I need to get a few definitions and terns straight:

What is the difference between a Control and a Component?

I have seen a lot of examples of Control design on this web site. I'm confused about the best approach to take. I see various starting points being used:

1. Some simply extend an existing control.
2. Some start by deriving from UserControl
3. Others use Control as the base class

It seems clear that extending an existing control is used if you just want to tweak or enhance an existing control. But what is the difference between deriving from Control and UserControl?

Thanks to any respondents in advance.
Mark

MadHatter
July 14th, 2005, 10:39 PM
Component - an item which sits (not always visibly represented) in the designer tray. It can be physically represented on the form in the same way a control is represented (for instance toolbar buttons or a menu) but doesnt have to be (ex: a file watcher). Each control has a "components" collection. These items are not interested in "windows messages" but are usually drawn or used by the form by some other control (or the hosting form). at a minimum it allows someone to set properties on an object in a design view (think of a file watcher. You can set properties and events even though it’s not actually used by the form). Other items which are used sort of like a file dialog browser are components.
Control - is an item which is represented directly on your form. This object is for all intents and purposes a "window" in win32. it receives "windows messages" which drive events and such in the object itself. When you're writing this object you do so in code view only. it does not have the same design experience as a "user control." You can drag items onto the "component" view of the control, but there is no forms designer (only the component view).
UserControl - is a special type of control which is a normal control but gives the developer a canvas with which to aggregate multiple "controls" to make some new conglomerate "control." It looks and feels like a borderless "form." After you've designed the UserControl they typically show up in a tab called "My Controls" in vs.net which allows you to drag & drop them on a form with 0 code.


Different methodologies are employed when creating controls. Typically all windows objects inherit from Control. This is a great base object. it has all the win32 stuff implemented for you so all you need to do is override this or that and go from there. Most items you use at design time (or their "base classes") start here.

You would inherit from this object if you wanted to create something unique. You would inherit from an existing control when you wish to change or extend what it already does, and keeps you from having to re-write a lot of functionality that the control already contains.

You would inherit from Component (which Control does) if you wish to have your object available to the form but not necessarily to interact in the windowing process (makes them lighter weight). Things like menu's and toolbars (some anyway) are usually components. They don’t need to respond to user interaction the same way as say a button--speaking purely on what actually goes on behind the scenes. they are typically handled by the component collections owner (in a menu's case the main form).

You would implement a UserControl if you wanted to make a control which was comprised by other controls & not necessarily inherited from them. for instance if you wanted to make a control which had a label, textbox and button & wanted to call it a "browse for file textbox" you'd probably inherit from UserControl, drag those 3 things onto your designer and roll with it. You need to then expose any properties you want the user to design at design time when using your control. The UserControl object will hide (as all OO objects do) your individual members when used in the designer after you're done creating it. I think the main difference between UserControl and control is the IDesigner used to render the control in composition design mode (as opposed to using it at design time).

Anyway, hope that’s helpful. It’s a very brief explanation but it should get a few questions out of the way.