Different Custom Controls Based on the Same Base Class?

J

Joe Keller

Hello,

I have two custom controls - one is based off of the TextBox class and the
other the CheckBox class. In my application, when looping through the
controls collection of the form to perform an action based on the contents
of the controls, I must do an "Is" comparison for each control before I an
properly reference the control.

For example:
KJMaskEdit myTextBox;
KJCheckBox myCheckBox;

foreach(System.Windows.Forms.Control myControl in myControls)
{
if (myControl is KJMaskEdit)
myTextBox = (KJMaskEdit)myControl;
else if (myControl is KJCheckBox)
myCheckBox = (KJCheckBox)myControl;
}

What I'd really like to do is somehow base all of my custom controls on one
type of custom base control and give that base control the common methods
and properties that I must code separately today in all of my custom
controls. This would also take away from all of the "Is" type checking that
I must perform today. The resultant code could look like the following:

Note: Assume "_KJControl" is the base class for both the KJMaskEdit and
KJCheckBox controls.

foreach(System.Windows.Forms.Control myControl in myControls)
if (myControl is _KJControl)
myKJControl = (_KJControl)myControl;

Is there a way to use a custom base class for two totally different types of
UI controls (in this case the TextBox and CheckBox controls) without
starting from the built-in "Control" class and coding everything myself
(i.e. the functionality of a TextBox and CheckBox control)?

Thanks!

Joe
 
E

Elisa

Hi,

Define an interface an have each type of control provide its own
relevant implementation of that interface.


Regards,

Elisa
 

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