Custom controls derived from common usercontrol ancestor

J

John B

Hello

I want to create a set of controls that have some common methods and
properties. I was thinking that I should subclass the UserControl class and
add these common features there and then base all my custom controls on this
new MyUserControl Class. Is this the way to go?

The MyUserControl would not actually have any UI component and should be
abstract I think. Does this mena that I should not create the control by
just selecting New UserControl as this generates the UI code automatically?

Each custom control will contain either a textbox or combobox. Can I write
the MyUserControl class in such a way as to assume this and access a
property (eg. enabled) somwhow or do I need to implement this in each custom
class seperately?

I have not really done this before, so any help would be appreciated.

Thanks

John
 
O

Ollie Riches

From what you have stated the approach you suggest is the correct approach.

You should never create an instance of your base user control as it will not
be of any use as it will not have an visual components.

If all the derived user controls have the same functionality then yes this
can be placed in the base user control.

--
HTH

Ollie Riches
http://www.phoneanalyser.net

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a programmer
helping programmers.

John B said:
Hello

I want to create a set of controls that have some common methods and
properties. I was thinking that I should subclass the UserControl class and
add these common features there and then base all my custom controls on this
new MyUserControl Class. Is this the way to go?

The MyUserControl would not actually have any UI component and should be
abstract I think. Does this mena that I should not create the control by
just selecting New UserControl as this generates the UI code automatically?

Each custom control will contain either a textbox or combobox. Can I write
the MyUserControl class in such a way as to assume this and access a
property (eg. enabled) somwhow or do I need to implement this in each custom
class seperately?

I have not really done this before, so any help would be appreciated.

Thanks

John
=----
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

First of all, will your control have more than the textbox/combobox?
If yes then extending UserControl is the way to go, otherwise you should
explore other ways as declaring an interface and extending TextBox/ComboBox

Response to your questions are inline
I want to create a set of controls that have some common methods and
properties. I was thinking that I should subclass the UserControl class
and
add these common features there and then base all my custom controls on
this
new MyUserControl Class. Is this the way to go?

Yes, depending of the answer to the above question
The MyUserControl would not actually have any UI component and should be
abstract I think. Does this mena that I should not create the control by
just selecting New UserControl as this generates the UI code
automatically?

You should declare it as abstract as you will never create an instance of
it.
Each custom control will contain either a textbox or combobox. Can I write
the MyUserControl class in such a way as to assume this and access a
property (eg. enabled) somwhow or do I need to implement this in each
custom
class seperately?

you could do several things, you could define an enum to indicate what
control to create and pass it ot the constructor, in this way you can assure
that you create only one type of control. How to access it depends also of
your preferences, you could declare a :
public Control GetControl
property to get the current control
or you could create two properties, each one return either null or the
control currently in use

public TextBox GetTextBox
public Combobox GetCombo



Hope this help,
 

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