0

I'm only just getting into GUIs, so please forgive me if I've missed something obvious. Google hasn't helped much I'm afraid.

My base objective is to have Marquee style text scrolling across the screen a set number of times. I've achieved the scrolling aspect utilizing a timer which scrolls a label named "My text here" across the screen (taken from this tutorial: http://www.youtube.com/watch?v=-y-Z0i-DeAs Note: I don't speak the language he does), but I've been unable to get the thing to stop. I'm open to using a different way to achieve the scrolling, but this is the only example I've found so far that worked well with my current level of knowledge of GUIs (basically dragging and dropping).

private void timer_Tick(object sender, EventArgs e)
    {
            this.Refresh();
            labelTesting.Left += 5;
            if (labelTesting.Left >= this.Width)
            {
                labelTesting.Left = labelTesting.Width * -1;
            }           
    }

My best guess is that the timer is simply starting the whole process over with each tick. I've tried countering this by having it return after running i times and telling the label what to display, but that's not working. Nor can I seem to find a way to tell the thing to stop.

http://www.dotnetperls.com/timer has an example where a timer is set to run for a given amount of time, but I do not know how to implement that when messing around with GUIs. What would be the best way to implement the feature I desire? Any insight or suggestions would be greatly appreciated.

EDIT: Based on suggestions in the answers and comments I have edited the code to run for what should be 30 seconds before setting itself to a given position. However the text no longer scrolls. I'm going to keep working at it, but more input would be appreciated.

        private void timer_Tick(object sender, EventArgs e)
    {
        var time = DateTime.Now;
        if(time < DateTime.Now.AddSeconds(-30)) // you decide when to stop scrolling
        {
            Timer timer = (Timer)sender;

            timer.Stop();
            labelTesting.Left = 0; // or wherever it should be at the end of the scrolling
        }

        this.Refresh();
        labelTesting.Left += 5;
        if (labelTesting.Left >= this.Width)
        {
            labelTesting.Left = labelTesting.Width * -1;
        }
    }
4

1 回答 1

0

您需要停止计时器:

private void timer_Tick(object sender, EventArgs e)
{
        if (someConditionToEndScroll) // you decide when to stop scrolling
        {
           Timer timer = (Timer) sender;

           timer.Stop();
           labelTesting.Left = 0; // or wherever it should be at the end of the scrolling
        }

        this.Refresh();
        labelTesting.Left += 5;
        if (labelTesting.Left >= this.Width)
        {
            labelTesting.Left = labelTesting.Width * -1;
        }           
}
于 2013-03-05T23:30:31.430 回答