2011-12-29 48 views
2

RichTextBoxを使用してテキストエディタを作成しようとしています。 RichTextBoxのやり直し機能についての私の懸念が取り消されました。RichTextBoxを元に戻すと元に戻すことができますか?

テキストボックスに書き始めたら、1分です!私がUndoメソッドを呼び出すと、それだけで私はrichtextboxを再びクリアまたはリセットすることを信じています。最後に追加した単語を元に戻す、最後に新しい行を追加するなど、もっとうまくやってもらえるようにするにはどうすればいいですか? (同じやり直しでも同じです!)

これを実現するプロパティまたはオプションがありますか?あるいは私自身のコードを実装する必要がありますか?

答えて

2

最後のデータを保存することができます。元に戻す場合は、データを最後のデータに変更することができます。いつでも最新のデータを設定できます!

私はボタンが書いたテキスト取り消しリッチテキストボックスとボタンとのWinFormください:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace test 
{ 


public partial class Form1 : Form 
{ 
    List<string> LastData = new List<string>(); 

    int undoCount = 0; 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     LastData.Add(richTextBox1.Text); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     try 
     { 
      richTextBox1.Text = LastData[LastData.Count - undoCount - 1]; 
      ++undoCount; 
     } 
     catch { } 
    } 

    private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e) 
    { 
     LastData.Add(richTextBox1.Text); 
     undoCount = 0; 
    } 
} 
} 

を、私は少しでもよくし、組織的な方法を見つけることができませんでした、あなたが変更することができます

LastData.Add(richTextBox1.Text); 
undoCount = 0; 

新しい単語や新しい行を保存するには

更新: RAMを保存したい場合は、多くの元に戻す保存後にリストの最初のデータ。ただ、ahmadaliのコードから上に行くために

+0

私はアイデアを取得していない、あなたはいくつかのコード例をしてください与えることができますか?私が尋ねているのは、アプリケーションからのアンドゥポイントをアクティブにして、ユーザーがテキストを変更する方法です。 –

+0

@ Sean87:コードを入力しました!!!! –

+0

ありがとうございましたが、うまくいきました...それはやりがいのある仕事です!ユーザーがたくさんのテキストを使って作業している場合、このリストが大きく成長すると思いませんか?それが唯一の方法であれば、タイマーを追加してコンテンツを毎回保存することもお勧めです。 –

3

- あなたは別々のクラスに入れて、またやり直し機能を実装することができます

NB。はい、それはテキストボックスが変更されるたびにすべてのテキストを保存するので、あなたのアプリが膨大な量のテキストに使用される場合や、アプリが長期間(つまり日/週)開いている場合には変更できます

public partial class MainForm : Form 
{ 

    Undoer undoer; 

    public MainForm() 
    { 
     InitializeComponent(); 

     this.txtBox.TextChanged += new EventHandler(TextBoxTextChanged); 
     this.undoer = new Undoer(ref this.txtText); 


     // create a context menu 
     ContextMenu menu = new ContextMenu(); 
     menu.MenuItems.AddRange(new MenuItem[] { 
        new MenuItem("&Undo", new EventHandler(this.undoer.undo_Click )), 
        new MenuItem("&Redo", new EventHandler(this.undoer.redo_Click )) 
     }); 
     this.txtBox.ContextMenu = menu; 

     // or create keypress event 
     this.txtBox.KeyDown += new KeyEventHandler(textBox_KeyDown); 
     this.KeyDown += new KeyEventHandler(textBox_KeyDown); 
    } 

    protected void TextBoxTextChanged(object sender, EventArgs e) 
    { 
     undoer.Save(); 
    } 

    protected void textBox_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) 
    { 
     if (e.Modifiers == (System.Windows.Forms.Keys.Control)) 
     { 
      if (e.KeyCode == Keys.Z) 
      { 
       this.undoer.Undo(); 
       e.Handled = true; 
      } 
      if (e.KeyCode == Keys.Y) 
      { 
       this.undoer.Redo(); 
       e.Handled = true; 
      } 
     } 
    } 
} 

public class Undoer 
{ 
    protected System.Windows.Forms.RichTextBox txtBox; 
    protected List<string> LastData = new List<string>(); 
    protected int undoCount = 0; 

    protected bool undoing = false; 
    protected bool redoing = false; 


    public Undoer(ref System.Windows.Forms.RichTextBox txtBox) 
    { 
     this.txtBox = txtBox; 
     LastData.Add(txtBox.Text); 
    } 

    public void undo_Click(object sender, EventArgs e) 
    { 
     this.Undo(); 
    } 
    public void redo_Click(object sender, EventArgs e) 
    { 
     this.Redo(); 
    } 

    public void Undo() 
    { 
     try 
     { 
      undoing = true; 
      ++undoCount; 
      txtBox.Text = LastData[LastData.Count - undoCount - 1]; 
     } 
     catch { } 
     finally{ this.undoing = false; } 
    } 
    public void Redo() 
    { 
     try 
     { 
      if (undoCount == 0) 
       return; 

      redoing = true; 
      --undoCount; 
      txtBox.Text = LastData[LastData.Count - undoCount - 1]; 
     } 
     catch { } 
     finally{ this.redoing = false; } 
    } 

    public void Save() 
    { 
     if (undoing || redoing) 
      return; 

     if (LastData[LastData.Count - 1] == txtBox.Text) 
      return; 

     LastData.Add(txtBox.Text); 
     undoCount = 0; 
    } 
} 
関連する問題