C# 有没有办法使标题栏C具有打字机效果#

C# 有没有办法使标题栏C具有打字机效果#,c#,C#,就像我想让标题栏有一种打字机的效果,每一个字母都经过两秒钟的延迟 H e l l o P r o g r a m 延迟两秒钟 private void timer2_Tick(object sender, EventArgs e) { this.Text = "H"; timer2.Stop(); this.Text = "He"; } 我已经试过了..你的思路是对的 using S

就像我想让标题栏有一种打字机的效果,每一个字母都经过两秒钟的延迟

H e l l o P r o g r a m
延迟两秒钟

  private void timer2_Tick(object sender, EventArgs e)
        {
            this.Text = "H";
            timer2.Stop();
            this.Text = "He";

        }

我已经试过了..

你的思路是对的

using System.Windows.Threading; //add reference to WindowsBase.  this gives you access to the DispatcherTimer
DispatcherTimer timer { get; set; } //i used this because it runs on the UI thread which allows it to update.
int letterCount { get; set; }  //i used this to keep track of how many loops ran
string message { get; set; } // set the message you want to display

public Form1()
{            
    InitializeComponent();
    this.Text = ""; //clear the text.  this can be done in the designer
    letterCount = 0; // set the count to 0
    timer = new DispatcherTimer(); //configure the timer
    timer.Interval = TimeSpan.FromSeconds(1);
    timer.Tick += timer_Tick;
    message = "Hello World!"; //set the message
    timer.Start(); //start the timer
}

void timer_Tick(object sender, EventArgs e)
{
    this.Text += message[letterCount]; // add the letter to the title bar
    letterCount++; // increment the count
    if (letterCount > message.Length -1) // stop the timer once the message finishes to avoid getting an error
    {
        timer.Stop(); // use this to stop after once

        // use this to clear and restart
        letterCount = 0;
        this.Text = "";
    }
}

你在正确的轨道上

using System.Windows.Threading; //add reference to WindowsBase.  this gives you access to the DispatcherTimer
DispatcherTimer timer { get; set; } //i used this because it runs on the UI thread which allows it to update.
int letterCount { get; set; }  //i used this to keep track of how many loops ran
string message { get; set; } // set the message you want to display

public Form1()
{            
    InitializeComponent();
    this.Text = ""; //clear the text.  this can be done in the designer
    letterCount = 0; // set the count to 0
    timer = new DispatcherTimer(); //configure the timer
    timer.Interval = TimeSpan.FromSeconds(1);
    timer.Tick += timer_Tick;
    message = "Hello World!"; //set the message
    timer.Start(); //start the timer
}

void timer_Tick(object sender, EventArgs e)
{
    this.Text += message[letterCount]; // add the letter to the title bar
    letterCount++; // increment the count
    if (letterCount > message.Length -1) // stop the timer once the message finishes to avoid getting an error
    {
        timer.Stop(); // use this to stop after once

        // use this to clear and restart
        letterCount = 0;
        this.Text = "";
    }
}

@斯图尔特:不,可能吗?@CyraX6你应该向我们提供你遇到的错误/问题,而不是简单地说它不起作用。就我所见,您可能会遇到一个跨线程异常,什么都没有发生,UI冻结,或者只显示全文。这已经是4种可能性了。这是一个简单的问题,很明显,即使没有交叉线程异常等,他的代码也无法按预期工作,OP主要要求按要求显示的逻辑。@stuartd no,可能吗?@CyraX6您应该向我们提供您遇到的错误/问题,而不是简单地说它不起作用。就我所见,您可能会遇到一个跨线程异常,什么都没有发生,UI冻结,或者只显示全文。这已经是4种可能性了。这是一个简单的问题,很明显,即使没有交叉线程异常等,他的代码也不会像预期的那样工作,OP主要要求按照要求显示逻辑。谢谢,我甚至不知道dispatcher Timer,当它写完它给出的所有字母时,错误:未处理AKP 1.exe中出现“System.IndexOutOfRangeException”类型的异常。是否包含最后一部分?消息中的所有字母用完后,你需要停止计时器。好的,是的,我把它放在另一个计时器下。对不起。它工作得很好,我会在它完全写入后,将这个标题全部清除,然后再写入,然后再清除,再写入(无限循环)?感谢感谢我甚至不知道调度程序计时器在它写完它给出的所有字母时出现了错误:AKP 1中发生了类型为“System.IndexOutOfRangeException”的未处理异常。EXE是否包含最后一部分?消息中的所有字母用完后,你需要停止计时器。好的,是的,我把它放在另一个计时器下。对不起。它工作得很好,我会在它完全写入后,将这个标题全部清除,然后再写入,然后再清除,再写入(无限循环)?谢谢