How to create Counter Application in VB.NET

In this post I will show you How to create a click counter in VB.NET, how can we make a Button click counter in VB.NET. And how can we make an Auto counter in VB.NET?

Create Counter Application in VB.NET

If You want to make A program In Which You want to count the total number of clicks of a button and then want to display the no. of count in a textbox using VB.NET so here is the solution for you.

Design Form:

Open your Visual Studio > Create A New Project In Visual Basic > On Form1 take one label, one Textbox for displaying the total clicks, and one button which will count the total number of clicks.

Now double click on Button1 control and write the below code there.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Static Counter
        Counter = Counter + 1
        TextBox1.Text = Counter
    End Sub

Here we declare a static variable counter which will work when the button will be clicked and it increases the counter variable value by 1 and then displaying it into TextBox1 by adding the line “TextBox1.Text = Counter” which will add the counter value in TextBox1 how many times the button is clicked or a total number of clicks.

Output:

Create Auto Counter in visual basic

Here we want an Auto Counter that should be increased on the basis of time slot like every 5 seconds. We can make this application with timer control from the toolbox.

What is Timer Control?

If we want to run some code after a certain interval of time continuously, so we can use the Timer control. The timer control is present in the visual studio toolbox that can be dragged and dropped on the form directly. Here Timer Control allows us to set Interval property in milliseconds like(1 second equal to 1000 milliseconds). For example, if we want to set an interval of one minute we set the value at the Interval property as 60000, which means 60×1000.

The Timer Control runs only when its Enabled property is set True, by default Enabled property is always False.

The Timer Control has a start and stops method to perform start and stop its functioning.

Design Form:

Open your Visual Studio > Create A New Project In Visual Basic > On Form1 drag and drop one Lable, one button and one timer control put it on the Textbox. Remove the text from the text property of label1 so on the runtime it will not show written as label1.

double click on the form and write the following code first declare a variable Dim second As Integer just below the public class. Then write the leftover code respectively on form load event, Timer1, and on Button1.

Public Class Form1
    Dim second As Integer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1.Interval = 5000
        Timer1.Start()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Label1.Text = second
        second = second + 1
    End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      Timer1.Stop()
   End Sub
End Class

Now run the application.

Output:

Running of form first it start the counter with 0 then after 5 sec it will show 1 in every 5 sec it increases the counter like 2…3…4 and so on.

 

When we click on the stop Button it Stops the counter. As shown below image and we can terminate our application.

So in this way, we can create Button Click Counter and Auto Counter in VB.NET.