Public Class Form1 Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents TxbPrevReading As System.Windows.Forms.TextBox Friend WithEvents TxbCurrReading As System.Windows.Forms.TextBox Friend WithEvents Calculate As System.Windows.Forms.Button Friend WithEvents Button2 As System.Windows.Forms.Button Friend WithEvents LblKWH As System.Windows.Forms.Label Friend WithEvents Lblcharge As System.Windows.Forms.Label Friend WithEvents Lbltotal As System.Windows.Forms.Label Friend WithEvents Lblsalestax As System.Windows.Forms.Label Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents Label2 As System.Windows.Forms.Label Friend WithEvents Label3 As System.Windows.Forms.Label Friend WithEvents Label4 As System.Windows.Forms.Label Friend WithEvents Label5 As System.Windows.Forms.Label Friend WithEvents Label6 As System.Windows.Forms.Label Private Sub InitializeComponent() Me.TxbPrevReading = New System.Windows.Forms.TextBox Me.TxbCurrReading = New System.Windows.Forms.TextBox Me.Calculate = New System.Windows.Forms.Button Me.Button2 = New System.Windows.Forms.Button Me.LblKWH = New System.Windows.Forms.Label Me.Lblcharge = New System.Windows.Forms.Label Me.Lbltotal = New System.Windows.Forms.Label Me.Lblsalestax = New System.Windows.Forms.Label Me.Label1 = New System.Windows.Forms.Label Me.Label2 = New System.Windows.Forms.Label Me.Label3 = New System.Windows.Forms.Label Me.Label4 = New System.Windows.Forms.Label Me.Label5 = New System.Windows.Forms.Label Me.Label6 = New System.Windows.Forms.Label Me.SuspendLayout() ' 'TxbPrevReading ' Me.TxbPrevReading.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.TxbPrevReading.Location = New System.Drawing.Point(32, 48) Me.TxbPrevReading.Name = "TxbPrevReading" Me.TxbPrevReading.TabIndex = 0 Me.TxbPrevReading.Text = "" ' 'TxbCurrReading ' Me.TxbCurrReading.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.TxbCurrReading.Location = New System.Drawing.Point(32, 96) Me.TxbCurrReading.Name = "TxbCurrReading" Me.TxbCurrReading.TabIndex = 1 Me.TxbCurrReading.Text = "" ' 'Calculate ' Me.Calculate.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Calculate.Location = New System.Drawing.Point(168, 32) Me.Calculate.Name = "Calculate" Me.Calculate.Size = New System.Drawing.Size(88, 23) Me.Calculate.TabIndex = 2 Me.Calculate.Text = "&Calculate" ' 'Button2 ' Me.Button2.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Button2.Location = New System.Drawing.Point(168, 88) Me.Button2.Name = "Button2" Me.Button2.Size = New System.Drawing.Size(88, 23) Me.Button2.TabIndex = 3 Me.Button2.Text = "E&xit." ' 'LblKWH ' Me.LblKWH.BackColor = System.Drawing.SystemColors.Info Me.LblKWH.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.LblKWH.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.LblKWH.Location = New System.Drawing.Point(160, 152) Me.LblKWH.Name = "LblKWH" Me.LblKWH.TabIndex = 4 Me.LblKWH.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ' 'Lblcharge ' Me.Lblcharge.BackColor = System.Drawing.SystemColors.Info Me.Lblcharge.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.Lblcharge.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Lblcharge.Location = New System.Drawing.Point(160, 184) Me.Lblcharge.Name = "Lblcharge" Me.Lblcharge.TabIndex = 5 Me.Lblcharge.TextAlign = System.Drawing.ContentAlignment.MiddleRight ' 'Lbltotal ' Me.Lbltotal.BackColor = System.Drawing.SystemColors.Info Me.Lbltotal.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.Lbltotal.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Lbltotal.Location = New System.Drawing.Point(160, 248) Me.Lbltotal.Name = "Lbltotal" Me.Lbltotal.TabIndex = 6 Me.Lbltotal.TextAlign = System.Drawing.ContentAlignment.MiddleRight ' 'Lblsalestax ' Me.Lblsalestax.BackColor = System.Drawing.SystemColors.Info Me.Lblsalestax.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.Lblsalestax.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Lblsalestax.Location = New System.Drawing.Point(160, 216) Me.Lblsalestax.Name = "Lblsalestax" Me.Lblsalestax.TabIndex = 7 Me.Lblsalestax.TextAlign = System.Drawing.ContentAlignment.MiddleRight ' 'Label1 ' Me.Label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.Label1.Location = New System.Drawing.Point(24, 216) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(120, 23) Me.Label1.TabIndex = 11 Me.Label1.Text = " Sales Tax:" Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft ' 'Label2 ' Me.Label2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.Label2.Location = New System.Drawing.Point(24, 248) Me.Label2.Name = "Label2" Me.Label2.Size = New System.Drawing.Size(120, 23) Me.Label2.TabIndex = 10 Me.Label2.Text = " Total Bill:" Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft ' 'Label3 ' Me.Label3.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.Label3.Location = New System.Drawing.Point(24, 184) Me.Label3.Name = "Label3" Me.Label3.Size = New System.Drawing.Size(120, 23) Me.Label3.TabIndex = 9 Me.Label3.Text = " Electricity Consumed:" Me.Label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft ' 'Label4 ' Me.Label4.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.Label4.Location = New System.Drawing.Point(24, 152) Me.Label4.Name = "Label4" Me.Label4.Size = New System.Drawing.Size(120, 23) Me.Label4.TabIndex = 8 Me.Label4.Text = " KWH Consumed:" Me.Label4.TextAlign = System.Drawing.ContentAlignment.MiddleLeft ' 'Label5 ' Me.Label5.Location = New System.Drawing.Point(24, 24) Me.Label5.Name = "Label5" Me.Label5.Size = New System.Drawing.Size(128, 24) Me.Label5.TabIndex = 12 Me.Label5.Text = "Previous Meter Reading" Me.Label5.TextAlign = System.Drawing.ContentAlignment.MiddleLeft ' 'Label6 ' Me.Label6.Location = New System.Drawing.Point(24, 72) Me.Label6.Name = "Label6" Me.Label6.Size = New System.Drawing.Size(128, 24) Me.Label6.TabIndex = 13 Me.Label6.Text = "Current Meter Reading" Me.Label6.TextAlign = System.Drawing.ContentAlignment.MiddleLeft ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 293) Me.Controls.Add(Me.TxbCurrReading) Me.Controls.Add(Me.TxbPrevReading) Me.Controls.Add(Me.Label6) Me.Controls.Add(Me.Label5) Me.Controls.Add(Me.Label1) Me.Controls.Add(Me.Label2) Me.Controls.Add(Me.Label3) Me.Controls.Add(Me.Label4) Me.Controls.Add(Me.Lblsalestax) Me.Controls.Add(Me.Lbltotal) Me.Controls.Add(Me.Lblcharge) Me.Controls.Add(Me.LblKWH) Me.Controls.Add(Me.Button2) Me.Controls.Add(Me.Calculate) Me.Name = "Form1" Me.Text = " Electric Bill" Me.ResumeLayout(False) End Sub #End Region Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Me.Close() End Sub Private Sub Txb_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TxbCurrReading.KeyPress, TxbPrevReading.KeyPress If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso e.KeyChar <> ControlChars.Back Then e.Handled = True End If If e.KeyChar = ControlChars.Cr Then If Me.TxbPrevReading.Text <> "" AndAlso Me.TxbCurrReading.Text <> "" Then Me.Calculate.PerformClick() Else Me.TxbCurrReading.Focus() End If End If End Sub Private Sub Calculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Calculate.Click Dim electricityCharge, salesTax As Double Dim previousMeterReading, currentMeterReading, kwhConsumed As Integer If Me.TxbCurrReading.Text <> "" AndAlso Me.TxbPrevReading.Text <> "" Then currentMeterReading = Me.TxbCurrReading.Text previousMeterReading = Me.TxbPrevReading.Text kwhConsumed = currentMeterReading - previousMeterReading If kwhConsumed < 0 Then MsgBox(" The Current Meter Reading have to be greater than the Previous", MsgBoxStyle.Critical, " Wrong entry") Me.TxbCurrReading.SelectAll() Else electricityCharge = ComputeElectricityCharge(kwhConsumed) salesTax = ComputeSalesTax(electricityCharge) Me.Lblcharge.Text = Format(electricityCharge, "currency") Me.LblKWH.Text = kwhConsumed Me.Lblsalestax.Text = Format(salesTax, "currency") Me.Lbltotal.Text = Format(salesTax + electricityCharge, "currency") Me.TxbCurrReading.Focus() End If End If End Sub Private Function ComputeElectricityCharge(ByVal kwh As Integer) As Double Dim amount As Double Select Case kwh Case Is < 500 amount = kwh * 0.05 Case 500 To 1000 amount = 25 + (kwh - 500) * 0.055 Case Is > 1000 amount = 52.5 + (kwh - 1000) * 0.06 End Select amount = Math.Round(amount, 2) Return amount End Function Public Function ComputeSalesTax(ByVal amount As Double) As Double Const SALES_TAX_RATE As Double = 0.085 Dim salesTax As Double salesTax = SALES_TAX_RATE * amount salesTax = Math.Round(salesTax, 2) Return salesTax End Function End Class