473,473 Members | 2,178 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Multiple derivations from UserControl

I am writing a number of controls which use inheritance and I have a
problem that I do not know how to solve. It's best if I describe the
class structure I want.

Grid - derives from UserControl and contains most of the functionality
required of a basic grid.

ViewGrid, EditGrid and TreeGrid - all derive from Grid

The problem I have is that I never want the programmer to create an
object of type Grid - they should choose the derived class that best
fits their needs.

If these were normal classes then I would just declare the Grid class
as abstract and it would work as I would like it to. The problem is
that they derive from UserControl and the designer needs to create an
instance of Grid when I view EditGrid in the designer.

Another idea I had was to declare the Grid class as protected or
internal but then I cannot derive a public class from it.

Is there a way to specify that a UserControl derived class cannot be
created outside of the assembly that it is in and still enable it to be
created by the designer?

steve.kaye

Apr 6 '06 #1
7 2497

"steve.kaye" wrote...
If these were normal classes then I would just declare the Grid class
as abstract and it would work as I would like it to. The problem is
that they derive from UserControl and the designer needs to create an
instance of Grid when I view EditGrid in the designer.
I don't know why it needs to instantiate from Grid in your case. I have
abstract controls that I have been able to edit subclasses to in the
designer. Maybe you at some point instantiate it by "composition" in your
class EditGrid.

Anyway...
Another idea I had was to declare the Grid class as protected or
internal but then I cannot derive a public class from it.


You can declare the constructor of Grid internal.

// Bjorn A
Apr 6 '06 #2

Bjorn Abelli wrote:
"steve.kaye" wrote...
If these were normal classes then I would just declare the Grid class
as abstract and it would work as I would like it to. The problem is
that they derive from UserControl and the designer needs to create an
instance of Grid when I view EditGrid in the designer.


I don't know why it needs to instantiate from Grid in your case. I have
abstract controls that I have been able to edit subclasses to in the
designer. Maybe you at some point instantiate it by "composition" in your
class EditGrid.


I've not been programming in .NET for long so could you tell me what
you mean by "instantiate it by "composition" "?

I have just tried this:

1) Start a new windows control library.
2) Rename the UserControl1 class to be BaseControl.
3) Change the BaseControl class to be abstract.
4) Build.
5) Add an Inherited UserControl to the project.

At this stage it would ask me to specify a class to derive from but it
warns me that no built assemblies contain components to inherit from
and advises me to build the current application or browse for a
previously built assembly from another application.

Another idea I had was to declare the Grid class as protected or
internal but then I cannot derive a public class from it.


You can declare the constructor of Grid internal.


This works but not as nicely as I would like. It still allows the
programmer to drag a Grid onto the form but it doesn't create the
object in InitializeComponent() because the constructor is not
accessible. I could use this method along with a comment in the
description of the control to not use it but use a derived class but I
would much rather avoid having the object in the toolbox to start with.

steve.kaye

Apr 6 '06 #3
"steve.kaye" wrote...
Bjorn Abelli wrote:
"steve.kaye" wrote...
> If these were normal classes then I would just declare the Grid class
> as abstract and it would work as I would like it to. The problem is
> that they derive from UserControl and the designer needs to create an
> instance of Grid when I view EditGrid in the designer.
I don't know why it needs to instantiate from Grid in your case. I have
abstract controls that I have been able to edit subclasses to in the
designer. Maybe you at some point instantiate it by "composition" in your
class EditGrid.


I've not been programming in .NET for long so could you tell me what
you mean by "instantiate it by "composition" "?


It can mean a lot of things...

A control is in many cases "composed" of other controls (buttons,
textfields, etc).

What I meant in this case was if you on your EditGrid possibly had put an
instance of Grid itself. I can't see any other reason for EditGrid to
complain on an instantiation of Grid.

I still believe you should be able to declare Grid abstract.
I have just tried this:

1) Start a new windows control library.
2) Rename the UserControl1 class to be BaseControl.
3) Change the BaseControl class to be abstract.
4) Build.
5) Add an Inherited UserControl to the project.

At this stage it would ask me to specify a class to derive from but it
warns me that no built assemblies contain components to inherit from
and advises me to build the current application or browse for a
previously built assembly from another application.


Never happened to me.

My VS automagically detects which controls the project already has, and lets
me choose which one I want to inherit from.
> Another idea I had was to declare the Grid class as protected or
> internal but then I cannot derive a public class from it.


You can declare the constructor of Grid internal.


This works but not as nicely as I would like. It still allows the
programmer to drag a Grid onto the form but it doesn't create the
object in InitializeComponent() because the constructor is not
accessible. I could use this method along with a comment in the
description of the control to not use it but use a derived class but I
would much rather avoid having the object in the toolbox to start with.


Didn't happen to me either.

I don't know if I have a bug somewhere in my VS, but when I add the library
to my toolbox, somehow it seems to detect that the control with only an
internal constructor cannot be instantiated, hence only shows the derivated
controls.

// Bjorn A
Apr 6 '06 #4

Bjorn Abelli wrote:
I don't know if I have a bug somewhere in my VS, but when I add the library
to my toolbox, somehow it seems to detect that the control with only an
internal constructor cannot be instantiated, hence only shows the derivated
controls.


Are you deriving your controls from UserControl or from Control? I
have just tried the simple controls test but derived the base from
Control instead of UserControl and I was able to make the class
abstract and it disappeared from the toolbax which is what I wanted.

I didn't get the same behaviour with the internal constructor. It
still had it on my toolbox and allowed me to add it to the form but
without the grid1 = new Grid() bit.

I'll have a look into what I need to do extra when deriving from
Control rather than UserControl.

Thanks

steve.kaye

Apr 6 '06 #5

Bjorn Abelli wrote:
I don't know if I have a bug somewhere in my VS, but when I add the library
to my toolbox, somehow it seems to detect that the control with only an
internal constructor cannot be instantiated, hence only shows the derivated
controls.


Are you deriving your controls from UserControl or from Control? I
have just tried the simple controls test but derived the base from
Control instead of UserControl and I was able to make the class
abstract and it disappeared from the toolbax which is what I wanted.

I didn't get the same behaviour with the internal constructor. It
still had it on my toolbox and allowed me to add it to the form but
without the grid1 = new Grid() bit.

I'll have a look into what I need to do extra when deriving from
Control rather than UserControl.

Thanks

steve.kaye

Apr 6 '06 #6

steve.kaye wrote:
Bjorn Abelli wrote:
I don't know if I have a bug somewhere in my VS, but when I add the library
to my toolbox, somehow it seems to detect that the control with only an
internal constructor cannot be instantiated, hence only shows the derivated
controls.


Are you deriving your controls from UserControl or from Control? I
have just tried the simple controls test but derived the base from
Control instead of UserControl and I was able to make the class
abstract and it disappeared from the toolbax which is what I wanted.


I have encountered the same problem with the Control class now. MSDN
says that the base class of the object being passed to the designer is
not allowed to be abstract.

steve.kaye

Apr 6 '06 #7
"steve.kaye" wrotw...

Bjorn Abelli wrote:
I don't know if I have a bug somewhere in my VS, but when
I add the library to my toolbox, somehow it seems to detect
that the control with only an internal constructor cannot be
instantiated, hence only shows the derivated controls.
Are you deriving your controls from UserControl or from Control?


In this tryout I derived from UserControl.
I have just tried the simple controls test but derived the base from
Control instead of UserControl and I was able to make the class
abstract and it disappeared from the toolbax which is what I wanted.
Nice that it worked out for you.
I didn't get the same behaviour with the internal constructor. It
still had it on my toolbox and allowed me to add it to the form but
without the grid1 = new Grid() bit.

I'll have a look into what I need to do extra when deriving from
Control rather than UserControl.


What you'll be missing depends on what you need... ;-)

Maybe you could try to derive from ContainerControl instead (the immediate
superclass of UserControl). You'll miss less features that way.

// Bjorn A
Apr 6 '06 #8

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

Similar topics

2
by: lothar.behrens | last post by:
Hi, I have a base class and virtually derived from them in two other classes. In turn I defined a class that derives from the two other class. Window < MasterWindow < < ...
2
by: Andy Meyer | last post by:
Hi all, I'm converting some C++ Controls to C# and there's one big thing, that I can't solve: class CControlEx { int nDescriptionID; CString strDescription;
0
by: blackdevil1979 | last post by:
Hello, > "In COM, multiple inheritance between interfaces is not supported. However, by using the derived members capability, multiple inheritance can be simulated."..MSDN Library I have...
9
by: Quina | last post by:
Hi. Is there someone that can tell me how can I have multiple screens on the the same form, displaying only on at the time? Thank you all for any replys. Joćo Carias
60
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the...
9
by: Gummy | last post by:
Hello, I created a user control that has a ListBox and a RadioButtonList (and other stuff). The idea is that I put the user control on the ASPX page multiple times and each user control will...
9
by: moondaddy | last post by:
I'm using asp.net 2.0 and c# and would like to share some user control between several websites. these websites are on the same server and have a physical location right next to each other like...
47
by: Larry Smith | last post by:
I just read a blurb in MSDN under the C++ "ref" keyword which states that: "Under the CLR object model, only public single inheritance is supported". Does this mean that no .NET class can ever...
2
by: =?Utf-8?B?VGFtbXkgTmVqYWRpYW4=?= | last post by:
Hi, I am going to write a large application using Visual Studio C#. I am going to use only one Form as main menu and go to other pages by cliking on next button in each page. I dont want to create...
0
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...
0
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...
1
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
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 ...
0
muto222
php
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.