2012-12-06 61 views
3

使用標籤的地方,在該標籤中顯示來自文本框的文本輸入。現在,我想讓標籤文本滾動。從來就環顧四周通過互聯網,我試圖寫入代碼這個標籤內:標籤滾動效果

private void label1_Click(object sender, EventArgs e) 
{ 
    int Scroll; 
    string strString = "This is scrollable text...This is scrollable text...This is scrollable text"; 

    Scroll = Scroll + 1; 
    int iLmt = strString.Length - Scroll; 
    if (iLmt < 20) 
    { 
     Scroll = 0; 
    } 
    string str = strString.Substring(Scroll, 20); 
    label1.Text = str; 
} 

有誰看到什麼林做錯了什麼?

+1

,它應該只進行一次移動。使用定時器定期調用滾動 – Offler

+4

請定義「你想要什麼」和「什麼是不工作」 –

+0

標籤上的文字showig應該在屏幕上滾動一次,滾動效果應該從左到右,應該是可讀的。我的表單上也有一個文本框和一個按鈕。文本框用於寫入文本,完成後按下按鈕。按下按鈕後,用戶編寫的文本應顯示在標籤內(並滾動)。 –

回答

1

您需要在函數調用之外聲明Scroll變量,每次單擊它時都會重置它。

這與形式負載自動計時碼滾動文本:

private Timer tmr; 
private int scrll { get; set; } 

void Form1_Load(object sender, EventArgs e) 
{ 
    tmr = new Timer(); 
    tmr.Tick += new EventHandler(this.TimerTick); 
    tmr.Interval = 200; 
    tmr.Start(); 
} 

private void TimerTick(object sender, EventArgs e) 
{ 
    ScrollLabel(); 
} 

private void ScrollLabel() 
{ 
    string strString = "This is scrollable text...This is scrollable text...This is scrollable text"; 

    scrll = scrll + 1; 
    int iLmt = strString.Length - scrll; 
    if (iLmt < 20) 
    { 
     scrll = 0; 
    } 
    string str = strString.Substring(scrll, 20); 
    label1.Text = str; 
} 

private void label1_Click(object sender, EventArgs e) 
{ 
    ScrollLabel(); 
} 
+0

是的,我得到了,但我想文本滾動自動,現在我不得不點擊文本滾動,任何想法如何可以做? –

3

//要容易得多:

private void timer2scroll_Tick(object sender, EventArgs e) 
{ 
    label10Info.Text = label10Info.Text.Substring(1, label10Info.Text.Length - 1) + label10Info.Text.Substring(0,1); 
} 
+3

雖然此代碼段可能會解決問題,但[包括解釋](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers)確實有助於提高帖子的質量。請記住,您將來會爲讀者回答問題,而這些人可能不知道您的代碼建議的原因。 – gunr2171

0

這是可能的使用我的圖書館。

WinForm的動畫庫[.NET3.5 +]

在淨WinForm的動畫控制/值的簡單庫(.NET 3.5和更高版本)。關鍵幀(路徑)基於和完全可定製。

https://falahati.github.io/WinFormAnimation/

var textToScroll = "Lorem Ipsum is simply dummy text of the printing and typesetting industry."; 
    var durationOfAnimation = 5000ul; 
    var maxLabelChars = 20; 
    var label = label1; 

    new Animator(new Path(0, 100, durationOfAnimation)) 
    { 
     Repeat = true, 
     ReverseRepeat = true 
    }.Play(
     new SafeInvoker<float>(f => 
     { 
      label.Text = 
       textToScroll.Substring(
        (int) Math.Max(Math.Ceiling((textToScroll.Length - maxLabelChars)/100f * f) - 1, 0), 
        maxLabelChars); 
     }, label)); 
,如果你有這樣的一個標籤內