Equivalent to VCL's DataModule

E

Edward Diener

In Borland's VCL ( Visual Component Library ), one can use a DataModule
into which one can drop non-visual components from the RAD designer. The
DataModule is a design-time visual container for non-GUI components,
which is never seen visually at run-time as a GUI control. An
application or Dll can have any number of datamodules in it, each with
its own name. One can also manually add any non-GUI data to any
particular DataModule. For an application at run-time, datamodule
instances for each visual datamodule are automatically created just like
forms, while for a Dll at run-time it is the programmer's responsibility
to create datamodule instances for each visual datamodule created at
design time.

I have found the ability to use datamodules as visual containers to
non-GUI components in an application or Dll to be very useful.
Admittedly a datamodule is nothing more than a class which contains
instances of non-GUI components and possibly other non-GUI fields, but I
like the ability to manipulate it at design time, and as a means of
separating the data portion of an application or Dll from the GUI portion.

Does .NET have any equivalent to this idea, so that non-visual
components can be created at design time without having to drop such a
component on a Windows or Web form. ?
 
C

Clive Dixon

Sounds like you could be thinking of a "component class". In VS 2003 if I
right click on my project, select Add -> Add Component and select Component
Class. VS then gives you a C#/VB/C++ class with a "designer page" onto which
you can drag drop components and set their properties in the same way as for
UI controls on a form. Is this the kind of thing you're looking for?
 
E

Edward Diener

Clive said:
Sounds like you could be thinking of a "component class". In VS 2003 if I
right click on my project, select Add -> Add Component and select Component
Class. VS then gives you a C#/VB/C++ class with a "designer page" onto which
you can drag drop components and set their properties in the same way as for
UI controls on a form. Is this the kind of thing you're looking for?

Bravo ! Yes. I knew MS had to be smart enough to include something like
this in .NET. I guess I should have found it previously, but thanks for
bringing it to my attention and seeing this is what I was asking for.

The name is a bit misleading to me. I would have preferred it to be
called a component container or even a data container since I am
guessing I can add non-component value data manually to it also.
 
S

Stephany Young

Why is it misleading?

It is a class that inherits Component. Nothing more, nothing less.
 
E

Edward Diener

Stephany said:
Why is it misleading?

It is a class that inherits Component. Nothing more, nothing less.

You are right. I guess the ability that it has to visually contain other
components fooled me into thinking it was something special.

So I will ask a couple of more questions.

Can any component derived class serve as a container for other
components if it implements System.ComponentModel.Component's
constructor which takes a System.ComponentModel.IContainer as a parameter ?

Unlike VCL's DataModule all of the contained components are declared
private in the class, which means that they are not accessible outside
of the class. What waa the reason for that ?

I noticed that my component will only visually accept non-GUI components
to be embedded within it in the IDE. That is what I want, but I was
wondering what controls that, especially as the
System.ComponentModel.IContainer accepts any IComponent interface ?
 
C

Clive Dixon

Can any component derived class serve as a container for other
components if it implements System.ComponentModel.Component's constructor
which takes a System.ComponentModel.IContainer as a parameter ?

If the component is to be a container in itself, I would have thought it
would have to either implement IContainer itself or have a Container object
to be used as a container for the second level of components, rather than
re-use the top-level container.
Unlike VCL's DataModule all of the contained components are declared
private in the class, which means that they are not accessible outside of
the class. What waa the reason for that ?

Usual design guidelines. Make data private and add public properties to
access the data. You can add your own public properties to the class to
access whatever subset of data you need to access, or you can change the
accessibility of components in the properties panel ("Modifiers" property),
but I would personally do the former.
I noticed that my component will only visually accept non-GUI components
to be embedded within it in the IDE. That is what I want, but I was
wondering what controls that, especially as the
System.ComponentModel.IContainer accepts any IComponent interface ?

Not sure what you mean. I can add a UI control to a component class.
 
C

Clive Dixon

Further to my previous answer, I think I might be misunderstanding what you
are getting at.

If I create a component class in VS, it will generated with:
1) a constructor taking IContainer and calling IContainer.Add(IComponent),
so that if you for example drag drop your component onto a form (which is
generated by VS containing a Container object), VS ensures that your
component will be added to the form's Container object.
2) a private variable of type Container which may be used as the container
for other components you drag drop onto your component. Likewise, VS will
add that other component to your component's container only if that other
component has a constructor taking IContainer (and that constructor would
have to call IContainer.Add(IComponent) to work correctly).
 
E

Edward Diener

Clive said:
Further to my previous answer, I think I might be misunderstanding what you
are getting at.

If I create a component class in VS, it will generated with:
1) a constructor taking IContainer and calling IContainer.Add(IComponent),
so that if you for example drag drop your component onto a form (which is
generated by VS containing a Container object), VS ensures that your
component will be added to the form's Container object.
2) a private variable of type Container which may be used as the container
for other components you drag drop onto your component. Likewise, VS will
add that other component to your component's container only if that other
component has a constructor taking IContainer (and that constructor would
have to call IContainer.Add(IComponent) to work correctly).

Thanks for the explanation.

Does the IDE know to look for a private variable of type
System.ComponentModel.Container in a CLR object in order to have that
object act as a container for other objects at design time ? If not,
what is the criteria by which an object at design time can act as a
visual container of other objects ?
 
E

Edward Diener

Clive said:
Usual design guidelines. Make data private and add public properties to
access the data. You can add your own public properties to the class to
access whatever subset of data you need to access, or you can change the
accessibility of components in the properties panel ("Modifiers" property),
but I would personally do the former.

Yes, I agree. Adding properties to the class is better.
Not sure what you mean. I can add a UI control to a component class.

I see that now. I do not understand what the use of adding a UI control
to a component class would be, sice I do not think the control would be
shown at run-time in any way..
 
C

Clive Dixon

Does the IDE know to look for a private variable of type
System.ComponentModel.Container in a CLR object in order to have that
object act as a container for other objects at design time ? If not, what
is the criteria by which an object at design time can act as a visual
container of other objects ?

You can add components to a form or component class even if it doesn't have
a container object (try deleting the container object from your wizard
generated form or component class and then add components). It's just that
if the component you add has a constructor taking IContainer, then VS will
add that component to the container object that was created by the wizard -
otherwise VS won't add it to the container.

I suspect VS's sole criterion for displaying a designer page for a class is
that the class derives either from Form or Component, though I don't know
for sure.
 
E

Edward Diener

Clive said:
You can add components to a form or component class even if it doesn't have
a container object (try deleting the container object from your wizard
generated form or component class and then add components). It's just that
if the component you add has a constructor taking IContainer, then VS will
add that component to the container object that was created by the wizard -
otherwise VS won't add it to the container.

I would assume ever GUI class must have a constructor taking IContainer,
else it could not be added to a form, yet when I drop a GUI object onto
my component class no code is generated which adds the GUI object to my
component class's private container.
I suspect VS's sole criterion for displaying a designer page for a class is
that the class derives either from Form or Component, though I don't know
for sure.

Since Form derived from Component, perhaps the criterion is that a
designer page for a class is displayed if that class is a component or
derived from a component.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top