Event Handling on Click Event in VB.NET
Here i will discuss the Event Handler for the Click event of the Calculate and Exit button.
Introduction
Here i will discuss the Event Handler for the Click event of the Calculate and Exit button. In this Calculate button calculates the discount percent, discount amount, and total based on the subtotal entered by the user whereas Exit button contains one statement that executes the Close method of the form. In this i have apply a condition, if a user enter the value more than 400 , it will show value less than 400 will be accept and if user enter the value less than 400 it will calculate it. To perform this example follow these steps:
Step1: Open the Visual Studio 2010, click on new project.
Step2: Click on VB and select the windows category, a page will open with a form.
Step3: Now open a toolbox and controls on the form i.e 5LabelBox, 4TextBox,2Button.
Step4: Edit the Buttons by Calculate and Exit by clicking on the properties of it.
Step5: Double click on the Buttons, it generates default code in which you can enter the rest of the code.
Coding for the Event is shown below.
Public Class Total
Private Sub Button1_Calculate(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles Button1.Click
Dim discountPercent As Decimal
If TextSubtotal.Text >= 400 Then
discountPercent = 0.4
Label5.Text = "<400 will be acept"
Else
If TextSubtotal.Text >= 200 And TextSubtotal.Text < 400 Then
discountPercent = 0.3
Else
If TextSubtotal.Text >= 100 And TextSubtotal.Text < 200 Then
discountPercent = 0.2
Label5.Text = ""
Else
discountPercent = 0
End If
Dim discountAmount As Decimal = TextSubtotal.Text * discountPercent
Dim Total1 As Decimal = TextSubtotal.Text - discountAmount
TextDiscountpercent.Text = FormatPercent(discountPercent, 1)
TextDiscountamount.Text = FormatNumber(discountAmount)
TextTotal.Text = FormatNumber(Total1)
TextSubtotal.Select()
End If
End If
End Sub
Private Sub Button2_Exit(sender As System.Object, e As System.EventArgs) HandlesButton2.Click
Me.Close()
End Sub
End Class
Press f5 for the output you will see the first page like this.
When you enter the value more than 400 it will not accept.
When you enter the value less than 400, it will accept the value and will show all values.