如何为文本框实现良好而有效的撤消/重做功能 [英] How to implement good and efficient undo/redo functionality for a TextBox

查看:125
本文介绍了如何为文本框实现良好而有效的撤消/重做功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个TextBox,我想为其实现撤销/重做功能.我已阅读,它可能已经具有一些撤消功能,但是它有问题吗?无论如何,我也想同时实现撤消和重做功能,只是为了学习如何进行.

I have a TextBox which I would like to implement undo/redo functionality for. I have read that it might have some slight undo functionality already, but that it is buggy? Anyways, I would like to implement both undo and redo functionality also just to learn how you would go ahead and do that.

我已经阅读了有关 Memento模式的内容,并在

I have read about the Memento Pattern and looked some on a Generic Undo/Redo example on CodeProject. And the pattern kiiind of makes sense. I just can't seem to wrap my head around how to implement it. And how to do it effeciently for the contents of a TextBox.

当然,我只能在TextChanges时存储textbox.Text,但这会相当快地占用很多内存,特别是如果TextBox包含很多文本的话.

Of course I could just store textbox.Text when TextChanges, but that would hug up quite a lot of memory pretty fast, especially if the TextBox contained a lot of text.

因此,无论如何,我正在寻找有关如何实施一种良好,清晰而有效的方式来实现此功能的建议.一般而言,尤其是对于TextBox c,)

So anyways, I'm looking for some advice on how to implement a good, clear and efficient way of implementing this functionality. Both in general and especially for a TextBox c",)

推荐答案

.NET System.ComponentModel命名空间带有IEditableObject接口,您也可以使用INotifyPropertyChangingINotifyPropertyChanged. MVC模式还可以使您的界面通过事件响应模型中的更改,从而更新或还原文本框的值.

The .NET System.ComponentModel namespace comes with an IEditableObject interface, you could also use INotifyPropertyChanging and INotifyPropertyChanged. MVC Pattern would also make it that your interface responds to changes in the model through events thus updating or restoring the value of your textbox.

有效地是 Memento模式.

您看过这些吗? 此处是一种方法.

Have you had a look into these? Here is a how to.

一个简单快捷的版本是存储文本框OnTextChanged的状态.每次撤消将返回数组中的最后一个事件. C#堆栈类型在这里很方便.您也可以在退出界面后或在Apply之后清除状态.

A simple and quicker version would be to store the state of the textbox OnTextChanged. Each undo would return the last event in an Array. The C# Stack Type would be handy here. You could clear the state once you are off the interface also or after Apply.

这篇关于如何为文本框实现良好而有效的撤消/重做功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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