如何使用VB.NET在Windows应用程序上实现数组的撤消/重做 [英] How can I achieve undo/redo of an array on a windows application using VB.NET

查看:116
本文介绍了如何使用VB.NET在Windows应用程序上实现数组的撤消/重做的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我是新手,所以我需要一些帮助。

我刚刚完成了我的第一个Windows窗体应用程序并且是时候插入撤消/重做代码了。

应用程序使用二维数组,其中存储了来自组合框,文本框,复选框,标签等的每个数据。我需要代码撤消/重做这个二维数组的更改。有人可以帮忙吗?



我尝试过的事情:



我有一直在寻找这个,但到目前为止我刚刚找到了如何在二维数据网格视图上实现撤消/重做。不幸的是,我使用的数组相当大,而使用datagridview会使应用程序变得非常慢。

Hi everybody,

I am new on this so i need some help.
I have just finished my first Windows forms application and it's time to insert the undo/redo code.
The application uses a two-dimensional array on which every data from comboboxes, textboxes, checkboxes, labels etc are stored. I need code to undo/redo the changes of this two-dimensional array. Can anyone help?

What I have tried:

I have been searching for this, but up to now i have just find how to achieve undo/redo on a two-dimensional datagridview. Unfortunately, the array i am using is quite big and the use of a datagridview instead, makes the app really slow.

推荐答案

您可以创建一个将存储的动态数组所有已执行的操作。

例如:

You can create a dynamic array that will store all the actions that are performed.
for example:
'Create a arraylist to store states
dim actions as new arraylist
'call CreateAction to register a state
Private sub CreateAction()
   'Clone (create a copy) your data and store in an object, say data
   actions.add(data)
End Sub

To undo, you simply go back and retrieve the data from the array. 
k = 1 is one step back
k = 2 is two steps back
Private sub UndoAction(byval k as integer)
   data = actions(actions.count - 1 - k)  
End Sub 





更好的方法是注册状态和动作,以便重做动作。

我不知道怎么做。

上面的方法为我工作。



A better way is to register both state and action so that the action can be redone.
I don't know how that can be done.
the method above worked for me.


有很多撤消CodeProject上的/ redo代码示例。或者,您还可以查看我编写的一些旧代码( RandomMonkeyWorks - 在C ++中撤消/重做 [ ^ ])。它不是VB.Net,你必须在语法上修改它的语言,但这应该提供一个适用于你的情况的框架。



如果你使用我的框架(和其他几个框架),你必须在命令处理程序中添加处理来填充过去的动作堆栈,并编写undo和redo代码来处理2d数组处理的实际撤消和重做。我只是在过去的动作堆栈中创建一个先前值的列表,以及这些值的x,y位置。然后,在将Ctrl-Z和Ctrl-Y(或任何命令信号)绑定到撤消和重做之后,使用撤消和重做代码来查找该x的当前值, y位置,存储它,并将下一个(或前一个)值放入x,y位置。



在我链接的页面上给出了一个简单的例子。



希望这有帮助。
There are tons of undo/redo code examples here on CodeProject. Or, you can also look at some old code I wrote (RandomMonkeyWorks - Undo/Redo in C++[^]). It isn't VB.Net, and you would have to modify it syntactically to your language, but that should provide a framework for applying in your case.

If you use my framework (and several other frameworks), you would have to add processing in the command handlers to populate the past actions stack, and write undo and redo code to handle the actual undoing and redoing of processing of the 2d array. I would simply create a list of previous values, and the x,y positions of those values, in the past actions stack. Then, after you have tied 'Ctrl-Z' and 'Ctrl-Y' (or whatever command signal) into 'undo' and redo, you use the 'undo' and 'redo' code to find the current value of that x,y position, store it, and place the next (or previous) value into that x,y position.

A simple example is given on the page I linked to.

Hope this helps.


这篇关于如何使用VB.NET在Windows应用程序上实现数组的撤消/重做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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