Mongus Pong
Mongus Pong

Reputation: 11477

Inheriting from a User Control to use a different child Control

I have a user control, GridMasterControl which contains (amongst other things) another control called Grid.

Now I need to inherit from Grid to create EditableGrid (to add editing functionality to the grid).

I then need to create an EditableGridMasterControl which will be identical to GridMasterControl except that it will use EditableGrid, not Grid.

Both ways I can think of to implement this have problems.

1. I could copy and paste GridMasterControl and just change Grid to EditableGrid. Bad idea (code duplication not good).

2. I could get EditableGridMasterControl to inherit from GridMasterControl, then add a virtual function CreateGrid into GridMasterControl. EditableGridMasterControl could implement this to create the EditableGrid control. This too is a very bad idea as I would need to change the designer file to create the control. I need to keep the designer file clean.

How best can I implement this?

EDIT

Just to clarify I think the crux of the problem is that Visual Studio doesnt allow you to modify the designer files it creates (or at least its a very bad idea to) and I want to be able to conditionally create a different class depending on the situation.

Upvotes: 3

Views: 2296

Answers (4)

TheSean
TheSean

Reputation: 4566

If you want to keep the designer file the same for both, try this: Instead of dropping the actual Grid control onto your GridMasterControl, just drop a panel as a place holder.

Then create a mandatory initialization step where the appropriate grid object is created (use an interface like IGrid). This can be done in the derived class.

Here is what I mean, of course you can implement the same pattern a few different ways. The amount of code you share between the concrete implementations of AbstractGridMasterControl can vary and depends on how similar the controls are (which I have no idea). Instead of interface IGrid, you could use a parent class - again, matters how you want to do inheritance.

interface IGrid {...}
class Grid : IGrid { ...}
class EditableGrid : IGrid { ... }

abstract class AbstractGridMasterControl
{
    protected IGrid Grid
    {
        set { this.panelControl.Controls.Add(value as Control);}
    }
}

class GridMasterConrol : AbstractGridMasterControl
{
    public GridMasterControl()
    {
        this.Grid = new Grid();
    }
}

class EditableGridMasterConrol : AbstractGridMasterControl
{
    public GridMasterControl()
    {
        this.Grid = new EditableGrid();
    }
}

Upvotes: 1

Rune Grimstad
Rune Grimstad

Reputation: 36300

Could you not have one control, EditableGridMasterControl, that has a ReadOnly property? If this is set to true you disable the editing. This gives you only one set of controls to maintain, but requires the control to be more advanced than the simple GridMasterControl.

Upvotes: 4

Matt Ellen
Matt Ellen

Reputation: 11592

If the GridMasterControl is doing the same thing for either grid class, you don't need to change anything: the Grid member of the GridMasterControl can hold an EditableGrid.

Upvotes: 0

Charlie
Charlie

Reputation: 10307

Perhaps you could look at using a composition pattern to reuse your common functionality between your different controls.

Upvotes: 3

Related Questions