combo box click event problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • IsaiahMatz
    New Member
    • Mar 2014
    • 6

    #1

    combo box click event problem

    Hey guys! I'm having trouble with my code. I want it to calculate the weight of the user on other planets. I want it to calculate as soon as they click the planet from the drop-down-list. The kicker is i have to go through and select it from the list twice to get the correct calculation. Ive been looking around to no avail. Please help. thanks!
    Code:
    Public Class frmMain
        Private Sub CalcMerc(PLANET_GRAV)
    
            Dim strEarthWeight As String
            Dim dblEarthWeight As Double
            Dim dblPlanetWeight As Double
    
            strEarthWeight = txtEarth.Text
            Double.TryParse(strEarthWeight, dblEarthWeight)
            dblPlanetWeight = dblEarthWeight * PLANET_GRAV
            txtConverted.Text = dblPlanetWeight.ToString
    
        End Sub
        Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            cboBox.Items.Add("Mercury")
            cboBox.Items.Add("Venus")
            cboBox.Items.Add("Mars")
            cboBox.Items.Add("Jupiter")
            cboBox.Items.Add("Saturn")
            cboBox.Items.Add("Uranus")
            cboBox.Items.Add("Neptune")
            cboBox.Items.Add("Pluto")
            cboBox.SelectedIndex = 3
    
        End Sub
        Private Sub cboBox_MouseClick(sender As Object, e As MouseEventArgs) Handles cboBox.MouseClick
    
            Const MERCURY_GRAV As Double = 0.38
            Const VENUS_GRAV As Double = 0.9
            Const MARS_GRAV As Double = 0.38
            Const JUPITER_GRAV As Double = 2.5
            Const SATURN_GRAV As Double = 0.91
            Const URANUS_GRAV As Double = 0.89
            Const NEPTUNE_GRAV As Double = 1.14
            Const PLUTO_GRAV As Double = 0.08
    
            If cboBox.SelectedIndex = 0 Then
                CalcMerc(MERCURY_GRAV)
            ElseIf cboBox.SelectedIndex = 1 Then
                CalcMerc(VENUS_GRAV)
            ElseIf cboBox.SelectedIndex = 2 Then
                CalcMerc(MARS_GRAV)
            ElseIf cboBox.SelectedIndex = 3 Then
                CalcMerc(JUPITER_GRAV)
            ElseIf cboBox.SelectedIndex = 4 Then
                CalcMerc(SATURN_GRAV)
            ElseIf cboBox.SelectedIndex = 5 Then
                CalcMerc(URANUS_GRAV)
            ElseIf cboBox.SelectedIndex = 6 Then
                CalcMerc(NEPTUNE_GRAV)
            ElseIf cboBox.SelectedIndex = 7 Then
                CalcMerc(PLUTO_GRAV)
            End If
    
        End Sub
    End Class
  • mcupito
    Contributor
    • Aug 2013
    • 294

    #2
    To re-cap: you say it works, but you have to select the option twice in the drop down?

    Did you try a different handler? Instead of MouseClick, maybe KeyDown or something else?

    Comment

    • IsaiahMatz
      New Member
      • Mar 2014
      • 6

      #3
      Yes i had to use the drop down and select it twice. I fixed it! KeyDown did the same thing. But SelectedIndexCh anged works perfectly! Im guessing because its really just an index value. Anywho, thanks a bunch!

      Comment

      • mcupito
        Contributor
        • Aug 2013
        • 294

        #4
        Good - Glad you got it. Whenever you encounter something goofy like that, just remember - it's the handler that is actually trying to use it. So play around with them. You'll learn a lot more that way also.

        Comment

        Working...