Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,076 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,584 people online right now. Registration is fast and FREE... Join Now!




Timer Question

 
Reply to this topicStart new topic

Timer Question

cbr4rusty
17 Apr, 2008 - 08:19 AM
Post #1

New D.I.C Head
*

Joined: 15 Apr, 2008
Posts: 25

I know this is fairly fundamental but I can't seem to find what I'm looking for online...so....I am making a slideshow for pictures to put on my desktop. I want to set a timer to allow the pictureBox to get a different picture every 5 or so seconds. I can get the timer to hit once. How can I get it to hit every 5 seconds?? Heres the code I have to test it so far:

(I am assuming my mistake is setting it under timer1_Tick? I dont quite understand how the timer/tick works...)


CODE

        private void timer1_Tick(object sender, EventArgs e)
        {
            bool Tester = true;
            if (Tester == true)
            {
                richTextBox1.Text = "Works true";// test to check if works
                Tester = false;
            }
            else
            {
                richTextBox1.Text = "Works false";// test to check if works
                Tester = true;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            System.Timers.Timer aTimer = new System.Timers.Timer();
            // Set the Interval to 5 seconds.
            aTimer.Interval = 5000;
            aTimer.Enabled = true;
            aTimer.Start();
        }



Thanks!
-Rusty

This post has been edited by cbr4rusty: 17 Apr, 2008 - 08:20 AM
User is offlineProfile CardPM
+Quote Post

MRJ
RE: Timer Question
17 Apr, 2008 - 08:34 AM
Post #2

D.I.C Head
Group Icon

Joined: 13 Oct, 2007
Posts: 83



Thanked: 1 times
My Contributions
I think all you need to do is set the AutoReset property to true.

CODE

        private void Form1_Load(object sender, EventArgs e)
        {
            System.Timers.Timer aTimer = new System.Timers.Timer();
            // Set the Interval to 5 seconds.
            aTimer.Interval = 5000;

            aTimer.AutoReset = true; //new code

            aTimer.Enabled = true;
            aTimer.Start();
        }

User is offlineProfile CardPM
+Quote Post

Jayman
RE: Timer Question
17 Apr, 2008 - 08:40 AM
Post #3

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,919



Thanked: 42 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
Grab a new picture and put it in the PictureBox inside the Tick event of the Timer.

CODE

        private void timer1_Tick(object sender, EventArgs e)
        {
            PictureBox.Image = Image.FromFile("fileName.jpg");
        }

User is offlineProfile CardPM
+Quote Post

cbr4rusty
RE: Timer Question
18 Apr, 2008 - 09:37 PM
Post #4

New D.I.C Head
*

Joined: 15 Apr, 2008
Posts: 25

well I tried adding both of the suggested codes but it was a no go...For the following code I have a richtextbox that I am using to test my timer. I droped the "Timer" from the toolbox onto my form. Is there any way I can get this to repeat without using a loop of somesort?? I'm sure that is what the interval part is for...but I can't seem to get it right.

CODE

        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            timer1.Interval = 2000;

            bool Tester = true;
            if (Tester == true)
            {
                richTextBox1.Text = "true";
                Tester = false;
                
            }
            else
            {
                richTextBox1.Text = "false";
                Tester = true;
            }
        }


The code should switch richtextbox1 from "true" to "false" every 2 seconds....What am I doing wrong? Should it be under the even Tick?? Thanks!

-Rusty
User is offlineProfile CardPM
+Quote Post

n8wxs
RE: Timer Question
19 Apr, 2008 - 08:37 PM
Post #5

D.I.C Regular
***

Joined: 6 Jan, 2008
Posts: 445



Thanked: 42 times
My Contributions
QUOTE(cbr4rusty @ 18 Apr, 2008 - 10:37 PM) *

well I tried adding both of the suggested codes but it was a no go...For the following code I have a richtextbox that I am using to test my timer. I droped the "Timer" from the toolbox onto my form. Is there any way I can get this to repeat without using a loop of somesort?? I'm sure that is what the interval part is for...but I can't seem to get it right.

csharp

private void timer1_Tick(object sender, EventArgs e)
{
timer1.Enabled = true;
timer1.Interval = 2000;

bool Tester = true;
if (Tester == true)
{
richTextBox1.Text = "true";
Tester = false;

}
else
{
richTextBox1.Text = "false";
Tester = true;
}
}


The code should switch richtextbox1 from "true" to "false" every 2 seconds....What am I doing wrong? Should it be under the even Tick?? Thanks!

-Rusty


You are setting the boolean variable Tester to true every time the timer event fires.
Why do you expect Tester to ever test false?

To do what you want you need to move the declaration of Tester outside
the timer event method.

You are redundantly enabling the timer every time the event happens.
It couldn't have happened if the timer wasn't already running! smile.gif

Jeff

User is online!Profile CardPM
+Quote Post

cbr4rusty
RE: Timer Question
20 Apr, 2008 - 09:59 AM
Post #6

New D.I.C Head
*

Joined: 15 Apr, 2008
Posts: 25

QUOTE(n8wxs @ 19 Apr, 2008 - 09:37 PM) *

QUOTE(cbr4rusty @ 18 Apr, 2008 - 10:37 PM) *

well I tried adding both of the suggested codes but it was a no go...For the following code I have a richtextbox that I am using to test my timer. I droped the "Timer" from the toolbox onto my form. Is there any way I can get this to repeat without using a loop of somesort?? I'm sure that is what the interval part is for...but I can't seem to get it right.

csharp

private void timer1_Tick(object sender, EventArgs e)
{
timer1.Enabled = true;
timer1.Interval = 2000;

bool Tester = true;
if (Tester == true)
{
richTextBox1.Text = "true";
Tester = false;

}
else
{
richTextBox1.Text = "false";
Tester = true;
}
}


The code should switch richtextbox1 from "true" to "false" every 2 seconds....What am I doing wrong? Should it be under the even Tick?? Thanks!

-Rusty


You are setting the boolean variable Tester to true every time the timer event fires.
Why do you expect Tester to ever test false?

To do what you want you need to move the declaration of Tester outside
the timer event method.

You are redundantly enabling the timer every time the event happens.
It couldn't have happened if the timer wasn't already running! smile.gif

Jeff




Jeff, Thanx! I can't believe I did that. I meant to put that line in the Form_Load. It works now...

Rusty


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 07:24PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month