Click here to Skip to main content
15,895,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have an array, let say 3 elements in it as below:
C#
string [] message = {how, are, you};
int counterTimerValue = 0;


For each element in the array, I would need to display them for 10 seconds for example. And when it reach the final element, it will go back to the first element and display the first element, second element again and again.
I have used a timer object to control the duration but clueless on how should I assign each element a timer or so.
Below are my current codes:

for(int i = 0; i < message.Length; i++)
{
    //txtNotification.Text = "";
    //txtNotification.Text = "Notification:";

    counterTimerValue = 10;
    displayTimer.Interval = 1000; // 1 second
    displayTimer.Tick += new EventHandler(this.displayTimerTick);
    displayTimer.Start();

    //txtNotification.Text = txtNotification.Text + message[i];
}



private void displayTimerTick(Object source, EventArgs e)
{
     counterTimerValue--;
     try
     {
         int index = (messageArray.Length - 1 - (counterTimerValue % messageArray.Length));
         txtNotification.Text = messageArray[index];
     }
     catch (Exception)
     {
     }
     if (counterTimerValue == 0)
     {
        displayTimer.Stop();
     }
}



The above codes will only display the last element "you" because the for loop will finish executing all the elements in the array instead of wait for the timer to finish one element then only go for another element.
Any help would be appreciated. Thank you.

What I have tried:

1. Search online for any ideas/brainstorming but no avail.
Posted
Updated 24-Sep-17 22:57pm
v2
Comments
Richard MacCutchan 25-Sep-17 3:54am    
Do not run the loop first. Create the timer and set the array index to zero. As each timer event displays the message increment the index value, not forgetting to check for the last value, and quit the timer.

1 solution

You have to move the code that displays the array item into the timer handler. Something like
private void displayTimerTick(Object source, EventArgs e)
{
    counterTimerValue--;
    try
    {
       int index = (message.length - 1 - (counterTimerValue % message.length));
       txtNotification.Text = message[index];
    }
    catch (Exception)
    {
    }

    if (counterTimerValue == 0)
    {
       displayTimer.Stop();
    }
}
 
Share this answer
 
v2
Comments
Jamie888 25-Sep-17 4:20am    
Sorry sir, I dont really get it. If I move the displaying codes into the timer handler as above, would not be the text would only display after the timer has ended?
CPallini 25-Sep-17 4:40am    
Sorry, my bad. It is (hopefully) fixed now.
Jamie888 25-Sep-17 5:08am    
Sir, I have updated my question based on your suggestion. After I have tried it but still the result keep on blinking with each at one second interval. Does the for loop is still necessary?
CPallini 25-Sep-17 5:19am    
Of course you don't need the loop anymore.
Jamie888 25-Sep-17 5:25am    
Sure sir. The program is working now. Thank you for your advise and help. Appreciate it a lot.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900