thongkorn โพสต์ 2019-5-30 12:30:08

[VB6/VB.NET] แจกโค้ดการคำนวณหาอัตราค่าใช้ไฟฟ้าในบ้านเรือนที่อยู่อาศัย แบบขั้นบันได

http://www.g2gnet.com/webboard/images/vbnet/calelectrical.png

นี่เป็นโค้ดโปรแกรมที่ คำนวณหาอัตราค่าไฟฟ้าที่ต้องจ่ายให้กับการไฟฟ้าส่วนภูมิภาค ตามใบแจ้งหนี้ในแต่ละเดือนน่ะครับ ไม่ใช่โปรแกรมที่ใช้คำนวณหาพลังงาน จากอุปกรณ์ไฟฟ้าต่างๆที่มีอยู่ในบ้าน หรือคำนวณหา Power Factor หรืออะไรต่อมิอะไรทางไฟฟ้า ซึ่งเป็นการคิดค่าไฟฟ้าแบบขั้นบันได (ดูรายละเอียดประเภทที่ 1 บ้านอยู่อาศัย) โดยคิดจากการใช้หน่วยไฟฟ้าหลักๆ คือ หากไม่เกิน 150 และเกิน 150 หน่วยต่อเดือน แอดมินขอยกตัวอย่างจริงที่บ้านแอดมินก็แล้วกัน ...

http://www.g2gnet.com/webboard/images/vbnet/calelectricalrate.png

วิธีการคิดแบบขั้นบันได ... (ดูภาพตารางประกอบ)

สมมุติว่าใช้ไฟจำนวน 636 หน่วย ซึ่งมากกว่า 400 หน่วย โดยมีค่า FT (ปัจจุบัน) = -0.1160 บาท
ต้องแยกการคำนวณออกเป็น 3 ส่วน คือ
ส่วนที่ไม่เกิน 150 หน่วย (อัตราต่อหน่วย = 3.2484 บาท)
ส่วนที่เกิน 150 หน่วย แต่ไม่เกิน 400 หน่วย หรือ 400 - 150 = 250 หน่วย (อัตราต่อหน่วย = 4.2218 บาท)
ส่วนที่เกิน 400 หน่วยขึ้นไป (อัตราต่อหน่วย = 4.4217 บาท)

(1) 150 หน่วยแรก (150 x 3.2484) = 487.26 บาท ...
(2) 250 หน่วยต่อไป (250 x 4.2218) = 1,055.45 บาท
หน่วยที่เกินจาก 400 หน่วย คือ 636 - 400 = 236 หน่วย
(3) หน่วยที่เหลือ (236 x 4.4217) = 1,043.52 บาท (สังเกตว่าเปลี่ยนตัวคูณด้วย)

(4) ค่า FT (จำนวนหน่วย x FT) หรือ 636 x -0.1160 = -73.78 บาท
(5) ค่าบริการรายเดือน = 38.22 บาท

(6) เอา (1) + (2) + (3) + (4) + (5) รวมเป็นเงิน = 2,550.68 บาท
(7) คิดภาษี 7% = 178.55 บาท
(8) เอา (6) + (7) รวมเป็นเงินที่ต้องชำระ = 2,729.23 บาท

มาดูโค้ดต้นฉบับกันเถอะ (VB.NET) ...
Public Class frmCalElectrical

    Private Sub frmCalElectrical_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
      Me.Dispose()
      Application.Exit()
    End Sub

    Private Sub frmCalElectrical_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
      Select Case e.KeyCode
            Case Keys.F8
                Call btnProcess_Click(sender, e)
            Case Keys.F10
                Me.Close()
      End Select
    End Sub

    Private Sub frmCalElectrical_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
      Call SetupGridView(Me.DataGridView1)
      Call SetupGridView(Me.DataGridView2)
      Call InitData()
      txtUnit.Text = ""
    End Sub

    Private Sub SetupGridView(ByRef DGV As DataGridView)
      '// Initialize DataGridView Control
      With DGV
            .RowHeadersVisible = False
            .AllowUserToAddRows = False
            .AllowUserToDeleteRows = False
            .AllowUserToResizeRows = False
            .MultiSelect = False
            .SelectionMode = DataGridViewSelectionMode.CellSelect
            .Font = New Font("Tahoma", 9)
            ' Autosize Column
            .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
            .AutoResizeColumns()
            '// Even-Odd Color
            .AlternatingRowsDefaultCellStyle.BackColor = Color.AliceBlue
            ' Adjust Header Styles
            With .ColumnHeadersDefaultCellStyle
                .BackColor = Color.Navy
                .ForeColor = Color.Black ' Color.White
                .Font = New Font("Tahoma", 9, FontStyle.Bold)
            End With
      End With
    End Sub

    ' / Initialized DataGridView and put the sample data.
    Private Sub InitData()
      txtSummary.Text = ""
      txtMonthFee1.Text = "8.19"
      txtMonthFee2.Text = "38.22"
      txtFT.Text = "-0.1160"
      txtUnit.Text = "0"
      '//
      Me.DataGridView1.Columns.Clear()
      Me.DataGridView2.Columns.Clear()
      '// Declare columns type.
      Dim Column1 As New DataGridViewTextBoxColumn()
      Dim Column2 As New DataGridViewTextBoxColumn()
      '// Add new Columns
      DataGridView1.Columns.AddRange(New DataGridViewColumn() { _
                Column1, Column2 _
                })
      With DataGridView1
            .Columns(0).Name = "UnitName"
            .Columns(0).HeaderText = "จำนวนหน่วย"
            .Columns(0).ReadOnly = True
            .Columns(1).Name = "UnitPrice"
            .Columns(1).HeaderText = "ค่าพลังงานไฟฟ้า (บาท/หน่วย)"
            .Columns(1).ReadOnly = False
      End With
      '// DataGridView1
      Dim row = New String() {"15 หน่วยแรก (หน่วยที่ 0 – 15)", "2.3488"}
      DataGridView1.Rows.Add(row)
      row = {"10 หน่วยต่อไป (หน่วยที่ 16 – 25)", "2.9882"}
      DataGridView1.Rows.Add(row)
      row = {"10 หน่วยต่อไป (หน่วยที่ 26 – 35)", "3.2405"}
      DataGridView1.Rows.Add(row)
      row = {"65 หน่วยต่อไป (หน่วยที่ 36 – 100)", "3.6237"}
      DataGridView1.Rows.Add(row)
      row = {"50 หน่วยต่อไป (หน่วยที่ 101 – 150)", "3.7171"}
      DataGridView1.Rows.Add(row)
      row = {"250 หน่วยต่อไป (หน่วยที่ 151 – 400)", "4.2218"}
      DataGridView1.Rows.Add(row)
      row = {"เกิน 400 หน่วยขึ้นไป (หน่วยที่ 401 เป็นต้นไป)", "4.4217"}
      DataGridView1.Rows.Add(row)
      With Me.DataGridView1
            .Columns(1).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
            .Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
      End With

      '// DataGridView2
      '// Declare columns type.
      Dim Col1 As New DataGridViewTextBoxColumn()
      Dim Col2 As New DataGridViewTextBoxColumn()
      '// Add new Columns
      DataGridView2.Columns.AddRange(New DataGridViewColumn() { _
                Col1, Col2 _
                })
      With DataGridView2
            .Columns(0).Name = "UnitName"
            .Columns(0).HeaderText = "จำนวนหน่วย"
            .Columns(0).ReadOnly = True
            .Columns(1).Name = "UnitPrice"
            .Columns(1).HeaderText = "ค่าพลังงานไฟฟ้า (บาท/หน่วย)"
            .Columns(1).ReadOnly = False
      End With
      row = {"150 หน่วยแรก         (หน่วยที่ 0 – 150)", "3.2484"}
      DataGridView2.Rows.Add(row)
      row = {"250 หน่วยต่อไป (หน่วยที่ 151 – 400)", " 4.2218"}
      DataGridView2.Rows.Add(row)
      row = {"เกิน 400 หน่วยขึ้นไป (หน่วยที่ 401 เป็นต้นไป)", "4.4217"}
      DataGridView2.Rows.Add(row)
      With Me.DataGridView2
            .Columns(1).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
            .Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
      End With
    End Sub

    Private Sub btnProcess_Click(sender As System.Object, e As System.EventArgs) Handles btnProcess.Click
      '// Trap Error
      If txtFT.Text = "" Then txtFT.Text = "0.00"
      If txtUnit.Text = "" Then txtUnit.Text = "0"
      If txtMonthFee1.Text = "" Then txtMonthFee1.Text = "0.00"
      If txtMonthFee2.Text = "" Then txtMonthFee2.Text = "0.00"
      '// เช็คค่าว่างในเซลล์
      For i = 0 To Me.DataGridView1.RowCount - 1
            If IsNothing(Me.DataGridView1.Rows(i).Cells(1).Value) Then Me.DataGridView1.Rows(i).Cells(1).Value = 0.0
      Next
      For i = 0 To Me.DataGridView2.RowCount - 1
            If IsNothing(Me.DataGridView2.Rows(i).Cells(1).Value) Then Me.DataGridView2.Rows(i).Cells(1).Value = 0.0
      Next
      '// CALCULATE
      Call CalElectrical()
    End Sub

    '// คำนวณผล
    Private Sub CalElectrical()
      Dim FirstUnit As Double                     ' ไม่เกิน 150 หน่วย (150 หน่วยแรก)
      Dim SecondUnit As Double            ' มากกว่า 150 หน่วย แต่ไม่เกิน 400 หน่วย (หรือ 400 - 150 = 250 หน่วย)
      Dim ThirdUnit As Double                   ' มากกว่า 400 หน่วยขึ้นไป
      Dim SumElectricCost As Double   ' (1) ผลรวมของค่าไฟฟ้ามาตรฐาน
      Dim SumFT As Double                     ' (2). ค่า FT คูณจำนวนหน่วย
      Dim Vat As Double                               ' (3). เอา 1 + 2 แล้วหาค่าภาษี 7%
      Dim TotalAmount As Double            ' (4)เอา 1 + 2 + 3 คืออัตราค่าไฟฟ้าทั้งหมด

      ' คิดแบบขั้นบันไดกรณีใช้พลังงานไฟฟ้าไม่เกิน 150 หน่วย/เดือน
      Dim intStep As Double
      Select Case Val(txtUnit.Text)
            Case 1 To 15 : intStep = Me.DataGridView1.Rows(0).Cells(1).Value
            Case 16 To 25 : intStep = Me.DataGridView1.Rows(1).Cells(1).Value
            Case 26 To 35 : intStep = Me.DataGridView1.Rows(2).Cells(1).Value
            Case 36 To 100 : intStep = Me.DataGridView1.Rows(3).Cells(1).Value
            Case 101 To 150 : intStep = Me.DataGridView1.Rows(4).Cells(1).Value
            Case 151 To 400 : intStep = Me.DataGridView1.Rows(5).Cells(1).Value
            Case Else : intStep = Me.DataGridView1.Rows(6).Cells(1).Value
      End Select

      ' / ---------------------------------------------------------------
      ' กรณีแรกใช้ไฟฟ้าระหว่าง 0 - 150 หน่วย
      If Val(txtUnit.Text) <= 150 Then
            FirstUnit = Val(txtUnit.Text) * intStep
            ' บวกค่าบริการด้วย (สงสัยว่ามันบริการอะไร ...)
            SumElectricCost = FirstUnit + Val(txtMonthFee1.Text) 'FeePaid
            SumFT = Val(txtUnit.Text) * Val(txtFT.Text)
            Vat = ((SumElectricCost + SumFT) * 7) / 100
            TotalAmount = SumElectricCost + SumFT + Vat
            txtSummary.Text = _
                "==================" & vbCrLf & _
                "รวมจำนวนเงิน" & vbTab & " = " & Format(SumElectricCost, "#,##0.00") & vbCrLf & _
                "ค่า FT" & vbTab & vbTab & " = " & Format(SumFT, "#,##0.00") & vbCrLf & _
                "==================" & vbCrLf & _
                "รวมเงินค่าไฟฟ้า" & vbTab & " = " & Format(SumElectricCost + SumFT, "#,##0.00") & vbCrLf & _
                "ภาษีมูลค่าเพิ่ม" & vbTab & " = " & Format(Vat, "#,##0.00") & vbCrLf & vbCrLf & _
                "รวมเงินที่ต้องชำระ: " & vbTab & " = " & Format(TotalAmount, "#,##0.00") & " บาท."

            '// -------------------------------------------------------------------------------
            '//ใช้ไฟฟ้าระหว่าง 151 - 400 หน่วย (หรือ 400 - 150 = 250 หน่วย)
      ElseIf (Val(txtUnit.Text) > 150) And (Val(txtUnit.Text) <= 400) Then
            FirstUnit = 150 * Me.DataGridView2.Rows(0).Cells(1).Value
            SecondUnit = 250 * Me.DataGridView2.Rows(1).Cells(1).Value
            SumElectricCost = Format(FirstUnit + SecondUnit, "#,##0.00")
            SumFT = Val(txtUnit.Text) * Val(txtFT.Text)
            Vat = ((SumElectricCost + SumFT) * 7) / 100
            TotalAmount = SumElectricCost + SumFT + Vat
            '//
            txtSummary.Text = _
                "150 หน่วยแรก" & vbTab & " = " & Format(FirstUnit, "#,##0.00") & vbCrLf & _
                "250 หน่วยต่อไป" & vbTab & " = " & Format(SecondUnit, "#,##0.00") & vbCrLf & _
                "==================" & vbCrLf & _
                "รวมจำนวนเงิน" & vbTab & " = " & Format(SumElectricCost, "#,##0.00") & vbCrLf & _
                "ค่า FT" & vbTab & vbTab & " = " & Format(SumFT, "#,##0.00") & vbCrLf & _
                "==================" & vbCrLf & _
                "รวมเงินค่าไฟฟ้า" & vbTab & " = " & Format(SumElectricCost + SumFT, "#,##0.00") & vbCrLf & _
                "ภาษีมูลค่าเพิ่ม" & vbTab & " = " & Format(Vat, "#,##0.00") & vbCrLf & vbCrLf & _
                "รวมเงินที่ต้องชำระ: " & vbTab & " = " & Format(TotalAmount, "#,##0.00") & " บาท."

            ' / ---------------------------------------------------------------
            ' ใช้ไฟฟ้าตั้งแต่ 401 หน่วยขึ้นไป
      ElseIf (Val(txtUnit.Text) > 400) Then
            FirstUnit = Format(150 * Me.DataGridView2.Rows(0).Cells(1).Value, "#,##0.0000")
            SecondUnit = Format(250 * Me.DataGridView2.Rows(1).Cells(1).Value, "#,##0.0000")
            ThirdUnit = Format((Val(txtUnit.Text) - 400) * Me.DataGridView2.Rows(2).Cells(1).Value, "#,##0.0000")
            'SumElectricCost = FirstUnit + SecondUnit + ThirdUnit + 38.22 'FeePaid
            SumElectricCost = Format(FirstUnit + SecondUnit + ThirdUnit, "#,##0.0000")         'FeePaid
            SumFT = Format(Val(txtUnit.Text) * Val(txtFT.Text), "#,##0.0000")
            Vat = Format(((SumElectricCost + SumFT + Val(txtMonthFee2.Text)) * 7) / 100, "#,##0.0000")
            TotalAmount = Format(SumElectricCost + SumFT + Vat + Val(txtMonthFee2.Text), "#,##0.0000")
            '//
            txtSummary.Text = _
                "150 หน่วยแรก" & vbTab & " = " & Format(FirstUnit, "#,##0.00") & vbCrLf & _
                "250 หน่วยต่อไป" & vbTab & " = " & Format(SecondUnit, "#,##0.00") & vbCrLf & _
                (Val(txtUnit.Text) - 400) & " หน่วยที่เหลือ" & vbTab & " = " & Format(ThirdUnit, "#,##0.00") & vbCrLf & _
                "==================" & vbCrLf & _
                "รวมจำนวนเงิน" & vbTab & " = " & Format(SumElectricCost, "#,##0.00") & vbCrLf & _
                "ค่าบริการ" & vbTab & vbTab & " = " & Format(Val(txtMonthFee2.Text), "#,##0.00") & vbCrLf & _
                "ค่า FT" & vbTab & vbTab & " = " & Format(SumFT, "#,##0.00") & vbCrLf & _
                "==================" & vbCrLf & _
                "รวมเงินค่าไฟฟ้า" & vbTab & " = " & Format(SumElectricCost + SumFT + Val(txtMonthFee2.Text), "#,##0.00") & vbCrLf & _
                "ภาษีมูลค่าเพิ่ม" & vbTab & " = " & Format(Vat, "#,##0.00") & vbCrLf & vbCrLf & _
                "รวมเงินที่ต้องชำระ: " & vbTab & " = " & Format(TotalAmount, "#,##0.00") & " บาท."
      End If
    End Sub

    ' / ---------------------------------------------------------------
    ' / หากมีการแก้ไขในเซลล์ของตารางกริด เมื่อสิ้นสุดการแก้ไขให้เช็คค่าในหลัก Index = 1 (เก็บค่าอัตราราคา)
    Private Sub DataGridView1_CellEndEdit(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
      Select Case DataGridView1.Columns(DataGridView1.CurrentCell.ColumnIndex).Index
            Case 1
                If IsNothing(Me.DataGridView1.Rows(e.RowIndex).Cells(1).Value) Then Me.DataGridView1.Rows(e.RowIndex).Cells(1).Value = 0.0
      End Select
    End Sub

    Private Sub DataGridView2_CellEndEdit(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView2.CellEndEdit
      Select Case DataGridView2.Columns(DataGridView2.CurrentCell.ColumnIndex).Index
            Case 1
                If IsNothing(Me.DataGridView2.Rows(e.RowIndex).Cells(1).Value) Then Me.DataGridView2.Rows(e.RowIndex).Cells(1).Value = 0.0
      End Select
    End Sub
    ' / ---------------------------------------------------------------

    ' / ---------------------------------------------------------------
    ' / ขณะทำการแก้ไขข้อมูลในเซลล์ ตรวจสอบคีย์ที่กดลงไป
    Private Sub DataGridView1_EditingControlShowing(sender As Object, e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
      Select Case DataGridView1.Columns(DataGridView1.CurrentCell.ColumnIndex).Index
            '// ColumeIndex 1 is double.
            Case 1
                '// Force to validate value at ValidKeyPress Event.
                RemoveHandler e.Control.KeyPress, AddressOf ValidKeyPress
                AddHandler e.Control.KeyPress, AddressOf ValidKeyPress
      End Select
    End Sub

    Private Sub DataGridView2_EditingControlShowing(sender As Object, e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView2.EditingControlShowing
      Select Case DataGridView1.Columns(DataGridView1.CurrentCell.ColumnIndex).Index
            '// ColumeIndex 1 is double.
            Case 1
                '// Force to validate value at ValidKeyPress Event.
                RemoveHandler e.Control.KeyPress, AddressOf ValidKeyPress
                AddHandler e.Control.KeyPress, AddressOf ValidKeyPress
      End Select
    End Sub
    ' / ---------------------------------------------------------------

    ' / ---------------------------------------------------------------
    ' / เช็คการกดคีย์ที่อยู่ในเซลล์ของตารางกริด ให้รับค่าตัวเลขแบบทศนิยมได้เท่านั้น
    Private Sub ValidKeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs)
      Dim tb As TextBox = sender
      Select Case DataGridView1.CurrentCell.ColumnIndex
            Case 1'// Double
                Select Case e.KeyChar
                  Case "0" To "9"
                        '// Allowed "."
                  Case "."
                        '// But it can present "." only one.
                        If InStr(tb.Text, ".") Then e.Handled = True

                  Case ChrW(Keys.Back)
                  Case Else
                        e.Handled = True
                End Select
      End Select
    End Sub

    Private Sub txtMonthFee1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtMonthFee1.KeyPress
      Dim KeyAscii As Short = Asc(e.KeyChar)
      If KeyAscii = 13 Then
            e.Handled = True
            SendKeys.Send("{TAB}")
      Else
            ' ตรวจสอบการ Return ค่ากลับว่า True หรือ False
            e.Handled = CheckCurrency(KeyAscii, txtMonthFee1.Text)
      End If
    End Sub

    Private Sub txtMonthFee2_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtMonthFee2.KeyPress
      Dim KeyAscii As Short = Asc(e.KeyChar)
      If KeyAscii = 13 Then
            e.Handled = True
            SendKeys.Send("{TAB}")
      Else
            ' ตรวจสอบการ Return ค่ากลับว่า True หรือ False
            e.Handled = CheckCurrency(KeyAscii, txtMonthFee2.Text)
      End If
    End Sub

    Private Sub txtUnit_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtUnit.KeyPress
      Dim KeyAscii As Short = Asc(e.KeyChar)
      If KeyAscii = 13 Then
            e.Handled = True
            Call btnProcess_Click(sender, e)
      Else
            ' ตรวจสอบการ Return ค่ากลับว่า True หรือ False
            e.Handled = CheckDigitOnly(KeyAscii)
      End If
    End Sub

    Private Sub txtFT_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtFT.KeyPress
      Dim KeyAscii As Short = Asc(e.KeyChar)
      If KeyAscii = 13 Then
            e.Handled = True
            Call btnProcess_Click(sender, e)
      Else
            ' ตรวจสอบการ Return ค่ากลับว่า True หรือ False
            e.Handled = CheckCurrency(KeyAscii, txtFT.Text)
      End If
    End Sub

    Private Sub btnReset_Click(sender As System.Object, e As System.EventArgs) Handles btnReset.Click
      Call InitData()
    End Sub

    Private Sub txtSummary_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles txtSummary.KeyDown
      e.SuppressKeyPress = True
    End Sub

    Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
      Me.Close()
    End Sub

    Private Sub ToolStripStatusLabel3_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripStatusLabel3.Click
      Process.Start("http://www.g2gnet.com/webboard")
    End Sub

    Private Sub ToolStripStatusLabel2_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripStatusLabel2.Click
      Process.Start("https://www.facebook.com/g2gnet")
    End Sub

#Region "FUNCTION"
    ' / --------------------------------------------------------------------------------
    ' / ฟังค์ชั่นในการป้อนเฉพาะค่าตัวเลขได้เท่านั้น
    Function CheckDigitOnly(ByVal index As Integer) As Boolean
      Select Case index
            Case 48 To 57 ' เลข 0 - 9
                CheckDigitOnly = False
            Case 8, 13 ' Backspace = 8, Enter = 13
                CheckDigitOnly = False
            Case Else
                CheckDigitOnly = True
      End Select
    End Function

    ' / --------------------------------------------------------------------------------
    ' / ฟังค์ชั่นในการป้อนเฉพาะค่าตัวเลขและทศนิยมได้ตัวเดียวเท่านั้น
    Function CheckCurrency(index As Integer, tmpStr As String) As Boolean
      CheckCurrency = False
      Select Case index
            Case 48 To 57 ' เลข 0 - 9
                ' Allowed "."
            Case 46
                ' can present "." only one
                If InStr(tmpStr, ".") Then CheckCurrency = True

            Case 8, 13 ' Backspace = 8, Enter = 13
            Case Else
                CheckCurrency = True
      End Select
    End Function
#End Region

End Class

มาดูโค้ดต้นฉบับ (VB6) ...
Option Explicit

' อัตราค่าบริการ (กินเปล่า) ... ประเภท 11 บ้านที่อยู่อาศัย และเป็นประเภท 1.2
' คือใช้พลังงานไฟฟ้าเกิน 150 หน่วยต่อเดือน
Const FeePaid As Double = 38.22 '/ ยังไม่ได้รวม Vat
' คือ 40.9 บาท เป็นค่าบริการถอดมิเตอร์ (ล่วงหน้า) ... ส่วน 107 บาท เป็นบริการใส่กลับคืน (ตามหลัง) ... 55555+ คิดได้ไงเนี่ย ....

' อัตราค่าไฟฟ้าที่ใช้งานเกิน 150 หน่วยขึ้นไป โดยคิดแบบขั้นบันได
Const Unit150 As Double = 3.2484         '/ 150 หน่วยแรก
Const Unit250 As Double = 4.2218    '/ 250 - 400
Const Unit400 As Double = 4.4217         '/ เกิน 400 หน่วย

Private Sub cmdCalElectric_Click()
    Dim FirstUnit As Double                     ' ไม่เกิน 150 หน่วย (150 หน่วยแรก)
    Dim SecondUnit As Double            ' มากกว่า 150 หน่วย แต่ไม่เกิน 400 หน่วย (หรือ 400 - 150 = 250 หน่วย)
    Dim ThirdUnit As Double                   ' มากกว่า 400 หน่วยขึ้นไป
    Dim SumElectricCost As Double   ' (1) ผลรวมของค่าไฟฟ้ามาตรฐาน
    Dim SumFT As Double                     ' (2). ค่า FT คูณจำนวนหน่วย
    Dim Vat As Double                               ' (3). เอา 1 + 2 แล้วหาค่าภาษี 7%
    Dim TotalAmount As Double            ' (4)เอา 1 + 2 + 3 คืออัตราค่าไฟฟ้าทั้งหมด
   
    If txtUnit.Text = 0 Or IsNull(txtUnit.Text) Then Exit Sub
   
    If txtFT.Text = "" Or IsNull(txtFT.Text) Then txtFT.Text = "0"
    txtSummary.Text = ""
   
    ' คิดแบบขั้นบันไดกรณีใช้พลังงานไฟฟ้าไม่เกิน 150 หน่วย/เดือน
    Dim intStep As Double
    Select Case Val(txtUnit.Text)
      Case 1 To 15: intStep = 2.3488
      Case 16 To 25: intStep = 2.9882
      Case 26 To 35: intStep = 3.2405
      Case 36 To 100: intStep = 3.6237
      Case 101 To 150: intStep = 3.7171
      Case 151 To 400: intStep = 4.2218
      Case Else: intStep = 4.4217
    End Select
   
    ' กรณีแรกใช้ไฟฟ้าระหว่าง 0 - 150 หน่วย
    If Val(txtUnit.Text) <= 150 Then
      FirstUnit = Val(txtUnit.Text) * intStep
      ' บวกค่าบริการด้วย (สงสัยว่ามันบริการอะไร ...)
      SumElectricCost = FirstUnit + 8.19 'FeePaid
      SumFT = Val(txtUnit.Text) * Val(txtFT.Text)
      Vat = ((SumElectricCost + SumFT) * 7) / 100
      TotalAmount = SumElectricCost + SumFT + Vat
      txtSummary.Text = _
            "==================" & vbCrLf & _
            "รวมจำนวนเงิน =    " & Format(SumElectricCost, "#,##0.00") & vbCrLf & _
            "ค่า FT =               " & Format(SumFT, "#,##0.00") & vbCrLf & _
            "==================" & vbCrLf & _
            "รวมเงินค่าไฟฟ้า =" & Format(SumElectricCost + SumFT, "#,##0.00") & vbCrLf & _
            "ภาษีมูลค่าเพิ่ม =   " & Format(Vat, "#,##0.00") & vbCrLf & vbCrLf & _
            "รวมเงินที่ต้องชำระ: " & Format(TotalAmount, "#,##0.00") & " บาท."
   
   
    '// -------------------------------------------------------------------------------
    '//ใช้ไฟฟ้าระหว่าง 151 - 400 หน่วย (หรือ 400 - 150 = 250 หน่วย)
    ElseIf (Val(txtUnit.Text) > 150) And (Val(txtUnit.Text) <= 400) Then
      FirstUnit = 150 * Unit150
      SecondUnit = 250 * Unit250
      SumElectricCost = Format(FirstUnit + SecondUnit, "#,##0.00")
      SumFT = Val(txtUnit.Text) * Val(txtFT.Text)
      Vat = ((SumElectricCost + SumFT) * 7) / 100
      TotalAmount = SumElectricCost + SumFT + Vat
      '//
      txtSummary.Text = _
            "150 หน่วยแรก =" & Format(FirstUnit, "#,##0.00") & vbCrLf & _
            "250 หน่วยต่อไป = " & Format(SecondUnit, "#,##0.00") & vbCrLf & _
            "==================" & vbCrLf & _
            "รวมจำนวนเงิน =    " & Format(SumElectricCost, "#,##0.00") & vbCrLf & _
            "ค่า FT =               " & Format(SumFT, "#,##0.00") & vbCrLf & _
            "==================" & vbCrLf & _
            "รวมเงินค่าไฟฟ้า =" & Format(SumElectricCost + SumFT, "#,##0.00") & vbCrLf & _
            "ภาษีมูลค่าเพิ่ม =   " & Format(Vat, "#,##0.00") & vbCrLf & vbCrLf & _
            "รวมเงินที่ต้องชำระ: " & Format(TotalAmount, "#,##0.00") & " บาท."
   
    ' ใช้ไฟฟ้าตั้งแต่ 401 หน่วยขึ้นไป
    ElseIf (Val(txtUnit.Text) > 400) Then
      FirstUnit = Format(150 * Unit150, "#,##0.0000")
      SecondUnit = Format(250 * Unit250, "#,##0.0000")
      ThirdUnit = Format((Val(txtUnit.Text) - 400) * Unit400, "#,##0.0000")
      'SumElectricCost = FirstUnit + SecondUnit + ThirdUnit + 38.22 'FeePaid
      SumElectricCost = Format(FirstUnit + SecondUnit + ThirdUnit, "#,##0.0000")         'FeePaid
      SumFT = Format(Val(txtUnit.Text) * Val(txtFT.Text), "#,##0.0000")
      Vat = Format(((SumElectricCost + SumFT + 38.22) * 7) / 100, "#,##0.0000")
      TotalAmount = Format(SumElectricCost + SumFT + Vat + 38.22, "#,##0.0000")
      '//
      txtSummary.Text = _
            "150 หน่วยแรก = " & Format(FirstUnit, "#,##0.00") & vbCrLf & _
            "250 หน่วยต่อไป = " & Format(SecondUnit, "#,##0.00") & vbCrLf & _
            (Val(txtUnit.Text) - 400) & " หน่วยที่เหลือ = " & Format(ThirdUnit, "#,##0.00") & vbCrLf & _
            "==================" & vbCrLf & _
            "รวมจำนวนเงิน = " & Format(SumElectricCost, "#,##0.00") & vbCrLf & _
            "ค่าบริการ =            " & Format(38.22, "#,##0.00") & vbCrLf & _
            "ค่า FT =            " & Format(SumFT, "#,##0.00") & vbCrLf & _
            "==================" & vbCrLf & _
            "รวมเงินค่าไฟฟ้า =" & Format(SumElectricCost + SumFT + 38.22, "#,##0.00") & vbCrLf & _
            "ภาษีมูลค่าเพิ่ม =       " & Format(Vat, "#,##0.00") & vbCrLf & vbCrLf & _
            "รวมเงินที่ต้องชำระ: " & Format(TotalAmount, "#,##0.00") & " บาท."
    End If
   
    ' =========== แถม ===========
    ' กรณีของการใช้ Select Case ...
    'Select Case Val(txtUnit.Text)
    '    Case 0 To 150
    '      MsgBox "0 - 150 (หรือ 150 หน่วยแรก)"
    '    Case 151 To 400
    '      MsgBox "151 - 400 (หรือ 250 หน่วย)"
    '    Case Else
    '      MsgBox "401หน่วย ขึ้นไป"
    'End Select
    ' ==========================
   
    Call HLText(txtUnit)
End Sub

' จะใช้ตัวนี้ได้ก็ต่อเมื่อตั้งค่าคุณสมบัติของฟอร์ม KeyPreview = True ก่อนด้วยครับ
' หมายความว่าเราสามารถใช้ปุ่มฟังค์ชั่นต่างๆของฟอร์มได้ ...
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    Select Case KeyCode
      ' กดปุ่มฟังค์ชั่น F8
      Case vbKeyF8
            Call cmdCalElectric_Click
            
      ' กดปุ่มฟังค์ชั่น F10
      Case vbKeyF10
            End
    End Select
End Sub

Private Sub Form_Load()
   
    Me.Move (Screen.Width - Width) \ 2, (Screen.Height - Height) \ 2
    ' ตั้งค่าตัวเลขทั้งสมมุติและใช้งานจริง
    txtUnit.Text = "636"
    txtFT.Text = "-0.1160"
    txtSummary.Text = ""
   
End Sub

Private Sub txtFT_GotFocus()
    ' ฟังค์ชั่นนี้อยู่ใน modFunction เพื่อเลือกข้อมูลใน Text Box ทั้งหมด
    Call HLText(txtFT)
End Sub

Private Sub cmdExit_Click()
    Set frmCalElectrical = Nothing
    End
End Sub

Private Sub txtFT_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyDown Then Sendkeys "{TAB}"
    If KeyCode = vbKeyUp Then Sendkeys "+{TAB}"
End Sub

Private Sub txtFT_KeyPress(KeyAscii As Integer)
    If KeyAscii = vbKeyReturn Then
      KeyAscii = 0
      Call cmdCalElectric_Click
    Else
      '/ ฟังค์ชั่นนี้อยู่ใน modFunction เพื่อตรวจสอบการกดคีย์ 0 - 9 และ จุดทศนิยม
      KeyAscii = CheckCurrency(KeyAscii, txtFT)
    End If
End Sub

Private Sub txtUnit_KeyPress(KeyAscii As Integer)
    ' กดปุ่ม Enter ส่งรหัส ASCII =13
    If KeyAscii = vbKeyReturn Then
      KeyAscii = 0
      Call cmdCalElectric_Click
    Else
      ' ฟังค์ชั่นนี้อยู่ใน modFunction เพื่อตรวจสอบการกดคีย์ 0 - 9 เท่านั้น
      KeyAscii = CheckDigitOnly(KeyAscii)
    End If
End Sub

Private Sub txtUnit_GotFocus()
    Call HLText(txtUnit)
End Sub

Private Sub txtUnit_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyDown Then Sendkeys "{TAB}"
    If KeyCode = vbKeyUp Then Sendkeys "+{TAB}"
End Sub

Private Sub txtPowerCost_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyDown Then Sendkeys "{TAB}"
    If KeyCode = vbKeyUp Then Sendkeys "+{TAB}"
End Sub

Private Sub txtPowerCost_KeyPress(KeyAscii As Integer)
    If KeyAscii = vbKeyReturn Then
      KeyAscii = 0
      Sendkeys "{TAB}"
    End If
End Sub

Private Sub cmdCalElectric_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyDown Then Sendkeys "{TAB}"
    If KeyCode = vbKeyUp Then Sendkeys "+{TAB}"
End Sub

ส่วนโมดูลใน VB6 จะประกอบด้วยฟังค์ชั่นในการกดคีย์ ...
Option Explicit

' ทำไฮไลท์สำหรับ Text Box
Public Sub HLText(ByRef sText)
    On Error Resume Next
    With sText
      .SelStart = 0
      .SelLength = Len(sText.Text)
    End With
End Sub

' ตรวจสอบค่าใน Text Box ให้รับเฉพาะตัวเลขเท่านั้น
Function CheckDigitOnly(Index As Integer) As Integer
    Select Case Index
      Case 48 To 57
            CheckDigitOnly = Index
      Case 8
            ' Back Space
      Case 13
            ' Enter
      
      Case Else
            Index = 0
    End Select
    CheckDigitOnly = Index
End Function

' ตรวจสอบค่าใน Text Box ให้รับเฉพาะตัวเลข และ จุดทศนิยมเท่านั้น
Function CheckCurrency(Index As Integer, Ctrl As TextBox) As Integer
    Select Case Index
      Case 48 To 57
            ' 0 - 9 and Return index = KeyAscii
      Case 8
            ' Back Space and Return index = KeyAscii
      Case 13
            ' Enter and Return index = KeyAscii
      Case 46 ' รหัส Ascii Codeของเครื่องหมายจุดครับพี่น้อง
            If InStr(Ctrl, ".") Then Index = 0 ' ใช้ฟังค์ชั่น InStr (In String) เพื่อหาเครื่องหมายจุดใน TextBox
      Case Else
            Index = 0
    End Select
    CheckCurrency = Index ' Return ค่ากลับตามที่ได้ตรวจสอบ
End Function

'/แก้ปัญหาฟังค์ชั่น SendKeys ใน Windows 8 64 บิต
Public Sub Sendkeys(Text As String, Optional Wait As Boolean = False)
    Dim WshShell As Object
    Set WshShell = CreateObject("Wscript.shell")
    WshShell.Sendkeys Text, Wait
    Set WshShell = Nothing
End Sub
ดาวน์โหลดโค้ดต้นฉบับ VB6 และ VB.NET (2010) ได้ที่นี่ ...
หน้า: [1]
ดูในรูปแบบกติ: [VB6/VB.NET] แจกโค้ดการคำนวณหาอัตราค่าใช้ไฟฟ้าในบ้านเรือนที่อยู่อาศัย แบบขั้นบันได