2012-12-06 59 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)); 
,如果你有这样的一个标签内