如何在列表中移回列表中的最后一个单元格 [英] How to move back in a list with removing the last cell in the list

查看:80
本文介绍了如何在列表中移回列表中的最后一个单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计



i创建了一个带有Undo和Redo选项的绘画项目

按下撤消按钮后我必须在列表中向上跳一步

以便能够显示最后一个动作



首次点击后问题开始撤消按钮,然后在重做按钮上

然后当我想再次按下撤销按钮时,它会显示结束时的第二个单元格而不是最后一个单元格



因为每次按下撤销按钮后我删除列表中的最后一个单元格

有没有其他方法可以在列表中移回而不删除最后一个单元格?



  private   void  BtnUndo_Click( object  sender,EventArgs e)
{
PicInMemory = UndoList [UndoList.Count - 1 ];
PaintOnPanel = panel1.CreateGraphics();
PaintOnPanel.DrawImage(PicInMemory, 0 0 );
UndoList.RemoveAt(UndoList.Count - 1 ); --->问题
undocounter ++;
}

private void BtnRedo_Click( object sender,EventArgs e)
{
PicInMemory = RedoList [RedoList.Count - undocounter];
PaintOnPanel = panel1.CreateGraphics();
PaintOnPanel.DrawImage(PicInMemory, 0 0 );
undocounter--;
}





感谢您的帮助...

解决方案

< blockquote>为什么要从撤消列表中删除某个操作,而不将其添加到重做列表中?为什么不从重做列表中删除项目并将它们放在撤消列表中?



我怀疑你需要做这两件事,当你你的问题可能会消失...


或使用稍微不同的方法:



只保留一个操作列表加上最后一次操作的当前索引。

On Undo:减量索引。

重做:增量索引。

操作:丢掉什么是在列表中的索引之后,追加操作,增量索引。


  private   void  BtnUndo_Click( object  sender,EventArgs e)
{
Counter ++;
PicInMemory = UndoList [UndoList.Count - Counter];
PaintOnPanel = panel1.CreateGraphics();
PaintOnPanel.DrawImage(PicInMemory, 0 0 );
UndoMade = true ;
BtnRedo.Enabled = true ;
}

private void BtnRedo_Click( object sender,EventArgs e)
{
PicInMemory = RedoList [RedoList.Count - Counter];
PaintOnPanel = panel1.CreateGraphics();
PaintOnPanel.DrawImage(PicInMemory, 0 0 );
柜台 - ;
}


Hey guys

i created a paint project with the option of Undo and Redo
After i press on the undo button i have to jump one step higher in the list
in order to be able to show the last action

the problem starts when after first click on the Undo button and then on the Redo button
then when i want to press on the undo button again it's shows me the second cell from the end and not the last

because after every press on the Undo button i Remove the last cell in the list
is there any other way to move back in the list without removing the last cell ?

private void BtnUndo_Click(object sender, EventArgs e)
        {
            PicInMemory = UndoList[UndoList.Count - 1];
            PaintOnPanel = panel1.CreateGraphics();
            PaintOnPanel.DrawImage(PicInMemory, 0, 0);
            UndoList.RemoveAt(UndoList.Count - 1);         ---> the problem is with this line
            undocounter++;
        }

        private void BtnRedo_Click(object sender, EventArgs e)
        {
            PicInMemory = RedoList[RedoList.Count - undocounter];
            PaintOnPanel = panel1.CreateGraphics();
            PaintOnPanel.DrawImage(PicInMemory, 0, 0);
            undocounter--;
        }



Thank's for the help ...

解决方案

Why are you removing an action from your undo list, without adding it to your redo list? And why do you not remove items from the redo list and put them on the undo list?

I suspect that you need to do both of these things, and when you do, your problem may disappear...


Or use a slightly different approach:

Keep just one list of operations plus the current index of the last operation.
On Undo: decrement index.
On Redo: increment index.
On Operation: Throw away what's after index in the list, append operation, increment index.


private void BtnUndo_Click(object sender, EventArgs e)
{
    Counter++; 
    PicInMemory = UndoList[UndoList.Count - Counter]; 
    PaintOnPanel = panel1.CreateGraphics();
    PaintOnPanel.DrawImage(PicInMemory, 0, 0);
    UndoMade = true;
    BtnRedo.Enabled = true;
}

private void BtnRedo_Click(object sender, EventArgs e)
{
    PicInMemory = RedoList[RedoList.Count - Counter];
    PaintOnPanel = panel1.CreateGraphics();
    PaintOnPanel.DrawImage(PicInMemory, 0, 0);
    Counter--;
}


这篇关于如何在列表中移回列表中的最后一个单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆