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

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

[VB.NET] โค้ดการคำนวณหาอัตราค่าบริการขนส่งไปรษณีย์ (eCo Post)

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

311

กระทู้

502

โพสต์

6060

เครดิต

ผู้ดูแลระบบ

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

Rank: 9Rank: 9Rank: 9

เครดิต
6060

โค้ด VB.NET (2010) กับการคำนวณหาอัตราค่าบริการขนส่งไปรษณีย์ (eCo Post) ...






มาดูโค้ดฉบับเต็มกันเถอะ ...
  1. Public Class frmWeightCalculation

  2.     Private Sub frmWeightCalculation_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
  3.         Select Case e.KeyCode
  4.             Case Keys.F8
  5.                 Call DeleteRow("btnDelRow")
  6.             Case Keys.F7
  7.                 Call btnAddToDataGridView_Click(sender, e)
  8.                 txtWeight.Focus()
  9.         End Select
  10.     End Sub

  11.     Private Sub frmWeightCalculation_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  12.         Call InitializeGrid()
  13.         Call rdoEnvelope_Click(sender, e)
  14.         Call ClearTextBox()
  15.         txtWeight.Focus()
  16.         txtWeight.Tag = 0
  17.     End Sub

  18.     Private Sub ClearTextBox()
  19.         txtWeight.Text = "0"
  20.         txtPriceEnvelope.Text = "0.00"
  21.         txtPriceBox.Text = "0.00"
  22.         txtPriceEnvelopeBox.Text = "0.00"
  23.         txtWeightDescription.Text = String.Empty
  24.     End Sub

  25.     Private Sub txtWeight_GotFocus(sender As Object, e As System.EventArgs) Handles txtWeight.GotFocus
  26.         If Not IsDBNull(txtWeight.Text) Then
  27.             txtWeight.SelectionStart = 0
  28.             txtWeight.SelectionLength = txtWeight.Text.Length
  29.         End If
  30.     End Sub

  31.     Private Sub txtWeight_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtWeight.KeyPress
  32.         If e.KeyChar = Chr(13) Then
  33.             e.Handled = True
  34.             Call CalWeight()
  35.         Else
  36.             '// รับค่าเฉพาะตัวเลขแบบจำนวนเต็ม
  37.             'e.Handled = CheckDigitOnly(Asc(e.KeyChar))
  38.             e.Handled = ValidateNumeric(e.KeyChar)
  39.         End If
  40.     End Sub

  41.     ' / --------------------------------------------------------------------------------
  42.     ' / S A M P L E ... D A T A    T A B L E (Products)
  43.     ' / --------------------------------------------------------------------------------
  44.     Function GetDataTable() As DataTable
  45.         '// Add Column
  46.         Dim DT As New DataTable
  47.         With DT.Columns
  48.             .Add("WeightPK", GetType(Integer)) '<< Index = 0
  49.             .Add("WeightName", GetType(String)) '<< 1
  50.             .Add("BeginWeight", GetType(Integer)) '<< 2
  51.             .Add("EndWeight", GetType(Integer)) '<< 3
  52.             .Add("PriceEnvelope", GetType(Double)) '<< 4
  53.             .Add("PriceBox", GetType(Double)) '<< 5
  54.             .Add("PriceEnvelopeBox", GetType(Double)) '<< 6
  55.         End With
  56.         '/ Add sample data.
  57.         '/ WeightPK, WeightName, BeginWeight, EndWeight, PriceEnvelope, PriceBox, PriceEnvelopeBox
  58.         With DT.Rows
  59.             .Add(1, "ไม่เกิน 20 กรัม", "1", "20", "16.00", "18.00", "20.00")
  60.             .Add(2, "เกิน 20 กรัม - 100 กรัม", "21", "100", "18.00", "20.00", "22.00")
  61.             .Add(3, "เกิน 100 กรัม - 250 กรัม", "101", "250", "22.00", "24.00", "26.00")
  62.             .Add(4, "เกิน 250 กรัม - 500 กรัม", "251", "500", "28.00", "30.00", "30.00")
  63.             .Add(5, "เกิน 500 กรัม - 1,000 กรัม", "501", "1000", "38.00", "40.00", "40.00")
  64.             .Add(6, "เกิน 1,000 กรัม - 2,000 กรัม", "1001", "2000", "58.00", "60.00", "60.00")
  65.             .Add(7, "เกิน 2,000 กรัม - 4,000 กรัม", "2001", "4000", "0.00", "0.00", "80.00")
  66.             .Add(8, "เกิน 4,000 กรัม - 6,000 กรัม", "4001", "6000", "0.00", "0.00", "120.00")
  67.             .Add(9, "เกิน 6,000 กรัม - 10,000 กรัม", "6001", "10000", "0.00", "0.00", "160.00")
  68.         End With
  69.         Return DT
  70.     End Function

  71.     Private Sub CalWeight()
  72.         If txtWeight.Text.Trim = "" Or txtWeight.Text.Trim.Length = 0 Then
  73.             txtWeight.Text = "0"
  74.             Return
  75.         End If
  76.         Dim DT As DataTable = GetDataTable()
  77.         '/ ค้นหาข้อมูลจากน้ำหนักที่ต้องการ แล้วเปรียบเทียบช่วงค่าระหว่างน้ำหนักต่ำสุดและน้ำหนักสูงสุด
  78.         Dim r() As DataRow = DT.Select(CInt(txtWeight.Text.Trim) & " >= BeginWeight AND " & CInt(txtWeight.Text.Trim) & "<= EndWeight")
  79.         '// หากพบข้อมูลใน DataTable
  80.         If r.Count > 0 Then
  81.             txtWeight.Tag = r(0).Item(0).ToString
  82.             txtPriceEnvelope.Text = Format(CDbl(r(0).Item(4).ToString), "0.00")
  83.             txtPriceBox.Text = Format(CDbl(r(0).Item(5).ToString), "0.00")
  84.             txtPriceEnvelopeBox.Text = Format(CDbl(r(0).Item(6).ToString), "0.00")
  85.             txtWeightDescription.Text = r(0).Item(1).ToString
  86.             '// กรณีที่อัตราค่าบริการซองและกล่อง = 0.00 ให้ไปโฟกัสที่ประเภทซองและกล่อง
  87.             If Val(r(0).Item(4).ToString) = 0 AndAlso Val(r(0).Item(5).ToString) = 0 Then
  88.                 rdoEnvelopeBox.Focus()
  89.             Else
  90.                 rdoEnvelope.Focus()
  91.             End If
  92.         Else
  93.             Call ClearTextBox()
  94.         End If
  95.         DT.Dispose()
  96.         txtWeight.Focus()
  97.     End Sub

  98.     Private Sub btnCalWeight_Click(sender As System.Object, e As System.EventArgs) Handles btnCalWeight.Click
  99.         Call CalWeight()
  100.         txtWeight.Focus()
  101.     End Sub

  102.     ' / --------------------------------------------------------------------------------
  103.     ' / ตั้งค่าเริ่มต้นให้กับ DataGridView แบบ Run Time (ใช้โค้ดทั้งหมด)
  104.     Private Sub InitializeGrid()
  105.         With dgvData
  106.             .RowHeadersVisible = False
  107.             .AllowUserToAddRows = False
  108.             .AllowUserToDeleteRows = False
  109.             .AllowUserToResizeRows = False
  110.             .MultiSelect = False
  111.             .ReadOnly = True
  112.             .RowTemplate.MinimumHeight = 30
  113.             .RowTemplate.Height = 30
  114.             .SelectionMode = DataGridViewSelectionMode.FullRowSelect
  115.             '/ Columns Specified
  116.             '/ Index = 0
  117.             .Columns.Add("PK", "Primary Key")
  118.             With .Columns("PK")
  119.                 .DefaultCellStyle.BackColor = Color.LightGoldenrodYellow
  120.                 .Visible = False '/ ปกติหลัก Primary Key จะต้องถูกซ่อนไว้
  121.             End With
  122.             '/ Index = 1
  123.             .Columns.Add("Category", "Category")
  124.             .Columns("Category").DefaultCellStyle.BackColor = Color.LightGoldenrodYellow
  125.             '/ Index = 2
  126.             .Columns.Add("WeightDescription", "Weight Description")
  127.             .Columns("WeightDescription").DefaultCellStyle.BackColor = Color.LightGoldenrodYellow
  128.             '/ Index = 3
  129.             .Columns.Add("Weight", "Weight")
  130.             .Columns("Weight").ValueType = GetType(Integer)
  131.             '/ Index = 4
  132.             .Columns.Add("UnitPrice", "Unit Price")
  133.             .Columns("UnitPrice").ValueType = GetType(Double)
  134.         End With
  135.         '// เพิ่มปุ่มลบ (Index = 5)
  136.         Dim btnDelRow As New DataGridViewButtonColumn
  137.         dgvData.Columns.Add(btnDelRow)
  138.         With btnDelRow
  139.             .HeaderText = "Delete F8"
  140.             .Text = "Delete"
  141.             .UseColumnTextForButtonValue = True
  142.             .Width = 30
  143.             .ReadOnly = True
  144.             .HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
  145.             .SortMode = DataGridViewColumnSortMode.NotSortable  '/ Not sort order but can click header for delete row.
  146.         End With
  147.         '/ Alignment MiddleRight only columns 3 to 5
  148.         For i As Byte = 3 To 4
  149.             With dgvData
  150.                 '/ Header Alignment
  151.                 .Columns(i).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
  152.                 '/ Cell Alignment
  153.                 .Columns(i).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
  154.                 .DefaultCellStyle.BackColor = Color.LightGoldenrodYellow
  155.                 .DefaultCellStyle.ForeColor = Color.Blue
  156.                 .DefaultCellStyle.Font = New Font(dgvData.Font, FontStyle.Bold)
  157.             End With
  158.         Next
  159.         With dgvData
  160.             '// Even-Odd Color
  161.             .AlternatingRowsDefaultCellStyle.BackColor = Color.AliceBlue
  162.             '/ Auto size column width of each main by sorting the field.
  163.             .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
  164.             .AutoResizeColumns()
  165.             '/ Adjust Header Styles
  166.             With .ColumnHeadersDefaultCellStyle
  167.                 .BackColor = Color.Orange
  168.                 .ForeColor = Color.Black
  169.                 .Font = New Font("Tahoma", 11, FontStyle.Bold)
  170.             End With
  171.             .ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing
  172.             .ColumnHeadersHeight = 40
  173.             '/ กำหนดให้ EnableHeadersVisualStyles = False เพื่อให้ยอมรับการเปลี่ยนแปลงสีพื้นหลังของ Header
  174.             .EnableHeadersVisualStyles = False
  175.         End With
  176.     End Sub

  177.     Private Sub btnAddToDataGridView_Click(sender As System.Object, e As System.EventArgs) Handles btnAddToDataGridView.Click
  178.         If txtWeight.Tag = 0 Then
  179.             txtWeight.Focus()
  180.             Exit Sub
  181.         End If
  182.         If (CInt(txtPriceEnvelope.Text) = 0 And CInt(txtPriceBox.Text) = 0) AndAlso (rdoEnvelope.Checked Or rdoBox.Checked) Then
  183.             MessageBox.Show("ไม่สามารถเพิ่มรายการได้ เนื่องจากอยู่นอกเหนือจากพิกัดน้ำหนัก.", "รายงานสถานะ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  184.             Return
  185.         End If
  186.         Dim Category As String = String.Empty
  187.         Dim Price As Double = "0.00"
  188.         If rdoEnvelope.Checked Then
  189.             Price = txtPriceEnvelope.Text
  190.             Category = lblEnvelope.Text
  191.         ElseIf rdoBox.Checked Then
  192.             Price = txtPriceBox.Text
  193.             Category = lblBox.Text
  194.         Else
  195.             Price = txtPriceEnvelopeBox.Text
  196.             Category = lblEnvelopeBox.Text
  197.         End If
  198.         '/ Primary Key (Weight), Category (มาจาก Label), WeightDescription (มาจาก TextBox), Weight, Price
  199.         dgvData.Rows.Add(txtWeight.Tag, Category, txtWeightDescription.Text, Format(CInt(txtWeight.Text), "#,##0"), Format(Price, "0.00"))
  200.         txtWeight.Tag = 0
  201.         Call ClearTextBox()
  202.         txtWeight.Focus()
  203.     End Sub

  204.     ' / --------------------------------------------------------------------------------
  205.     ' / โปรแกรมย่อยในการลบแถวรายการที่เลือกออกไป
  206.     Private Sub DeleteRow(ByVal ColName As String)
  207.         If dgvData.RowCount = 0 Then Return
  208.         '/ ColName เป็นชื่อของหลัก Index = 6 ของตารางกริด (ไปดูที่โปรแกรมย่อย InitializeGrid)
  209.         If ColName = "btnDelRow" Then
  210.             '// ลบรายการแถวที่เลือกออกไป
  211.             dgvData.Rows.Remove(dgvData.CurrentRow)
  212.         End If
  213.         txtWeight.Focus()
  214.     End Sub

  215.     Private Sub rdoEnvelope_Click(sender As Object, e As System.EventArgs) Handles rdoEnvelope.Click
  216.         txtPriceEnvelope.BackColor = Color.Red
  217.         txtPriceBox.BackColor = Color.Yellow
  218.         txtPriceEnvelopeBox.BackColor = Color.Yellow
  219.         Call CalWeight()
  220.     End Sub

  221.     Private Sub rdoBox_Click(sender As Object, e As System.EventArgs) Handles rdoBox.Click
  222.         txtPriceEnvelope.BackColor = Color.Yellow
  223.         txtPriceBox.BackColor = Color.Red
  224.         txtPriceEnvelopeBox.BackColor = Color.Yellow
  225.         Call CalWeight()
  226.     End Sub

  227.     Private Sub rdoEnvelopeBox_Click(sender As Object, e As System.EventArgs) Handles rdoEnvelopeBox.Click
  228.         txtPriceEnvelope.BackColor = Color.Yellow
  229.         txtPriceBox.BackColor = Color.Yellow
  230.         txtPriceEnvelopeBox.BackColor = Color.Red
  231.         Call CalWeight()
  232.     End Sub

  233.     Private Sub txtWeight_LostFocus(sender As Object, e As System.EventArgs) Handles txtWeight.LostFocus
  234.         If txtWeight.Text.Trim = "" Or txtWeight.Text.Trim.Length = 0 Then txtWeight.Text = "0"
  235.     End Sub

  236.     Private Sub dgvData_CellClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvData.CellClick
  237.         Select Case e.ColumnIndex
  238.             '// Delete Button
  239.             Case 5
  240.                 'MsgBox(("Row : " + e.RowIndex.ToString & "  Col : ") + e.ColumnIndex.ToString)
  241.                 Call DeleteRow("btnDelRow")
  242.         End Select
  243.     End Sub

  244.     Private Sub frmWeightCalculation_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
  245.         Me.Dispose()
  246.         GC.SuppressFinalize(Me)
  247.         Application.Exit()
  248.     End Sub
  249. End Class
คัดลอกไปที่คลิปบอร์ด

โมดูล modFunction.vb ...
  1. Module modFunction

  2.     ' / --------------------------------------------------------------------------------
  3.     ' / Get my project path
  4.     ' / AppPath = C:\My Project\bin\debug
  5.     ' / Replace "\bin\debug" with ""
  6.     ' / Return : C:\My Project\
  7.     ' / --------------------------------------------------------------------------------
  8.     ' / ฟังค์ชั่นในการกำหนดพาธให้กับโปรแกรม
  9.     Function MyPath(ByVal AppPath As String) As String
  10.         MyPath = AppPath.ToLower.Replace("\bin\debug", "").Replace("\bin\release", "").Replace("\bin\x86\debug", "")
  11.         '// If not found folder then put the \ (BackSlash) at the end.
  12.         '/ Return Value
  13.         If Right(MyPath, 1) <> Chr(92) Then MyPath = MyPath & Chr(92)
  14.     End Function

  15.     ' / Function to test for Positive Integers with zero inclusive.
  16.     Public Function ValidateNumeric(ByVal strNumber As [String]) As Boolean
  17.         Dim Pattern As New Regex("[^0-9]")
  18.         Return Pattern.IsMatch(strNumber)
  19.     End Function

  20.     ' / --------------------------------------------------------------------------------
  21.     ' / ฟังค์ชั่นในการป้อนเฉพาะค่าตัวเลขได้เท่านั้น
  22.     Function CheckDigitOnly(ByVal index As Integer) As Boolean
  23.         Select Case index
  24.             Case 48 To 57 ' เลข 0 - 9
  25.                 CheckDigitOnly = False
  26.             Case 8, 13 ' Backspace = 8, Enter = 13
  27.                 CheckDigitOnly = False
  28.             Case Else
  29.                 CheckDigitOnly = True
  30.         End Select
  31.     End Function

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

  42.             Case 8, 13 ' Backspace = 8, Enter = 13
  43.             Case Else
  44.                 CheckCurrency = True
  45.         End Select
  46.     End Function

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

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

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

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

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

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

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

GMT+7, 2024-4-30 02:19 , Processed in 0.292766 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

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