By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,985 Members | 1,863 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,985 IT Pros & Developers. It's quick & easy.

combo box click event problem

P: 6
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!
Expand|Select|Wrap|Line Numbers
  1. Public Class frmMain
  2.     Private Sub CalcMerc(PLANET_GRAV)
  3.  
  4.         Dim strEarthWeight As String
  5.         Dim dblEarthWeight As Double
  6.         Dim dblPlanetWeight As Double
  7.  
  8.         strEarthWeight = txtEarth.Text
  9.         Double.TryParse(strEarthWeight, dblEarthWeight)
  10.         dblPlanetWeight = dblEarthWeight * PLANET_GRAV
  11.         txtConverted.Text = dblPlanetWeight.ToString
  12.  
  13.     End Sub
  14.     Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  15.  
  16.         cboBox.Items.Add("Mercury")
  17.         cboBox.Items.Add("Venus")
  18.         cboBox.Items.Add("Mars")
  19.         cboBox.Items.Add("Jupiter")
  20.         cboBox.Items.Add("Saturn")
  21.         cboBox.Items.Add("Uranus")
  22.         cboBox.Items.Add("Neptune")
  23.         cboBox.Items.Add("Pluto")
  24.         cboBox.SelectedIndex = 3
  25.  
  26.     End Sub
  27.     Private Sub cboBox_MouseClick(sender As Object, e As MouseEventArgs) Handles cboBox.MouseClick
  28.  
  29.         Const MERCURY_GRAV As Double = 0.38
  30.         Const VENUS_GRAV As Double = 0.9
  31.         Const MARS_GRAV As Double = 0.38
  32.         Const JUPITER_GRAV As Double = 2.5
  33.         Const SATURN_GRAV As Double = 0.91
  34.         Const URANUS_GRAV As Double = 0.89
  35.         Const NEPTUNE_GRAV As Double = 1.14
  36.         Const PLUTO_GRAV As Double = 0.08
  37.  
  38.         If cboBox.SelectedIndex = 0 Then
  39.             CalcMerc(MERCURY_GRAV)
  40.         ElseIf cboBox.SelectedIndex = 1 Then
  41.             CalcMerc(VENUS_GRAV)
  42.         ElseIf cboBox.SelectedIndex = 2 Then
  43.             CalcMerc(MARS_GRAV)
  44.         ElseIf cboBox.SelectedIndex = 3 Then
  45.             CalcMerc(JUPITER_GRAV)
  46.         ElseIf cboBox.SelectedIndex = 4 Then
  47.             CalcMerc(SATURN_GRAV)
  48.         ElseIf cboBox.SelectedIndex = 5 Then
  49.             CalcMerc(URANUS_GRAV)
  50.         ElseIf cboBox.SelectedIndex = 6 Then
  51.             CalcMerc(NEPTUNE_GRAV)
  52.         ElseIf cboBox.SelectedIndex = 7 Then
  53.             CalcMerc(PLUTO_GRAV)
  54.         End If
  55.  
  56.     End Sub
  57. End Class
  58.  
Mar 24 '14 #1

✓ answered by mcupito

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?

Share this Question
Share on Google+
3 Replies


100+
P: 294
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?
Mar 25 '14 #2

P: 6
Yes i had to use the drop down and select it twice. I fixed it! KeyDown did the same thing. But SelectedIndexChanged works perfectly! Im guessing because its really just an index value. Anywho, thanks a bunch!
Mar 25 '14 #3

100+
P: 294
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.
Mar 25 '14 #4

Post your reply

Sign in to post your reply or Sign up for a free account.