Re: VB 2010 - Unit Converter
Hi... Welcome to the forums...:wave:
I made a small very basic example:
vb.net Code:
Public Class Form1
'~~~ On clicking the button
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Variables
Dim dblInput As Double
Dim dblOutput As Double
Dim dblFactor As Double
'~~~ Converting the input entered by user to Double type
If Double.TryParse(TextBox1.Text, dblInput) = False Then
MessageBox.Show("Can't convert the input value to Double !")
Else
'~~~ Checking what conversion is selected by the user, from the ComboBox
Select Case ComboBox1.SelectedIndex
Case 0 '~~~ cm to m
dblFactor = 1 / 100
Case 1 '~~~ m to cm
dblFactor = 100
Case 2 '~~~ cm to mm
dblFactor = 10
Case 3 '~~~ mm to cm
dblFactor = 1 / 10
End Select
'~~~ Do the math
dblOutput = dblInput * dblFactor
'~~~ Display the result
MessageBox.Show("Output = " & dblOutput.ToString("###,###,##0.00"))
End If
End Sub
'~~~ During loading, fill the ComboBox with some text
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ComboBox1.Items.Add("cm to m") '~~~ At index 0 of ComboBox
ComboBox1.Items.Add("m to cm") '~~~ At index 1 of ComboBox
ComboBox1.Items.Add("cm to mm") '~~~ At index 2 of ComboBox
ComboBox1.Items.Add("mm to cm") '~~~ At index 3 of ComboBox
'..etc..
End Sub
End Class
Hope you'll get some ideas. :)
:wave:
Re: VB 2010 - Unit Converter
I disagree with using a single Select statement with a Case for every conversion. Each time you add a unit, the number of Cases grows exponentially. You should pick a single base until and then you just need a single conversion factor for each other unit to that base. Say you pick metres as the base. If the user wants to convert from kilometres to inches, you convert the kilometres to metres and the metres to inches. Three units only need to two conversion factors.
Re: VB 2010 - Unit Converter
Starting point for parsing the user input
Code:
Dim smplTB As String = "1,000.045 km" 'simulated textbox entry
Dim parts() As String = smplTB.Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries)
parts(0) = parts(0).Replace(",", "")
Dim valPart As Double
Dim unitPart As String
Dim numDecs As Integer = 0
If Double.TryParse(parts(0), valPart) Then
'valid number
unitPart = parts(1)
Debug.WriteLine(unitPart & " " & valPart.ToString)
If parts(0).IndexOf("."c) <> -1 Then
numDecs = parts(0).Length - parts(0).IndexOf("."c) - 1
End If
Debug.WriteLine(numDecs.ToString)
End If
Re: VB 2010 - Unit Converter
Convert the from value to a common base. Then the to Value can be computed from there.
This code (building on previous post) shows a start at the process
Code:
Dim meters As Double = 0
'from unit converted to meters
Select Case unitPart
Case ""
Case "km"
meters = valPart * 1000
Case "m"
meters = valPart
Case "cm"
meters = valPart / 100
Case "mm"
meters = valPart / 1000
Case "in"
meters = valPart * 0.00254
Case "ft"
meters = valPart * 0.3048
Case "mi"
meters = valPart * 1609.3
End Select
Dim toUnit As String = "in"
Dim toVal As Double = 0
'to unit based on meters
Select Case toUnit
Case "in"
toVal = meters * 39.37
Case "ft"
toVal = meters * 3.28
End Select
http://inotek.com/conversions.html