Google Tricks | Facebook Tricks | Subscribe for Daily Updates


Saturday, January 12, 2013

C Sharp (C#) Timer Example and Tutorial


Timer in C Sharp, or I will just write C#, is a very powerful tool for things we want to do again and again, Yes it does works like loop which works without a condition. I had some problems when I started programming C#, now everything is cleared, so I thought sharing the skill with every one out there. Its easy than you think. Yeah I forgot to tell this is my first C Sharp post, I will be writing a lot more.
Assuming you know the basics of Visual Studio and C#.


C Sharp Timer Example

For example we need a text to be blinked continuously, we can use timer. Here I will make a label  with the text "ecolumns.NET" blink, an easy example to give you the idea how the timer works.

C Sharp Timer Tutorial

  • First of all make a new C# project.
  • Add a label and change the text to eColumns.NET,
  • Then, from the list of tools drag and drop timer on the form, you will see it appears below the form in a separate section.

csharp timer
  • Right click on the timer and click on properties
  • In the timer properties you will see a property named interval, this is the time in Milli seconds for the timer to tick and perform the action again, I am making it 1000 which will allow me to make the label blink every second.
chsarp timer interval

  •  Now go to the events tab (lightening symbol) above the properties. and double click on Tick
timer tick











Now the code we will write will get executed every second. On the first tick we need to hide the text so we will change the visibility to false. But for the next second we want to make the visibility true again. So what logic should we apply to do that.
Here is the logic I implemented.

label1.Visible = !label1.Visible; 
What it will do is that it will change the visibility every time the timer ticks, we are setting the visibility of the label to the visibility of the label with a NOT gate.
Now the last thing we need to do is that we need to trigger the timer. Double click on the form and write
timer1.Start(); 
This will start the timer as the form loads. Hope this helps you in getting the basic intro and the functionality of the timer.

 
eColumnson Google+
Subscribe to get the latest right to your inbox:

0 comments:

Post a Comment

Please do give your name while commenting :)