2017-12-17 1 views
0

간단한 Windows Forms 응용 프로그램을 만들려고합니다. TextBox에 텍스트를 쓰고 그럴 때마다 텍스트가 목록에 푸시되므로 모든 텍스트 조건이 적용됩니다. 그래서 우리는 그것을 사용하기 위해 "BACK"버튼을 가지고 있습니다. buttonBack.Click 잘 작동하지만 ... box.TextChanged 아닙니다. 나는이 이벤트가 나는 텍스트를 변경할 때마다 호출 할Windows Forms TextChanged

using System.Collections.Generic; 
using System.Linq; 
using System.Windows.Forms; 
using System.Drawing; 

namespace TextBox 
{ 
    class Revision 
    { 
     public string Text { get; set; } 
     public int CoursorPosition { get; set; } 
    } 

    class MyForm : Form 
    { 
     static RichTextBox box = new RichTextBox(); 
     static List<Revision> revisions = new List<Revision>(); 

     static void MakeRevision() 
     { 
      revisions.Add(new Revision 
      { 
       Text = box.Text, 
       CoursorPosition = box.SelectionStart 
      }); 
     } 

     public MyForm() 
     { 

      var buttonBack = new Button() 
      { 
       Location = new Point(0, 0), 
       Size = new Size(ClientSize.Width, 30), 
       Text = "Back" 
      }; 

      box.Size = new Size(ClientSize.Width, 100); 
      box.Multiline = true; 
      box.Location = new Point(0, buttonBack.Bottom); 
      box.TextChanged += (sender, args) => MakeRevision(); 
      box.MouseDown += (sender, args) => MakeRevision(); 

      Controls.Add(buttonBack); 
      Controls.Add(box); 

      buttonBack.Click += (sender, args) => 
      { 
       box.Text = revisions.Last().ToString(); 
       revisions.RemoveAt(revisions.IndexOf(revisions.Last())); 
      }; 
     } 

     public static void Main() 
     { 
      var form = new MyForm(); 
      Application.Run(form); 
     } 
    } 
} 
+0

그게 무슨 뜻 이니? –

+0

box.TextChanged + = (보낸 사람, args) => MakeRevision(); 나는 그것을 부른다) –

+0

그것은 어떻게 작동하지 않느냐? –

답변

1

나는 당신이 "뒤로"버튼의 텍스트를 업데이트 할 때,이 추가되기 때문에 작동하지 않는 것을 믿는 스택에 있도록 뒤로 버튼이 작동하지 않는 것처럼 보이지만 실제로는 그렇습니다. 텍스트 변경이 스택에 추가되어야하는지 여부를 평가하기 만하면됩니다. 다음은 그 예입니다.

using System.Collections.Generic; 
using System.Linq; 
using System.Windows.Forms; 
using System.Drawing; 

namespace TextBox 
{ 
    class Revision 
    { 
     public string Text { get; set; } 
     public int CoursorPosition { get; set; } 
    } 

    class MyForm : Form 
    { 
     static RichTextBox box = new RichTextBox(); 
     static List<Revision> revisions = new List<Revision>(); 
     static bool loading = false; 

     static void MakeRevision() 
     { 
      if (loading) 
       return; 

      revisions.Add(new Revision 
      { 
       Text = box.Text, 
       CoursorPosition = box.SelectionStart 
      }); 
     } 

     public MyForm() 
     { 

      var buttonBack = new Button() 
      { 
       Location = new Point(0, 0), 
       Size = new Size(ClientSize.Width, 30), 
       Text = "Back" 
      }; 

      box.Size = new Size(ClientSize.Width, 100); 
      box.Multiline = true; 
      box.Location = new Point(0, buttonBack.Bottom); 
      box.TextChanged += (sender, args) => MakeRevision(); 
      box.MouseDown += (sender, args) => MakeRevision(); 

      Controls.Add(buttonBack); 
      Controls.Add(box); 

      buttonBack.Click += (sender, args) => 
      { 
       if (revisions.Count > 0) 
       { 
        loading = true; 
        box.Text = revisions.Last().Text; 
        box.SelectionStart = revisions.Last().CoursorPosition; 
        box.Focus(); 
        revisions.RemoveAt(revisions.IndexOf(revisions.Last())); 
        loading = false; 
       } 
      }; 
     } 

     public static void Main() 
     { 
      var form = new MyForm(); 
      Application.Run(form); 
     } 
    } 
} 

나는 또한 buttonBack.Click 이벤트에 다시 텍스트 상자에 전송 된 것을 변경되었습니다.

+0

예, 이벤트의 이벤트 ... Thx :) –