Results 1 to 5 of 5

Thread: VB 2010 - Unit Converter

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    6

    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.

  2. #2
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: VB 2010 - Unit Converter

    Hi... Welcome to the forums...

    I made a small very basic example:
    vb.net Code:
    1. Public Class Form1
    2.  
    3.     '~~~ On clicking the button
    4.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    5.         'Variables
    6.         Dim dblInput As Double
    7.         Dim dblOutput As Double
    8.         Dim dblFactor As Double
    9.  
    10.         '~~~ Converting the input entered by user to Double type
    11.         If Double.TryParse(TextBox1.Text, dblInput) = False Then
    12.             MessageBox.Show("Can't convert the input value to Double !")
    13.         Else
    14.             '~~~ Checking what conversion is selected by the user, from the ComboBox
    15.             Select Case ComboBox1.SelectedIndex
    16.                 Case 0  '~~~ cm to m
    17.                     dblFactor = 1 / 100
    18.                 Case 1  '~~~ m to cm
    19.                     dblFactor = 100
    20.                 Case 2  '~~~ cm to mm
    21.                     dblFactor = 10
    22.                 Case 3  '~~~ mm to cm
    23.                     dblFactor = 1 / 10
    24.             End Select
    25.  
    26.             '~~~ Do the math
    27.             dblOutput = dblInput * dblFactor
    28.  
    29.             '~~~ Display the result
    30.             MessageBox.Show("Output = " & dblOutput.ToString("###,###,##0.00"))
    31.         End If
    32.     End Sub
    33.  
    34.     '~~~ During loading, fill the ComboBox with some text
    35.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    36.         ComboBox1.Items.Add("cm to m")  '~~~ At index 0 of ComboBox
    37.         ComboBox1.Items.Add("m to cm")  '~~~ At index 1 of ComboBox
    38.         ComboBox1.Items.Add("cm to mm") '~~~ At index 2 of ComboBox
    39.         ComboBox1.Items.Add("mm to cm") '~~~ At index 3 of ComboBox
    40.         '..etc..
    41.     End Sub
    42. 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,...

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width