2016-05-17 45 views
0

就像我想的标题栏有这样的类型:其中,每一个字母而来的,是两秒的延迟后打字机效果..有没有办法来对标题栏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"; 

     } 

我试过这个..

+0

..并做了工作? – stuartd

+0

@stuartd不,可能吗? – CyraX6

+0

@ CyraX6您应该向我们提供您遇到的错误/问题 - 不要简单地说它不起作用。就我所见,您可能会遇到跨线程异常,根本没有任何反应,UI冻结或仅显示完整文本。这已经是4种可能性了。 – Rob

回答

4

你是在正确的轨道上

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 = ""; 
    } 
} 
+0

感谢我甚至不知道调度定时器 – CyraX6

+0

当写完所有的字母提示错误:类型System.IndexOutOfRangeException“未处理的异常发生在AKP的1.exe – CyraX6

+0

你有半句?一旦使用了消息中的所有字母,就需要停止定时器。 –

相关问题