Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Working with timer control (Time interval, Start, Stop) in VB.net - C#.net Part1

Author: Shehzad Hemani
Download Source Code : 1047_source.zip

In this simple article you will learn that how timer control works, how we can get or set time interval and how to start and stop timer in VB and C#.net.

 Timer:

Timer is a control which is provided by dot net. This control is very useful for different application. This control acts like a thread and easy to use. By using this control we can do a specific work after some specific time interval. We can set a time interval after that our function will be called, in that we can do anything that we want to do. Timer is like a thread and runs side by side.

Set Interval Time:

First of all this is important to know that how we can set or get interval time for our timer. To set or get the interval time we have a property named interval. This property takes and returns integer value.

Set:

To demonstrate make a window application. Drag timer control, textbox and one button on form.

Now write the following code on Button click event:

C#

private void btn_setInterval_Click(object sender, EventArgs e)

        {

            if (txt_interval.Text!=string.Empty)

            {

                int i = 0;

                string str = txt_interval.Text;

                i = Convert.ToInt32(str);

                timer1.Interval = i;

                MessageBox.Show("Interval is set");

            }

            else

                MessageBox.Show("please enter value");

        }

VB

Private Sub btn_setInterval_Click(ByVal sender As ObjectByVal As EventArgs)
        
If (txt_interval.Text <> string.Empty) Then
            Dim 
As Integer = 0
            
Dim str As String = txt_interval.Text
            i 
Convert.ToInt32(str)
            timer1.Interval 
i
            MessageBox.Show(
"Interval is set")
        
Else
            
MessageBox.Show("please enter value")
        
End If
    End Sub

This simple code sets the interval value.

Get:

To demonstrate make a window application. Drag timer control and one button on form.

Now write the following code on Button click event:

C#

private void btn_getinterval_Click(object sender, EventArgs e)

        {

            int i = 0;

            i = timer1.Interval;

            string str ="Interval :" + i.ToString();

            MessageBox.Show(str);

        }

VB

Private Sub btn_getinterval_Click(ByVal sender As ObjectByVal As EventArgs)
        
Dim As Integer = 0
        
timer1.Interval
        
Dim str As String = ("Interval :" + i.ToString)
        MessageBox.Show(
str)
    
End Sub

This simple code gets the interval value.

Start Timer:

After setting the interval of the time we have to start the timer. When the timer start it starts calculating the time as it reaches to the specified interval. It performs the task that is given to it then again start calculating from zero. To start the timer we have a function named start. This function takes and returns nothing.

To demonstrate make a window application. Drag timer control and one button on form.

Now write the following code on Button click event:

C#

private void btn_start_Click(object sender, EventArgs e)

        {

            try

            {

                timer1.Start();

                MessageBox.Show("Timer has started");

            }

            catch (Exception ex) { }

        }

VB

Private Sub btn_start_Click(ByVal sender As ObjectByVal As EventArgs)
        
Try 
            
timer1.Start
            MessageBox.Show(
"Timer has started")
        
Catch ex As Exception
            
        End Try
    End Sub

This simple code starts timer.

Stop Timer:

After starting the timer if you want to stop it for this purpose we have a function of timer class named stop. This function takes and returns nothing and stops the timer. When the timer is stopped and then again starts, it starts from zero. Means it cannot be paused and resumed.

To demonstrate make a window application. Drag timer control and one button on form.

Now write the following code on Button click event:

C#

private void btn_stop_Click(object sender, EventArgs e)

        {

            try

            {

                timer1.Stop();

                MessageBox.Show("Timer has stoped");

            }

            catch (Exception ex) { }

 

        }

VB

Private Sub btn_stop_Click(ByVal sender As ObjectByVal As EventArgs)
        
Try 
            
timer1.Stop
            
MessageBox.Show("Timer has stoped")
        
Catch ex As Exception
            
        End Try
    End Sub

This simple code stops timer.

Now write the following code on FORM LOAD event:

C#

private void Form1_Load(object sender, EventArgs e)

        {

            this.Text = "DEVASP TIMER APPLICATION";

        }

VB

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)

 Me.Text "DEVASP TIMER APPLICATION"

   End Sub

This simple article tells that how timer control works, how we can get or set time interval and how to start and stop timer in VB and C#.net.

Article Comments
helped me alot

Posted on 7/20/2010 3:38:01 PM by pico farad

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to draw an image on the form using Graphics class in vb.net - C #.net.

Disclaimer - Privacy
© 2002-2017 DevASP.net