|
-
Feb 27th, 2011, 01:21 AM
#1
Thread Starter
New Member
VB 2010 - Unit Converter
I am attempting to make a unit converter that converts between km, m, cm, miles, yards, feet, and inches. I have a textbox that is used to input the value to convert followed by a space and the units (i.e., 56 km) and a second textbox where the user types what unit they want to convert to (i.e., cm). When converting, the output will be displayed in the second textbox. (I know this is complicated, but this is the way it is to be done).
I am doing this using sub and function procedures and If blocks and Select Case blocks (i.e., keeping it as simple as possible).
I'm not sure how to declare the variables because they contain both numbers and letters so I declared as strings (Dim unitFrom, unitTo As String = txtUnitTo.Text, etc.). I then tried to use case blocks where:
Select Case unitTo
Case "cm"
txtUnitTo.Text = CStr(txtUnitFrom.Text) & "cm" [to display the number and unit -- i.e, 5,600,000 cm]
This, however, is not working and I think I am looking at this all wrong. I have also tried using the Double variable but am running into the same problem.
I also need to add a comma every 3 digits in the answer, which i am unsure on how to do. Any help would be appreciated on where to start or how to accomplish this. As an added note, I am a beginning programmer so if explanations could be kept in simple terms, I would appreciate it.
-
Feb 27th, 2011, 04:13 AM
#2
Re: VB 2010 - Unit Converter
Hi... Welcome to the forums...
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. 
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Feb 27th, 2011, 06:09 AM
#3
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.
-
Feb 27th, 2011, 10:43 AM
#4
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
-
Feb 27th, 2011, 11:10 AM
#5
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
Last edited by dbasnett; Feb 27th, 2011 at 11:16 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|