2016-05-17 85 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

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

相關問題