Hi,
I have implemented an UndoEngine in my custom designer, and it works fine for operations such as moving, resizing etc.
The problem is with undoing a delete command which was done using GlobalInvoke(StandardCommands.Delete). I can see that the ComponentAdded event gets triggered by the IComponentChangeService but the control never reappears on the designersurface.
I have all the required services (I think):
ComponentSerializationService
IDesignerSerializationService
INameCreationService
UndoEngine
The undo is performed by calling the DoUndo() method from the hosting forms menu click event.
Any help would be greatly appreciated.
Here is my UndoEngine implementation:
internal class MyDesignerUndoEngine : UndoEngine
{
Stack<MyDesignerUndoUnit> undoUnits = null;
Stack<MyDesignerUndoUnit> redoUnits = null;
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
undoUnits.Clear();
redoUnits.Clear();
}
public MyDesignerUndoEngine(IServiceProvider provider)
: base(provider)
{
undoUnits = new Stack<MyDesignerUndoUnit>();
redoUnits = new Stack<MyDesignerUndoUnit>();
}
public bool CanUndo { get { return undoUnits.Count != 0; } }
public bool CanRedo { get { return redoUnits.Count != 0; } }
public string UndoText { get { return CanUndo ? undoUnits.Peek().Name : String.Empty; } }
public string RedoText { get { return CanRedo ? redoUnits.Peek().Name : String.Empty; } }
public void DoUndo()
{
if (CanUndo)
{
MyDesignerUndoUnit undoUnit = undoUnits.Pop();
undoUnit.Undo();
redoUnits.Push(undoUnit);
}
}
public void DoRedo()
{
if (CanRedo)
{
MyDesignerUndoUnit undoUnit = redoUnits.Pop();
undoUnit.Undo();
undoUnits.Push(undoUnit);
}
}
protected override void AddUndoUnit(UndoEngine.UndoUnit unit)
{
redoUnits.Clear();
undoUnits.Push(unit as MyDesignerUndoUnit);
}
protected override UndoEngine.UndoUnit CreateUndoUnit(string name, bool primary)
{
return new MyDesignerUndoUnit(this, name) as UndoUnit;
}
protected override void DiscardUndoUnit(UndoEngine.UndoUnit unit)
{
base.DiscardUndoUnit(unit);
}
protected override void OnUndoing(EventArgs e)
{
base.OnUndoing(e);
}
protected override void OnUndone(EventArgs e)
{
base.OnUndone(e);
}
protected class MyDesignerUndoUnit : UndoEngine.UndoUnit
{
public MyDesignerUndoUnit(UndoEngine parent, string desc)
: base(parent, desc)
{
}
public override void ComponentRemoved(ComponentEventArgs e)
{
_removalUnit = true;
_component = e.Component;
base.ComponentRemoved(e);
}
private bool _removalUnit = false;
private System.ComponentModel.IComponent _component = null;
public System.ComponentModel.IComponent Component
{
get { return _component; }
}
public bool RemovalUnit
{
get { return _removalUnit; }
}
}
}
Philip.