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
 

FREE 12 month online training for ASP.NET & MS Expression Studio and a Free copy of MS Expression Web with Windows Server Purchase
Working with timer control (Tag, Timer tick) in VB.net - C#.net Part2

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

In this simple article you will learn that how timer control works, how we can tag timer and how to use timer tick event in VB and C#.net.

 User State:

In timer control you can set or get user state with it. To do this we have a property named tag. This property is used to associate a user state with timer control. It takes and returns a string value which represents the user state.

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_settag_Click(object sender, EventArgs e)

        {

            if (txt_gettag.Text != string.Empty)

            {

                string str = txt_gettag.Text;

                timer1.Tag = str;

                MessageBox.Show("User state");

            }

            else

                MessageBox.Show("Please enter value");

        }

VB

Private Sub btn_settag_Click(ByVal sender As ObjectByVal As EventArgs)
        
If (txt_gettag.Text <> string.Empty) Then
            Dim str As String = 
txt_gettag.Text
            timer1.Tag 
= str
            
MessageBox.Show("User state")
        
Else
            
MessageBox.Show("Please enter value")
        
End If
    End Sub

This simple code sets the user state.

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 txt_gettag_Click(object sender, EventArgs e)

        {

            string str = timer1.Tag;

            MessageBox.Show(str);

        }

VB

Private Sub txt_gettag_Click(ByVal sender As ObjectByVal As EventArgs)
        
Dim str As String = timer1.Tag
        MessageBox.Show(
str)
    
End Sub

This simple code gets the user state.

Timer Tick:

The most important part of the timer control is timer tick event. This is the event that we can use to perform our task. Now the question is why this event gets called. This event is called when the timer control completes its time interval. In fist part of this article we learned that how to set interval time for timer. When timer is start it calculates the time and after reaching to the specified interval it calls its tick event. Any task which we want to perform after the specified interval it can be done under tick event. Here we will display a simple message box but we can do any operation which we want to do.

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

Now write the following code on Tick event:

C#

private void timer(object sender, EventArgs e)

        {

            try

            {

                MessageBox.Show("This is timer tick");

            }catch(Exception ex){}

        }

VB

Private Sub timer(ByVal sender As ObjectByVal As EventArgs)
        
Try 
            
MessageBox.Show("This is timer tick")
        
Catch ex As Exception
            
        End Try
    End Sub

This simple code performs the operation on tick event.

Now write the following code on FORM LOAD event:

C#

private void Form1_Load(object sender, EventArgs e)

        {

            this.Text = "DEVASP TIMER APPLICATION";

            timer1.interval = 2000;

            timer1.Start();

            MessageBox.Show("Timer has started");

        }

VB

Private Sub Form1_Load(ByVal sender As ObjectByVal As EventArgs)
        
Me.Text "DEVASP TIMER APPLICATION"
        
timer1.interval 2000
        
timer1.Start
        MessageBox.Show(
"Timer has started")
    
End Sub

This simple article tells that how timer control works, how we can tag timer and how to use timer tick event in VB and C#.net.

Article Comments
Really nice and impressive blog i found today.

Posted on 7/19/2010 7:10:55 PM by cheap mbt

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Working with timer control (Time interval, Start, Stop) in VB.net - C#.net Part1

Disclaimer - Privacy
© 2002-2014 DevASP.net