Inheriting from a user control to use another child control

I have a GridMasterControl user control that contains (among other things) another control called Grid.

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

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

Both ways that I can come up with to implement this have problems.

1. I could copy and paste the GridMasterControl and just change the Grid to EditableGrid. Bad idea (wrong code copying).

2. I could get EditableGridMasterControl to inherit from GridMasterControl, and then add the CreateGrid virtual function to GridMasterControl. EditableGridMasterControl can implement this to create an EditableGrid control. This is also a very bad idea, since I will need to modify the constructor file to create the control. I need the constructor file to be clean.

What is the best way to implement this?

EDIT

Just to clarify, I believe that the essence of the problem is that Visual Studio does not allow you to modify the designer files it creates (or at least its very bad idea), and I want to be able to conditionally create another class depending on the situation .

+3
source share
4

, : , Grid GridMasterControl, .

, ( , IGrid). .

, , . , AbstractGridMasterControl, , ( ). IGrid - -, .

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();
    }
}
+1

, EditableGridMasterControl, ReadOnly? true, . , , , GridMasterControl.

+4

, .

+3

GridMasterControl , : Grid GridMasterControl EditableGrid.

0

Source: https://habr.com/ru/post/1727361/


All Articles