C# - 在标签中逐字显示文字?

问题描述 投票:0回答:2

我对编码非常陌生,而且还很糟糕,但遗憾的是我找不到任何可以帮助我的东西。

我想为这个游戏制作一个小对话标签,我们正在为儿童制作,这些字母在标签中逐个显示,然后在按下按钮后,您将获得下一个对话框。到目前为止,我有这个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");

但这只会在输出中显示它而不是标签?而且我不能简单地将其转换为字符串并在标签中显示。

c# class label
2个回答
2
投票

假设您使用WinForms,您可以创建一个SlowLabel控件,您可以将其放在表单上并像其他任何表单一样使用。它使用知道如何处理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 
       }
    }
}

当您将其放在表单上时,您可以像这样使用它:

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.";
}

哪个会给你这个结果:

slow label with characters appearing


0
投票

Console.Write(c)将字符发送到控制台,而不是标签。

在Write函数中,可以为要写入的标签添加参数,并使用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);
© www.soinside.com 2019 - 2024. All rights reserved.