ชุมชนคนรักภาษาเบสิค - Visual Basic Community

 ลืมรหัสผ่าน
 ลงทะเบียน
ค้นหา
ดู: 3667|ตอบกลับ: 0

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

[คัดลอกลิงก์]

308

กระทู้

499

โพสต์

6021

เครดิต

ผู้ดูแลระบบ

ทองก้อน ทับทิมกรอบ

Rank: 9Rank: 9Rank: 9

เครดิต
6021



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



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

สมมุติว่าใช้ไฟจำนวน 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) ...
  1. Public Class frmCalElectrical

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

  6.     Private Sub frmCalElectrical_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
  7.         Select Case e.KeyCode
  8.             Case Keys.F8
  9.                 Call btnProcess_Click(sender, e)
  10.             Case Keys.F10
  11.                 Me.Close()
  12.         End Select
  13.     End Sub

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

  20.     Private Sub SetupGridView(ByRef DGV As DataGridView)
  21.         '// Initialize DataGridView Control
  22.         With DGV
  23.             .RowHeadersVisible = False
  24.             .AllowUserToAddRows = False
  25.             .AllowUserToDeleteRows = False
  26.             .AllowUserToResizeRows = False
  27.             .MultiSelect = False
  28.             .SelectionMode = DataGridViewSelectionMode.CellSelect
  29.             .Font = New Font("Tahoma", 9)
  30.             ' Autosize Column
  31.             .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
  32.             .AutoResizeColumns()
  33.             '// Even-Odd Color
  34.             .AlternatingRowsDefaultCellStyle.BackColor = Color.AliceBlue
  35.             ' Adjust Header Styles
  36.             With .ColumnHeadersDefaultCellStyle
  37.                 .BackColor = Color.Navy
  38.                 .ForeColor = Color.Black ' Color.White
  39.                 .Font = New Font("Tahoma", 9, FontStyle.Bold)
  40.             End With
  41.         End With
  42.     End Sub

  43.     ' / Initialized DataGridView and put the sample data.
  44.     Private Sub InitData()
  45.         txtSummary.Text = ""
  46.         txtMonthFee1.Text = "8.19"
  47.         txtMonthFee2.Text = "38.22"
  48.         txtFT.Text = "-0.1160"
  49.         txtUnit.Text = "0"
  50.         '//
  51.         Me.DataGridView1.Columns.Clear()
  52.         Me.DataGridView2.Columns.Clear()
  53.         '// Declare columns type.
  54.         Dim Column1 As New DataGridViewTextBoxColumn()
  55.         Dim Column2 As New DataGridViewTextBoxColumn()
  56.         '// Add new Columns
  57.         DataGridView1.Columns.AddRange(New DataGridViewColumn() { _
  58.                 Column1, Column2 _
  59.                 })
  60.         With DataGridView1
  61.             .Columns(0).Name = "UnitName"
  62.             .Columns(0).HeaderText = "จำนวนหน่วย"
  63.             .Columns(0).ReadOnly = True
  64.             .Columns(1).Name = "UnitPrice"
  65.             .Columns(1).HeaderText = "ค่าพลังงานไฟฟ้า (บาท/หน่วย)"
  66.             .Columns(1).ReadOnly = False
  67.         End With
  68.         '// DataGridView1
  69.         Dim row = New String() {"15 หน่วยแรก (หน่วยที่ 0 – 15)", "2.3488"}
  70.         DataGridView1.Rows.Add(row)
  71.         row = {"10 หน่วยต่อไป (หน่วยที่ 16 – 25)", "2.9882"}
  72.         DataGridView1.Rows.Add(row)
  73.         row = {"10 หน่วยต่อไป (หน่วยที่ 26 – 35)", "3.2405"}
  74.         DataGridView1.Rows.Add(row)
  75.         row = {"65 หน่วยต่อไป (หน่วยที่ 36 – 100)", "3.6237"}
  76.         DataGridView1.Rows.Add(row)
  77.         row = {"50 หน่วยต่อไป (หน่วยที่ 101 – 150)", "3.7171"}
  78.         DataGridView1.Rows.Add(row)
  79.         row = {"250 หน่วยต่อไป (หน่วยที่ 151 – 400)", "4.2218"}
  80.         DataGridView1.Rows.Add(row)
  81.         row = {"เกิน 400 หน่วยขึ้นไป (หน่วยที่ 401 เป็นต้นไป)", "4.4217"}
  82.         DataGridView1.Rows.Add(row)
  83.         With Me.DataGridView1
  84.             .Columns(1).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
  85.             .Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
  86.         End With

  87.         '// DataGridView2
  88.         '// Declare columns type.
  89.         Dim Col1 As New DataGridViewTextBoxColumn()
  90.         Dim Col2 As New DataGridViewTextBoxColumn()
  91.         '// Add new Columns
  92.         DataGridView2.Columns.AddRange(New DataGridViewColumn() { _
  93.                 Col1, Col2 _
  94.                 })
  95.         With DataGridView2
  96.             .Columns(0).Name = "UnitName"
  97.             .Columns(0).HeaderText = "จำนวนหน่วย"
  98.             .Columns(0).ReadOnly = True
  99.             .Columns(1).Name = "UnitPrice"
  100.             .Columns(1).HeaderText = "ค่าพลังงานไฟฟ้า (บาท/หน่วย)"
  101.             .Columns(1).ReadOnly = False
  102.         End With
  103.         row = {"150 หน่วยแรก         (หน่วยที่ 0 – 150)", "3.2484"}
  104.         DataGridView2.Rows.Add(row)
  105.         row = {"250 หน่วยต่อไป (หน่วยที่ 151 – 400)", " 4.2218"}
  106.         DataGridView2.Rows.Add(row)
  107.         row = {"เกิน 400 หน่วยขึ้นไป (หน่วยที่ 401 เป็นต้นไป)", "4.4217"}
  108.         DataGridView2.Rows.Add(row)
  109.         With Me.DataGridView2
  110.             .Columns(1).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
  111.             .Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
  112.         End With
  113.     End Sub

  114.     Private Sub btnProcess_Click(sender As System.Object, e As System.EventArgs) Handles btnProcess.Click
  115.         '// Trap Error
  116.         If txtFT.Text = "" Then txtFT.Text = "0.00"
  117.         If txtUnit.Text = "" Then txtUnit.Text = "0"
  118.         If txtMonthFee1.Text = "" Then txtMonthFee1.Text = "0.00"
  119.         If txtMonthFee2.Text = "" Then txtMonthFee2.Text = "0.00"
  120.         '// เช็คค่าว่างในเซลล์
  121.         For i = 0 To Me.DataGridView1.RowCount - 1
  122.             If IsNothing(Me.DataGridView1.Rows(i).Cells(1).Value) Then Me.DataGridView1.Rows(i).Cells(1).Value = 0.0
  123.         Next
  124.         For i = 0 To Me.DataGridView2.RowCount - 1
  125.             If IsNothing(Me.DataGridView2.Rows(i).Cells(1).Value) Then Me.DataGridView2.Rows(i).Cells(1).Value = 0.0
  126.         Next
  127.         '// CALCULATE
  128.         Call CalElectrical()
  129.     End Sub

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

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

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

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

  187.             ' / ---------------------------------------------------------------
  188.             ' ใช้ไฟฟ้าตั้งแต่ 401 หน่วยขึ้นไป
  189.         ElseIf (Val(txtUnit.Text) > 400) Then
  190.             FirstUnit = Format(150 * Me.DataGridView2.Rows(0).Cells(1).Value, "#,##0.0000")
  191.             SecondUnit = Format(250 * Me.DataGridView2.Rows(1).Cells(1).Value, "#,##0.0000")
  192.             ThirdUnit = Format((Val(txtUnit.Text) - 400) * Me.DataGridView2.Rows(2).Cells(1).Value, "#,##0.0000")
  193.             'SumElectricCost = FirstUnit + SecondUnit + ThirdUnit + 38.22 'FeePaid
  194.             SumElectricCost = Format(FirstUnit + SecondUnit + ThirdUnit, "#,##0.0000")         'FeePaid
  195.             SumFT = Format(Val(txtUnit.Text) * Val(txtFT.Text), "#,##0.0000")
  196.             Vat = Format(((SumElectricCost + SumFT + Val(txtMonthFee2.Text)) * 7) / 100, "#,##0.0000")
  197.             TotalAmount = Format(SumElectricCost + SumFT + Vat + Val(txtMonthFee2.Text), "#,##0.0000")
  198.             '//
  199.             txtSummary.Text = _
  200.                 "150 หน่วยแรก" & vbTab & " = " & Format(FirstUnit, "#,##0.00") & vbCrLf & _
  201.                 "250 หน่วยต่อไป" & vbTab & " = " & Format(SecondUnit, "#,##0.00") & vbCrLf & _
  202.                 (Val(txtUnit.Text) - 400) & " หน่วยที่เหลือ" & vbTab & " = " & Format(ThirdUnit, "#,##0.00") & vbCrLf & _
  203.                 "==================" & vbCrLf & _
  204.                 "รวมจำนวนเงิน" & vbTab & " = " & Format(SumElectricCost, "#,##0.00") & vbCrLf & _
  205.                 "ค่าบริการ" & vbTab & vbTab & " = " & Format(Val(txtMonthFee2.Text), "#,##0.00") & vbCrLf & _
  206.                 "ค่า FT" & vbTab & vbTab & " = " & Format(SumFT, "#,##0.00") & vbCrLf & _
  207.                 "==================" & vbCrLf & _
  208.                 "รวมเงินค่าไฟฟ้า" & vbTab & " = " & Format(SumElectricCost + SumFT + Val(txtMonthFee2.Text), "#,##0.00") & vbCrLf & _
  209.                 "ภาษีมูลค่าเพิ่ม" & vbTab & " = " & Format(Vat, "#,##0.00") & vbCrLf & vbCrLf & _
  210.                 "รวมเงินที่ต้องชำระ: " & vbTab & " = " & Format(TotalAmount, "#,##0.00") & " บาท."
  211.         End If
  212.     End Sub

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

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

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

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

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

  261.                     Case ChrW(Keys.Back)
  262.                     Case Else
  263.                         e.Handled = True
  264.                 End Select
  265.         End Select
  266.     End Sub

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

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

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

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

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

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

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

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

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

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

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

  345.             Case 8, 13 ' Backspace = 8, Enter = 13
  346.             Case Else
  347.                 CheckCurrency = True
  348.         End Select
  349.     End Function
  350. #End Region

  351. End Class
คัดลอกไปที่คลิปบอร์ด


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

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

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

  10. Private Sub cmdCalElectric_Click()
  11.     Dim FirstUnit As Double                     ' ไม่เกิน 150 หน่วย (150 หน่วยแรก)
  12.     Dim SecondUnit As Double              ' มากกว่า 150 หน่วย แต่ไม่เกิน 400 หน่วย (หรือ 400 - 150 = 250 หน่วย)
  13.     Dim ThirdUnit As Double                   ' มากกว่า 400 หน่วยขึ้นไป
  14.     Dim SumElectricCost As Double     ' (1) ผลรวมของค่าไฟฟ้ามาตรฐาน
  15.     Dim SumFT As Double                       ' (2). ค่า FT คูณจำนวนหน่วย
  16.     Dim Vat As Double                               ' (3). เอา 1 + 2 แล้วหาค่าภาษี 7%
  17.     Dim TotalAmount As Double            ' (4)  เอา 1 + 2 + 3 คืออัตราค่าไฟฟ้าทั้งหมด
  18.    
  19.     If txtUnit.Text = 0 Or IsNull(txtUnit.Text) Then Exit Sub
  20.    
  21.     If txtFT.Text = "" Or IsNull(txtFT.Text) Then txtFT.Text = "0"
  22.     txtSummary.Text = ""
  23.    
  24.     ' คิดแบบขั้นบันไดกรณีใช้พลังงานไฟฟ้าไม่เกิน 150 หน่วย/เดือน
  25.     Dim intStep As Double
  26.     Select Case Val(txtUnit.Text)
  27.         Case 1 To 15: intStep = 2.3488
  28.         Case 16 To 25: intStep = 2.9882
  29.         Case 26 To 35: intStep = 3.2405
  30.         Case 36 To 100: intStep = 3.6237
  31.         Case 101 To 150: intStep = 3.7171
  32.         Case 151 To 400: intStep = 4.2218
  33.         Case Else: intStep = 4.4217
  34.     End Select
  35.    
  36.     ' กรณีแรกใช้ไฟฟ้าระหว่าง 0 - 150 หน่วย
  37.     If Val(txtUnit.Text) <= 150 Then
  38.         FirstUnit = Val(txtUnit.Text) * intStep
  39.         ' บวกค่าบริการด้วย (สงสัยว่ามันบริการอะไร ...)
  40.         SumElectricCost = FirstUnit + 8.19 'FeePaid
  41.         SumFT = Val(txtUnit.Text) * Val(txtFT.Text)
  42.         Vat = ((SumElectricCost + SumFT) * 7) / 100
  43.         TotalAmount = SumElectricCost + SumFT + Vat
  44.         txtSummary.Text = _
  45.             "==================" & vbCrLf & _
  46.             "รวมจำนวนเงิน =    " & Format(SumElectricCost, "#,##0.00") & vbCrLf & _
  47.             "ค่า FT =               " & Format(SumFT, "#,##0.00") & vbCrLf & _
  48.             "==================" & vbCrLf & _
  49.             "รวมเงินค่าไฟฟ้า =  " & Format(SumElectricCost + SumFT, "#,##0.00") & vbCrLf & _
  50.             "ภาษีมูลค่าเพิ่ม =     " & Format(Vat, "#,##0.00") & vbCrLf & vbCrLf & _
  51.             "รวมเงินที่ต้องชำระ: " & Format(TotalAmount, "#,##0.00") & " บาท."
  52.    
  53.    
  54.     '// -------------------------------------------------------------------------------
  55.     '//  ใช้ไฟฟ้าระหว่าง 151 - 400 หน่วย (หรือ 400 - 150 = 250 หน่วย)
  56.     ElseIf (Val(txtUnit.Text) > 150) And (Val(txtUnit.Text) <= 400) Then
  57.         FirstUnit = 150 * Unit150
  58.         SecondUnit = 250 * Unit250
  59.         SumElectricCost = Format(FirstUnit + SecondUnit, "#,##0.00")
  60.         SumFT = Val(txtUnit.Text) * Val(txtFT.Text)
  61.         Vat = ((SumElectricCost + SumFT) * 7) / 100
  62.         TotalAmount = SumElectricCost + SumFT + Vat
  63.         '//
  64.         txtSummary.Text = _
  65.             "150 หน่วยแรก =  " & Format(FirstUnit, "#,##0.00") & vbCrLf & _
  66.             "250 หน่วยต่อไป = " & Format(SecondUnit, "#,##0.00") & vbCrLf & _
  67.             "==================" & vbCrLf & _
  68.             "รวมจำนวนเงิน =    " & Format(SumElectricCost, "#,##0.00") & vbCrLf & _
  69.             "ค่า FT =               " & Format(SumFT, "#,##0.00") & vbCrLf & _
  70.             "==================" & vbCrLf & _
  71.             "รวมเงินค่าไฟฟ้า =  " & Format(SumElectricCost + SumFT, "#,##0.00") & vbCrLf & _
  72.             "ภาษีมูลค่าเพิ่ม =     " & Format(Vat, "#,##0.00") & vbCrLf & vbCrLf & _
  73.             "รวมเงินที่ต้องชำระ: " & Format(TotalAmount, "#,##0.00") & " บาท."
  74.    
  75.     ' ใช้ไฟฟ้าตั้งแต่ 401 หน่วยขึ้นไป
  76.     ElseIf (Val(txtUnit.Text) > 400) Then
  77.         FirstUnit = Format(150 * Unit150, "#,##0.0000")
  78.         SecondUnit = Format(250 * Unit250, "#,##0.0000")
  79.         ThirdUnit = Format((Val(txtUnit.Text) - 400) * Unit400, "#,##0.0000")
  80.         'SumElectricCost = FirstUnit + SecondUnit + ThirdUnit + 38.22 'FeePaid
  81.         SumElectricCost = Format(FirstUnit + SecondUnit + ThirdUnit, "#,##0.0000")         'FeePaid
  82.         SumFT = Format(Val(txtUnit.Text) * Val(txtFT.Text), "#,##0.0000")
  83.         Vat = Format(((SumElectricCost + SumFT + 38.22) * 7) / 100, "#,##0.0000")
  84.         TotalAmount = Format(SumElectricCost + SumFT + Vat + 38.22, "#,##0.0000")
  85.         '//
  86.         txtSummary.Text = _
  87.             "150 หน่วยแรก = " & Format(FirstUnit, "#,##0.00") & vbCrLf & _
  88.             "250 หน่วยต่อไป = " & Format(SecondUnit, "#,##0.00") & vbCrLf & _
  89.             (Val(txtUnit.Text) - 400) & " หน่วยที่เหลือ = " & Format(ThirdUnit, "#,##0.00") & vbCrLf & _
  90.             "==================" & vbCrLf & _
  91.             "รวมจำนวนเงิน = " & Format(SumElectricCost, "#,##0.00") & vbCrLf & _
  92.             "ค่าบริการ =            " & Format(38.22, "#,##0.00") & vbCrLf & _
  93.             "ค่า FT =              " & Format(SumFT, "#,##0.00") & vbCrLf & _
  94.             "==================" & vbCrLf & _
  95.             "รวมเงินค่าไฟฟ้า =  " & Format(SumElectricCost + SumFT + 38.22, "#,##0.00") & vbCrLf & _
  96.             "ภาษีมูลค่าเพิ่ม =       " & Format(Vat, "#,##0.00") & vbCrLf & vbCrLf & _
  97.             "รวมเงินที่ต้องชำระ: " & Format(TotalAmount, "#,##0.00") & " บาท."
  98.     End If
  99.    
  100.     ' =========== แถม ===========
  101.     ' กรณีของการใช้ Select Case ...
  102.     'Select Case Val(txtUnit.Text)
  103.     '    Case 0 To 150
  104.     '        MsgBox "0 - 150 (หรือ 150 หน่วยแรก)"
  105.     '    Case 151 To 400
  106.     '        MsgBox "151 - 400 (หรือ 250 หน่วย)"
  107.     '    Case Else
  108.     '        MsgBox "401หน่วย ขึ้นไป"
  109.     'End Select
  110.     ' ==========================
  111.    
  112.     Call HLText(txtUnit)
  113. End Sub

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

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

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

  140. Private Sub cmdExit_Click()
  141.     Set frmCalElectrical = Nothing
  142.     End
  143. End Sub

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

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

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

  167. Private Sub txtUnit_GotFocus()
  168.     Call HLText(txtUnit)
  169. End Sub

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

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

  178. Private Sub txtPowerCost_KeyPress(KeyAscii As Integer)
  179.     If KeyAscii = vbKeyReturn Then
  180.         KeyAscii = 0
  181.         Sendkeys "{TAB}"
  182.     End If
  183. End Sub

  184. Private Sub cmdCalElectric_KeyDown(KeyCode As Integer, Shift As Integer)
  185.     If KeyCode = vbKeyDown Then Sendkeys "{TAB}"
  186.     If KeyCode = vbKeyUp Then Sendkeys "+{TAB}"
  187. End Sub
คัดลอกไปที่คลิปบอร์ด


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

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

  10. ' ตรวจสอบค่าใน Text Box ให้รับเฉพาะตัวเลขเท่านั้น
  11. Function CheckDigitOnly(Index As Integer) As Integer
  12.     Select Case Index
  13.         Case 48 To 57
  14.             CheckDigitOnly = Index
  15.         Case 8
  16.             ' Back Space
  17.         Case 13
  18.             ' Enter
  19.         
  20.         Case Else
  21.             Index = 0
  22.     End Select
  23.     CheckDigitOnly = Index
  24. End Function

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

  41. '/  แก้ปัญหาฟังค์ชั่น SendKeys ใน Windows 8 64 บิต
  42. Public Sub Sendkeys(Text As String, Optional Wait As Boolean = False)
  43.     Dim WshShell As Object
  44.     Set WshShell = CreateObject("Wscript.shell")
  45.     WshShell.Sendkeys Text, Wait
  46.     Set WshShell = Nothing
  47. End Sub
คัดลอกไปที่คลิปบอร์ด

ดาวน์โหลดโค้ดต้นฉบับ VB6 และ VB.NET (2010) ได้ที่นี่ ...

ขออภัย! โพสต์นี้มีไฟล์แนบหรือรูปภาพที่ไม่ได้รับอนุญาตให้คุณเข้าถึง

คุณจำเป็นต้อง ลงชื่อเข้าใช้ เพื่อดาวน์โหลดหรือดูไฟล์แนบนี้ คุณยังไม่มีบัญชีใช่ไหม? ลงทะเบียน

x
สิ่งที่ดีกว่าการให้ คือการให้แบบไม่มีที่สิ้นสุด
ขออภัย! คุณไม่ได้รับสิทธิ์ในการดำเนินการในส่วนนี้ กรุณาเลือกอย่างใดอย่างหนึ่ง ลงชื่อเข้าใช้ | ลงทะเบียน

รายละเอียดเครดิต

ข้อความล้วน|อุปกรณ์พกพา|ประวัติการแบน|G2GNet.com  

GMT+7, 2024-4-16 21:27 , Processed in 0.115040 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

ตอบกระทู้ ขึ้นไปด้านบน ไปที่หน้ารายการกระทู้