2017-12-22 36 views
0

나는 코딩에 초보자이지만 여전히 나쁘지 만, 슬프게도 나를 도울 수있는 어떤 것도 찾을 수 없었다.C# - 문자를 문자로 문자로 표시 하시겠습니까?

우리가 아이들을 위해 만드는이 게임을위한 작은 대화 라벨을 갖고 싶었습니다. 문자가 라벨에 하나씩 표시되고 버튼을 누르면 다음 대화 상자가 나타납니다. 지금까지이 SlowWriter 클래스가 있습니다 :

public class SlowWriter 
    { 

     public static void Write(string text) 
     { 
      Random rnd = new Random(); 
      foreach (char c in text) 
      { 
       Console.Write(c); 
       Thread.Sleep(rnd.Next(30, 60)); 
      } 
     } 
    } 

그러나이 방법을 레이블에 표시하는 방법을 실제로 알 수는 없습니까? 왜냐하면 내 버튼 클릭 이벤트에 있기 때문입니다.

SlowWriter.Write("Lorem Ipsum"); 

그러나 이것은 출력이 아닌 라벨에 표시됩니다. 그리고 단순히 문자열로 변환하여 레이블에 표시 할 수는 없습니다.

+2

어떤 아이 :

private void check_Click(object sender, EventArgs e) { l1.Text = "The quick brown fox jumps over the lazy dog"; l2.Text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."; } 

당신이 결과를 줄 것이다? 이게 우승입니까? WPF? ...? – itsme86

+0

콘솔에 인쇄하기 때문에 '출력물'은 귀하의 경우 콘솔입니다. –

답변

0

Console.Write (c)는 레이블이 아닌 콘솔에 문자를 보냅니다.

쓰기 기능에서 쓰기를 원하는 레이블에 매개 변수를 추가하고 StringBuilder를 사용하여 문자를 추가 할 수 있습니다.

이 Write 함수를 비동기 적으로 실행하면 전체 문자열이 기록 될 때까지 스레드가 묶여 있지 않으므로 Task를 만들고 실행합니다. 이 경우 Invoke를 사용하여 UI 스레드를 호출하여 레이블의 텍스트를 변경해야합니다.

이 솔루션은 WinForms 용이지만 일반적인 아이디어가 있습니다. 이와

public class SlowWriter 
{ 
    public static void Write(string text, Label lbl) 
    { 
     Task.Run(() => 
     { 
      Random rnd = new Random(); 
      StringBuilder sb = new StringBuilder(); 
      foreach (char c in text) 
      { 
       sb.Append(c); 
       if (lbl.InvokeRequired) 
       { 
        lbl.Invoke((MethodInvoker)delegate { lbl.Text = sb.ToString(); }); 
       } 
       else 
       { 
        lbl.Text = sb.ToString(); 
       } 
       Thread.Sleep(rnd.Next(30, 60)); 
      } 
     }); 
    } 
} 

는 버튼 클릭 이벤트에 대한 유일한 변화는 당신이 쓰고 싶은 레이블에 전달하고, 그래서 "LBL은"원하는 라벨 객체의 변수 이름은 이런 식으로 뭔가를 보일 것 .

SlowWriter.Write("Lorem Ipsum", lbl); 
2

WinForms를 사용한다고 가정하면 양식에 넣을 수 있고 다른 양식처럼 사용할 수있는 저속 레이블 컨트롤을 만들 수 있습니다. UI 크로스 스레드 작업을 처리하는 방법을 알고있는 Timer을 사용합니다. 처리 할 문자의 경우 문자열에있는 IEnumerator<char>을 사용합니다.

public class SlowLabel:Label 
{ 
    System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer(); // Ticks now and then 

    IEnumerator<char> text = null; // holds the characters that still need to show up 

    static Random r = new Random(); // guarantee randomness 

    public SlowLabel() 
    { 
     timer.Interval = r.Next(30,60);  // when is the next tick 
     timer.Enabled = false;    // let's not start now 
     timer.Tick += (s,e) => {    // do one character 
      base.Text += text.Current;  // Current has charactwer to Add 
      timer.Interval = r.Next(30,60); // random next run 
      timer.Enabled = text.MoveNext(); // or we stop if no more charcters 
     }; 
    } 

    public override string Text 
    { 
     set 
     { 
      text = value.GetEnumerator();  // get the charcters to process 
      base.Text = String.Empty;   // start empty 
      timer.Enabled = text.MoveNext(); // tell the timer to start 
     } 
    } 
} 

당신이 당신의 폼에 떨어졌다 때 당신은과 같이 사용할 수 있습니다 : 라벨의

slow label with characters appearing