Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
inDis.Items.Add("เช็นติเมตร")
inDis.Items.Add("นิ้ว")
inDis.Items.Add("ฟุต")
inDis.Items.Add("เมตร")
inDis.Items.Add("หลา")
inDis.Items.Add("กิโลเมตร")
outDis.Items.Add("เช็นติเมตร")
outDis.Items.Add("นิ้ว")
outDis.Items.Add("ฟุต")
outDis.Items.Add("เมตร")
outDis.Items.Add("หลา")
outDis.Items.Add("กิโลเมตร")
End Sub
Private Sub btn_sumdis_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_sumdis.Click
Dim num As Double
txtdisin.Text = num
Dim sum As Double
If inDis.SelectedIndex = 1 Then
outDis.SelectedIndex = 1
sum = num
txtdisout.Text = sum
ElseIf inDis.SelectedIndex = 1 Then
outDis.SelectedIndex = 2
sum = num * 0.39
txtdisout.Text = sum
ElseIf inDis.SelectedIndex = 1 Then
outDis.SelectedIndex = 3
sum = num * 0.03
txtdisout.Text = sum
ElseIf inDis.SelectedIndex = 1 Then
outDis.SelectedIndex = 4
sum = num * 0.01
txtdisout.Text = sum
ElseIf inDis.SelectedIndex = 1 Then
outDis.SelectedIndex = 5
sum = num * 0.01
txtdisout.Text = sum
ElseIf inDis.SelectedIndex = 1 Then
outDis.SelectedIndex = 6
sum = num * (1.0 * 10 ^ -5)
txtdisout.Text = sum
ElseIf inDis.SelectedIndex = 2 Then
outDis.SelectedIndex = 1
sum = num * 2.54
txtdisout.Text = sum
ElseIf inDis.SelectedIndex = 2 Then
outDis.SelectedIndex = 2
sum = num
txtdisout.Text = sum
ElseIf inDis.SelectedIndex = 2 Then
outDis.SelectedIndex = 3
sum = num * 0.08
txtdisout.Text = sum
ElseIf inDis.SelectedIndex = 2 Then
outDis.SelectedIndex = 4
sum = num * 0.03
txtdisout.Text = sum
ElseIf inDis.SelectedIndex = 2 Then
outDis.SelectedIndex = 5
sum = num * 0.03
txtdisout.Text = sum
ElseIf inDis.SelectedIndex = 2 Then
outDis.SelectedIndex = 6
sum = num * (2.54 * 10 ^ -5)
txtdisout.Text = sum
ElseIf inDis.SelectedIndex = 3 Then
outDis.SelectedIndex = 1
sum = num * 30.48
txtdisout.Text = sum
ElseIf inDis.SelectedIndex = 3 Then
outDis.SelectedIndex = 2
sum = num * 12
txtdisout.Text = sum
ElseIf inDis.SelectedIndex = 3 Then
outDis.SelectedIndex = 3
sum = num
txtdisout.Text = sum
ElseIf inDis.SelectedIndex = 3 Then
outDis.SelectedIndex = 4
sum = num*0.33
txtdisout.Text = sum
ElseIf inDis.SelectedIndex = 3 Then
outDis.SelectedIndex = 5
sum = num * 0.33
txtdisout.Text = sum
ElseIf inDis.SelectedIndex = 3 Then
outDis.SelectedIndex = 6
sum = num * 0.0003048
txtdisout.Text = sum
ElseIf inDis.SelectedIndex = 4 Then
outDis.SelectedIndex = 1
sum = num * 91.44
txtdisout.Text = sum
ElseIf inDis.SelectedIndex = 4 Then
outDis.SelectedIndex = 2
sum = num * 36
txtdisout.Text = sum
ElseIf inDis.SelectedIndex = 4 Then
outDis.SelectedIndex = 3
sum = num * 3
txtdisout.Text = sum
ElseIf inDis.SelectedIndex = 4 Then
outDis.SelectedIndex = 4
sum = num
txtdisout.Text = sum
ElseIf inDis.SelectedIndex = 4 Then
outDis.SelectedIndex = 5
sum = num * 1.093613
txtdisout.Text = sum
ElseIf inDis.SelectedIndex = 4 Then
outDis.SelectedIndex = 5
sum = num * 0.001
txtdisout.Text = sum
ElseIf inDis.SelectedIndex = 5 Then
outDis.SelectedIndex = 1
sum = num * 91.44
txtdisout.Text = sum
ElseIf inDis.SelectedIndex = 5 Then
outDis.SelectedIndex = 2
sum = num * 36
txtdisout.Text = sum
ElseIf inDis.SelectedIndex = 5 Then
outDis.SelectedIndex = 3
sum = num * 3
txtdisout.Text = sum
ElseIf inDis.SelectedIndex = 5 Then
outDis.SelectedIndex = 4
sum = num * 0.9144
txtdisout.Text = sum
ElseIf inDis.SelectedIndex = 5 Then
outDis.SelectedIndex = 5
sum = num
txtdisout.Text = sum
ElseIf inDis.SelectedIndex = 5 Then
outDis.SelectedIndex = 6
sum = num * 0.0009144
txtdisout.Text = sum
ElseIf inDis.SelectedIndex = 6 Then
outDis.SelectedIndex = 1
sum = num * 100000
txtdisout.Text = sum
ElseIf inDis.SelectedIndex = 6 Then
outDis.SelectedIndex = 2
sum = num * 39370.0787
txtdisout.Text = sum
ElseIf inDis.SelectedIndex = 6 Then
outDis.SelectedIndex = 3
sum = num * 3280.8399
txtdisout.Text = sum
ElseIf inDis.SelectedIndex = 6 Then
outDis.SelectedIndex = 4
sum = num * 1000
txtdisout.Text = sum
ElseIf inDis.SelectedIndex = 6 Then
outDis.SelectedIndex = 5
sum = num * 1093.6133
txtdisout.Text = sum
ElseIf inDis.SelectedIndex = 6 Then
outDis.SelectedIndex = 6
sum = num
txtdisout.Text = sum
End If
End Sub
Private Sub inDis_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles inDis.SelectedIndexChanged
End Sub
Private Sub outDis_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles outDis.SelectedIndexChanged
End Sub
Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
End Class
แต่ถ้าประกาศไว้ใน sub เช่นพวก Provate Sub Form1_Load อะไรพวกนี้ ก็จะเรียกใช้ได้เฉพาะ ในเหตุการ FormLoad เท่านั้น
Date :
2013-05-08 11:50:08
By :
CPU4Core
No. 7
Guest
มีตัวอย่างที่ดีมากมายบน Internet เกี่ยวกับการแปลงหน่วยนับ (Unit Converter) อทิเช่น
http://www.unitconverters.net/
ในกรณีนี้ผมหมายถึง ระยะทาง เท่านั้น
---- หน่วยนับย่อยที่สุดคือ เซนติเมตร (Centimeter)
---- หน่วยนับใหญ่ที่สุดคือ กิโลเมตร (km)
---- ใช้ ComboxBox.SelectedIndex เป็นตัวช่วยแปลงสลับไปมาได้ เช่น cm --> m หรือ m --> cm เป็นต้น
-------- cm = เซตติเมตร, m = เมตร
#NO6 กับการประยุกต์ใช้งานครับ
---- สร้าง Class สำหรับการแปลงหน่วยนับ's
Code (VB.NET)
Imports System
Imports System.Linq
Public Class UnitConverter
Private dicFormula As New Dictionary(Of String, List(Of Formula))
Sub New()
dicFormula.Add("mm2cm", New List(Of Formula) From {
New Formula() With {.SourceDesc = "mm2cm", .Ratio = 10, .Remark = "มิลลิเมตร 2 เซนติเมตร"}
})
dicFormula.Add("mm2m", New List(Of Formula) From {
New Formula() With {.SourceDesc = "mm2m", .Ratio = 1000, .Remark = "มิลลิเมตร 2 เมตร"}
})
dicFormula.Add("mm2km", New List(Of Formula) From {
New Formula() With {.SourceDesc = "mm2km", .Ratio = 1000000, .Remark = "มิลลิเมตร 2 กิโลเมตร"}
})
dicFormula.Add("cm2m", New List(Of Formula) From {
New Formula() With {.SourceDesc = "mm2cm", .Ratio = 100, .Remark = "มิลลิเมตร 2 เซนติเมตร"}
})
dicFormula.Add("cm2km", New List(Of Formula) From {
New Formula() With {.SourceDesc = "mm2m", .Ratio = 100000, .Remark = "มิลลิเมตร 2 เมตร"}
})
dicFormula.Add("m2km", New List(Of Formula) From {
New Formula() With {.SourceDesc = "mm2km", .Ratio = 1000, .Remark = "มิลลิเมตร 2 กิโลเมตร"}
})
End Sub
Public Function Calculate(ByVal n As Double, ByVal key As String) As Double
Dim iRet As Double = 0.0
Try
If dicFormula.ContainsKey(key) Then
iRet = n / dicFormula.Item(key)(0).Ratio 'dicFormula.TryGetValue(key, b)
End If
Catch ex As Exception
'1/0 --> Infinity (Programmer Error)
End Try
Return iRet
End Function
Private Class Formula
Sub New()
End Sub
Public Property SourceDesc As String
Public Property Ratio As Double = 1.0
Public Property Remark As String
End Class
End Class
Public Class frmUnitConverter
Private Sub frmUnitConverter_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Call FillMeasurement()
End Sub
'Fill ComboBox
Private Sub FillMeasurement()
cboFrom.Items.Add(New ListItem("มิลลิเมตร", "mm"))
cboFrom.Items.Add(New ListItem("เซนติเมตร", "cm"))
cboFrom.Items.Add(New ListItem("เมตร", "m"))
cboFrom.Items.Add(New ListItem("กิโลเมตร", "km"))
cboFrom.SelectedIndex = 0
'
cboTo.Items.Add(New ListItem("มิลลิเมตร", "mm"))
cboTo.Items.Add(New ListItem("เซนติเมตร", "cm"))
cboTo.Items.Add(New ListItem("เมตร", "m"))
cboTo.Items.Add(New ListItem("กิโลเมตร", "km"))
cboTo.SelectedIndex = 1
End Sub
'คำนวณ
Private Sub btnCalculate_Click(sender As System.Object, e As System.EventArgs) Handles btnCalculate.Click
Dim c As New UnitConverter()
Dim x = cboFrom.Items(cboFrom.SelectedIndex).Text.ToString()
Dim y = cboFrom.Items(cboFrom.SelectedIndex).Value.ToString()
Dim key As String = cboFrom.Items(cboFrom.SelectedIndex).Value.ToString() & "2" & cboTo.Items(cboTo.SelectedIndex).Value.ToString()
If cboFrom.SelectedIndex < cboTo.SelectedIndex Then
'หน่วยย่อย --> หน่วยที่ใหญ่กว่า (หาร)
txtTo.Text = c.Calculate(Convert.ToDouble(txtFrom.Text), key)
End If
End Sub
'มันควรจะอยู่ที่นี่หรือไม่?
Private Class ListItem
Public Property Text As String
Public Property Value As String
Public Sub New(ByVal txt As String, ByVal ID As String)
Text = txt
Value = ID
End Sub
Public Overrides Function ToString() As String
Return Text.ToString()
End Function
End Class
End Class
บันทัดที่ 25 ไม่ได้ใช้งาน Dim x = cboFrom.Items(cboFrom.SelectedIndex).Text.ToString() บันทัดที่ 26 ไม่ได้ใช้งาน Dim y = cboTo.Items(cboTo.SelectedIndex).Value.ToString()