dimanche 20 décembre 2015

Does the command pattern for implementing Undo and Redo keep the button's state?

I tried to use Command Pattern for implementing Undo and Redo in my project

public abstract class Command
{
    protected Form Receiver { set; get; }
    protected HtmlElement Element { set; get; }
    abstract public  void ReDo();
    abstract public  void UnDo();
    public Command(Form receiver)
    {
        this.Receiver = receiver;
    }
}
class AddElementCmd : Command
{        
    public AddElementCmd(HtmlElement elem, Form receiver)
        : base(receiver)
    {
        Element = elem;
    }
    public override void ReDo()
    {
        ((FormBrowser)Receiver).AddElement(Element,false);
    }
    public override void UnDo()
    {
        ((FormBrowser)Receiver).DelElement(Element, false);
    }
}
class DelElementCmd : Command
{
    public DelElementCmd(HtmlElement elem, Form receiver)
        : base(receiver)
    {
        Element = elem;
    }
    public override void ReDo()
    {
        ((FormBrowser)Receiver).DelElement(Element, false);
    }
    public override void UnDo()
    {
        ((FormBrowser)Receiver).AddElement(Element, false);
    }
}

Somewhere in the AddElement(Element,false) I check the Checked state of a toolStripButton

public void AddElement(HtmlElement elem, bool isNew = true)
{
    IHTMLElement2 dom = elem.DomElement as IHTMLElement2;
    if (isNew)
    {
        Command cmd = new AddElementCmd(elem, this);
        Undo.Push(cmd);
        Redo.Clear();
    }    
   // some codes here....
   if (showAlltoolStripButton.Checked)
   {
       dom.runtimeStyle.visibility = "hidden";
   }
   else if (showSelectionToolStripButton.Checked)
   {
      dom.runtimeStyle.visibility = "visible";
   }
   ...

But surprisingly, sometimes when the toolStripButton is checked and I click undo button, (or redo), at the debug, the break point in the function shows the button.Checked is false (while it is true) !!! and it leads to misbehavior. I suspect if the command class store some old states of the buttons and now it remembers those states or not? if yes, how can I avoid this problem?

Aucun commentaire:

Enregistrer un commentaire