You have several issues with your attempts. Most importantly you are trying to mix numeric data and string data. Sometimes you will get an implicit conversion done "for you" but you should never rely on this.
In your second attempt you are confusing
SelectedIndex
(the rank of the selected item within the list) and
SelectedItem
(the actual item selected). - I somehow doubt that you have 3600 items in the comboBox fy.
Instead of having multiple
if
statements consider using some form of lookup (have a look through
sergey's solutions[
^] as I know he's written several good solutions on this subject)
I've tested this code and it works
Dim factors As Double() = {8.56, 7, 5, 4.31}
Const multiplier As Double = 10000.0
Dim fcuAsDouble As Double
If Not Double.TryParse(fcu.Text, fcuAsDouble) Then
End If
Dim result As Double = (factors(fy.SelectedIndex) / multiplier) * fcuAsDouble
umax.Text = result.ToString()
factors
is my lookup for your values that you are using in your multiple if statements. I've used an array for simplicity but there are other (and better) options.
I've also defined
multiplier
as a constant because I don't like "magic numbers"
Next I look to the
fcu
textbox and convert it's text into a double so I can use it in the calculation. Note the use of
Double.TryParse()
- it will convert text into a Double value if in the right format otherwise it will return False. Double.Parse will error if fcu contained (for example) "three"
In the next line I use
fy.SelectedIndex
to look up the appropriate factor from my array, and work out the result.
Finally when I assign the result into the umax text box I convert it to text using
ToString()
All of the above I put into the
fy_SelectedIndexChanged
event as per my earlier comments