リッチテキストボックスのための独自の元に戻すシステムを作成しました。何かをするたびに、元に戻す操作がスタックに追加されます。元に戻すを押すと、元に戻します。RichTextBoxスペースの追加を取り消す
この動作は、RichTextBoxesを除いて、私が実装したすべてのコントロールで完全に機能します。私はシンプルな要素にシステムを縮小しました。削除を押すたびに、現在選択されているテキストとそのインデックスがスタックに追加され、元に戻すとテキストがこのインデックスに戻されます。あなたは1行からちょうど\ nを、そしてこのような下にさらにテキストを選択した場合は
// Struct I use to store undo data
public struct UndoSection
{
public string Undo;
public int Index;
public UndoSection(int index, string undo)
{
Index = index;
Undo = undo;
}
}
public partial class Form1 : Form
{
// Stack for holding Undo Data
Stack<UndoSection> UndoStack = new Stack<UndoSection>();
// If delete is pressed, add a new UndoSection, if ctrl+z is pressed, peform undo.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Modifiers == Keys.None && e.KeyCode == Keys.Delete)
UndoStack.Push(new UndoSection(textBox1.SelectionStart, textBox1.SelectedText));
else if (e.Control && e.KeyCode == Keys.Z)
{
e.Handled = true;
UndoMenuItem_Click(textBox1, new EventArgs());
}
}
// Perform undo by setting selected text at stored index.
private void UndoMenuItem_Click(object sender, EventArgs e)
{
if (UndoStack.Count > 0)
{
// Save last selection for user
int LastStart = textBox1.SelectionStart;
int LastLength = textBox1.SelectionLength;
UndoSection Undo = UndoStack.Pop();
textBox1.Select(Undo.Index, 0);
textBox1.SelectedText = Undo.Undo;
textBox1.Select(LastStart, LastLength);
}
}
}
:ここ
は(テキストファイルの実際の読み取りなど)取り除か最も単純な要素を持つコードであります:![alt text](https://i.stack.imgur.com/48LOi.jpg)
コードをステップしましたか?おそらく、あなたのスタックのどこかにすべてを出力してから、スタックを再投入しますか? – MunkiPhD