C

Simplified tutorials to learn the popular basic language C

VB.Net

Learn the basic desktop development application language

CodeIgniter

One of the popular PHP three tier (MVC) framework

Working with Button and Click Event in VB


Tip Abstraction:
        This visual basic tutorial describes the working of button element and its most relevant event


We have been learning the working of Textbox, Variables, etc. In those examples working of all codes are based on form load events.  Form load event is useful but some other events like button click, checkbox check, etc are more usable and popular.

Here let us learn the working of button and its click events.

Button Control in the Basis of VB.Net

It is easy to add a button control in to our form. Just select button control from the ToolBox and drag or draw on the form window.
The main event of a button is its click event. Like getting the form load event just double click on the button control to get is click event space.

Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

   

    End Sub
End Class

We have to write our VB codes inside these event lines.
What can we do now with this button event.? We can do all thing that we have done with form load event in our previous lesson.

Now let us add another label and a textbox with name ‘answerbox’. Make the label Text into ‘Answer:’.

Button click event on visual basic

Now we are going to reach (transfer) the text on the first text box (now named ‘valuebox’) into the second textbox (answerbox).

You know that the Textbox.Text denotes the text on the specified textbox.
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim textvalue As String
        textvalue = valuebox.Text
        answerbox.Text = textvalue

    End Sub
End Class

Code Explanation

Dim value As String
- Here we declare a string typed variable named ‘textvalue’. Learn more about variable declaration

textvalue = valuebox.Text
-Variable 'textvalue' gets the value on the textbox named ‘valuebox’.

answerbox.Text = textvalue
-Here the 'answerbox' textbox gets the value of variable ‘textvalue’.

Transferring textbox value into another

You can simply do the same in a single line of code without declaring any variable.
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        answerbox.Text = valuebox.Text

    End Sub
End Class

Find the Square of a Number using VB


This may be your first VB program. This program finds the square of the given number.
The codes are similar to above codes of transferring a text to another textbox.
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim number As Integer

        number = valuebox.Text

        answerbox.Text = number * number

    End Sub
End Class


The variable ‘number’ gets the value on the 'valuebox'. Then ‘answerbox’ shows the square (number * number) of the given number.

Find the square of a number in vb

Note:
You can change the size of textbox as well as button by dragging from its corners' small boxes that shown when you select an element.

Resizing a button textbox in VB


No comments:

Post a Comment