0

I need help with this code, My purpose is to show multiple Message one after one, this one is working fine but I need several messages to be shown one after one, I tryied different Method but allways showing Message1 and loop. Any hints guys. Thank you in advance.

private void TmrScroll_Tick(object sender, EventArgs e)
{
    String T1 = "Scorolling Message1";
    String T2 = "Scorolling Message2";

    LblScroll.Location = new Point(LblScroll.Location.X + 5, LblScroll.Location.Y);

    if (LblScroll.Location.X > this.Width)
    {
        LblScroll.Location = new Point(0 - LblScroll.Width, LblScroll.Location.Y);

        LblScroll.Text = T1;/
    }

1
  • Welcome to Stack Overflow. Please take the tour to learn how Stack Overflow works and read How to Ask on how to improve the quality of your question. Then edit your question to include your source code as a working minimal reproducible example, which can be compiled and tested by others to provide an answer faster. Please see: Why is “Can someone help me?” not an actual question?. It is unclear what you are asking or what the problem is. Please edit your question to include a more detailed description of the problem you have. Commented May 25 at 19:42

2 Answers 2

2

If the previous code didn’t work try this one:

string[] messages = { "Hello", "Hru?", "Welcome!" };
int current = 0;
int speed = 2;
int mDisplayTime = 2000;
int displayTimer = 0;
scrollTimer.Interval = 50;
scrollTimer.Start();
// Inside scrollTimer's Tick event
    lbl.Location = new Point(lbl.Location.X + speed, lbl.Location.Y);
    displayTimer += scrollTimer.Interval;
    if (lbl.Location.X > this.Width && displayTimer >= mDisplayTime)
    {
        current = (current + 1) % messages.Length;
        lbl.Text = messages[current];
        lbl.Location = new Point(-lbl.Width, lbl.Location.Y);
        displayTimer = 0;
    }
}

it basically does these things:

  • Keeps a list of messages and knows which one is showing right now.

  • Moves the text smoothly across the screen by a small amount every tiny moment.

  • Tracks how long the current message has been visible.

  • Once the message has fully moved off screen and shown for enough time (like 2 seconds), it switches to the next message.

  • Resets the text position to just outside the screen so the new message can scroll in smoothly.

  • Loops through the messages over and over without jumping or freezing.

  • That way, messages slide nicely one after another, and you get a smooth, neat scrolling effect.

I hope this one works...

Sign up to request clarification or add additional context in comments.

2 Comments

I realize a smal issue: How to shorten time between message, I mean when first message disappear it should take few second before appearence of next message.
If you want the messages to appear faster one after another just decrease the value of mDisplayTime. For example: 2000 means 2 seconds, 1000 means 1 second and so on... And if you want a longer pause between messages, just increase that value
0

So, you got two timers:

  1. One moves the text a little bit every 50 milliseconds to make it slide smooth.

  2. The other one changes the message every 2 seconds and puts the text back to the start.

Why two timers?

  1. Because smooth moving needs a fast timer.

  2. Changing messages doesn’t need to be that fast. If you use just one timer, either the text jumps around or the messages change too slow.

string[]  messages = { "1", "2", "3" };
int current = 0;
int speed = 2;
scrollTimer.Interval = 50;
mChangeTimer.Interval = 2000;
// On scrollTimer tick:
lbl.Location = new Point(lbl.Location.X + speed, lbl.Location.Y);
if (lbl.Location.X > this.Width)
    lbl.Location = new Point(-lbl.Width, lbl.Location.Y);
// On mChangeTimer tick
current = (current + 1) % messages.Length;
lbl.Text = messages[current];
lbl.Location = new Point(-lbl.Width, lbl.Location.Y);

The % sign means “mod”. It’s like when you add 1 to current, but if you go past the last message, you start again at 0. So current goes 0,1,2,0,1,2, and so on.

Oh, and just make sure:

Both timers are turned on (Enabled = true).

The label (lbl) has some text and starts in the right spot.

And that you actually put the code inside the Tick events of the timers.

If any of that’s missing things might not work right. If you still don’t get it or want help just ask:).

1 Comment

I added second timer and placed codes as you mentioned but it only shows Message 2 with loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.