jay_t55
jay_t55

Reputation: 11652

How to make all new Controls inherit new, Extended Control

I have extended from Control, like so:

public class Ctrl : Control
{
     public Boolean HasBorder { get; set; }
     public Boolean ShouldDrawBorder { get; set; }

     protected override void OnPaint(PaintEventArgs e)
     {
          if(CertainConditionIsMet)
          {
               // Then draw the border(s).
               if(this.BorderType == BorderTypes.LeftRight)
               {
                   // Draw left and right borders around this Ctrl.

               }
           }

           base.OnPaint(e);
     }
}

But, when I add a new TextBox(); to the Form it still inherits from Control and not from Ctrl. How do I make all new Controls inherit from Ctrl instead?

Upvotes: 3

Views: 135

Answers (2)

Brad Christie
Brad Christie

Reputation: 101604

You would have to manually re-create each control you want inheriting from Ctrl. e.g.

public class TextBoxCtrl : Ctrl
{
  /* implementation */
}

EDIT:

to avoid having to re-invent the wheel, I'd probably tackle it the following way:

First, make the added properties part of an interface so it's more a control you can hand-off:

public interface ICtrl
{
    Boolean HasBorder { get; set; }
    Boolean ShouldDrawBorder { get; set; }
}

next, work out a helper method (in a separate class) that will handle the UI enhancements:

public static class CtrlHelper
{
    public static void HandleUI(Control control, PaintEventArgs e)
    {
        // gain access to new properties
        ICtrl ctrl = control as ICtrl;
        if (ctrl != null)
        {
            // perform the checks necessary and add the new UI changes
        }
    }
}

Next, work this implementation in to each control you want to customize:

public class TextBoxCtrl : ICtrl, TextBox
{
    #region ICtrl

    public Boolean HasBorder { get; set; }
    public Boolean ShouldDrawBorder { get; set; }

    #endregion

    protected override void OnPaint(PaintEventArgs e)
    {
        CtrlHelper.HandleUI(this, e);

        base.OnPaint(e);
    }
}
/* other controls */

Now you keep most of the original functionality of each control, keep its inheritance, and extend the functionality in one location with minimal effort (or change tot he original control).

Upvotes: 4

It'sNotALie.
It'sNotALie.

Reputation: 22794

You can't do this, unless you redo all the classes you need, for example:

public class ExtendedTextBox : Ctrl
{
    //implement the thing here
}

Upvotes: 1

Related Questions