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.
  • 4076

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.

Clipboard01.jpg

Step2:
 Click on VB and select the windows category, a page will open with a form.

Clipboard02.jpg

Step3: Now open a toolbox and controls on the form i.e 5LabelBox, 4TextBox,2Button.

Clipboard03.jpg

Step4: Edit the Buttons by Calculate and Exit by clicking on the properties of it.

  clipboard04 (1).jpg

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.ObjectByVal 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.EventArgsHandlesButton2.Click
         Me
.Close() 
    End 
Sub

End Class

Press f5 for the output you will see the first page like this.

1.jpg

When you enter the value more than 400 it will not accept.

2.jpg

When you enter the value less than 400, it will accept the value and will show all values.

3.jpg



  

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.