Timer tick event in c# windows application

Timer control,Timer tick event in c# windows application,non-visual control
Share it:

Timer tick event in c#

The Timer control (System.Windows.Forms.Timer) is used to execute commands for every tick of time which is specified with an interval. For example, if you want to print your name every 5 seconds, then you can use the Timer control. The Timer control is a non-visual control and can be seen in the component tray after you drag it to the form from the toolbox.



The Timer control doesn't have that many properties. The two properties that are most important are the Enabled and Interval properties. The Enabled when set to true starts the timer, and stops it when setting to false. The Interval property specifies the interval time between calls to the Tick event, which is the default event of the Timer used to execute commands. Note that the value that is accepted by the Interval property is in milliseconds. If you give the value 5000 as the interval, then the Tick event will be called for every 5000 milliseconds or 5 seconds.

Let's take a look at an example application of the Timer control. The following application has a button that will be animated to move up and down and bounce from the bounds of the client area of the form. Start by creating a new Windows Forms Application and naming in TimerDemo. Add a button control to the center of the form as seen in Figure 1.



Timer tick event in c#




Drag a Timer control to the form. It can be found in the Components section of the toolbox. You will notice that it now appears inside the component tray at the bottom portion of the Designer.


Timer tick event in c#



private int velocity = 5;

Use the following event handler for the Timer's Tick event.


private void timer1_Tick(object sender, EventArgs e)
{
    if (button1.Top <= 0 || button1.Bottom > this.ClientSize.Height)
        velocity = -velocity;

    button1.Top += velocity;
}



The very first that the timer ticks, this handler will be called. It will first test if the button hits the topmost or bottommost corner of the form. To determine if the button hits the top corner, we used the Button.

Top property which returns the Y coordinate of the top of the button. If it returns 0 or less, then it means that the button is or beyond the top bound of the client area of the form. 

Note that the coordinate system used by the form starts it's origin from the top-left.

The second part of the condition in the if contidion tests if the button hits the bottom bounds of the client area. We used the Button.Bottom property to get the Y coordinate of the bottom of the button. 

We then check if it exceeds the bottom of the form by comparing it to the height of the client area of the form. 

We do this using the Form.ClientSize property and accessing it's Height property.

If any of the condition is true, then we simply reverse the value of the velocity by using the - operator to multiply the value by -1. 

This would produce a bouncing effect. If the button is moving up (because it moves 5 pixels up), and it hits the top corner, then the velocity will be inverted and become's -5. Moving -5 pixels up is equivalent to moving 5 pixels down.


Run the program and see the button bounce from the top and bottom corners of the form.

Do you know:-


If you like the tutorial, then please share this tutorial with your friends on social media.

Share it:

Windows Forms

Post A Comment:

0 comments: