PDA

View Full Version : Solved: Form Help



austenr
03-09-2010, 08:43 PM
Im using the following sub in a form to calculate the calories burned based on the number of miles ran which is input by the user in a text box.

Problem is, when the button is clicked, the label that is supposed to display the calories burned returns zero and the text box where the user enters the number of miles they ran turns to zero.


Private Sub btnCalories_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalories.Click
Dim decMilesRan As Decimal
Const _cdecCaloriesBurnedPerHour As Decimal = 700D
txtMilesRan.Text = Convert.ToDecimal(decMilesRan)
lblCaloriesBurned.text = _cdecCaloriesBurnedPerHour * decMilesRan
End Sub


Probably something simple but I am not seeing it.

GTO
03-09-2010, 09:39 PM
Greetings,

Little chance of this being of any value, but cat-killin' curiosity has just got the best of me.

Is this in a class or VB or ver? Just wondering as I don't recognize COnvert.ToDecimal and am used to a Label having a Caption rather than Text.

Anyways, probably nothing, but presuming Decimal is supported (I'm currently in excel 2000), what does the "D" do?

Mark

Jacob Hilderbrand
03-09-2010, 10:49 PM
The line should be:


decMilesRan = TxtMilesRan.Text

If you want to account for non numberic entries then use:


decMilesRan = Val(TxtMilesRan.Text)


You code is changing the textbox to the value of decMilesRan which is never set and therefore 0. Then the multiplication below is 0 as well.

Alternately you can replace the entire code with this:


LblCaloriesBurned.Text = 700 * Val(TxtMilesRan.Text)

austenr
03-10-2010, 09:48 AM
Thanks Jake