hey im trying to create a program where the user will type in a number then choose the units its in and units it will be converted to from 2 comboboxes. The output should be changing as different units are clicked without a button.Please tell me how to fix my code and whats wrong with it.Thanks

Public Class Convert

    Sub Calculate()
        Dim Unit1 As Double = (ComboBox1.SelectedItem)
        Dim Unit2 As Double = (ComboBox2.SelectedItem)
        Dim Input As Long

        lblValue.Text = Input * Unit1 / Unit2
    End Sub

    Function Change(ByVal Unit)
        Select Case Unit
            Case "Meter"
                Return 1
            Case "Nanometer"
                Return 0.000000001
            Case "Centimeter"
                Return 0.01
            Case "Millimeter"
                Return 0.001
            Case "Kilometer"
                Return 1000
            Case "Hectometer"
                Return 100
            Case "Inch"
                Return 39.37
            Case "Feet"
                Return 0.3048
            Case "Mile"
                Return 1069.344
            Case "Yard"
                Return 0.9144
        End Select
    End Function

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        Call Calculate()
    End Sub

    Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
        Call Calculate()
    End Sub

    Private Sub Input_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Input.TextChanged
        Call Calculate()
    End Sub
End Class

The problem is in the method Calculate(). The "SelectedItem" property returns an object, which is, on this time, it returns the string you have selected and you assigned it to different type which is double.

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        ComboBox1.Items.AddRange(New Object() {"Meter", "Nanometer", "Centimeter", "Millimeter", "Kilometer", "Hectometer", "Inch", "Feet", "Mile", "Yard"})
        ComboBox2.Items.AddRange(New Object() {"Meter", "Nanometer", "Centimeter", "Millimeter", "Kilometer", "Hectometer", "Inch", "Feet", "Mile", "Yard"})

        ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
        ComboBox2.DropDownStyle = ComboBoxStyle.DropDownList

        ComboBox1.SelectedIndex = 0
        ComboBox2.SelectedIndex = 2
    End Sub

    Sub Calculate()
        If ComboBox1.SelectedItem Is Nothing OrElse ComboBox2.SelectedItem Is Nothing Then Exit Sub
        Dim Unit1 As Double = Change(ComboBox1.SelectedItem)
        Dim Unit2 As Double = Change(ComboBox2.SelectedItem)

        lblValue.Text = (Convert.ToDouble(textInput.Text) * Unit1 / Unit2).ToString
    End Sub

    Function Change(ByVal Unit) As Double
        Select Case Unit
            Case "Meter"
                Return 1
            Case "Nanometer"
                Return 0.000000001
            Case "Centimeter"
                Return 0.01
            Case "Millimeter"
                Return 0.001
            Case "Kilometer"
                Return 1000
            Case "Hectometer"
                Return 100
            Case "Inch"
                Return 39.37
            Case "Feet"
                Return 0.3048
            Case "Mile"
                Return 1069.344
            Case "Yard"
                Return 0.9144
            Case Else
                Return 0
        End Select
    End Function

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged, ComboBox2.SelectedIndexChanged
        Call Calculate()
    End Sub

    Private Sub textInput_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles textInput.TextChanged
        Call Calculate()
    End Sub

End Class

Edited 5 Years Ago by jireh: n/a

Wow.Your good at this thankssssss so much.Im new to vb and you really helped me

This question has already been answered. Start a new discussion instead.