Click to See Complete Forum and Search --> : VB (I feel so noobish)
alucard0234
04-24-2007, 01:07 AM
I keep getting mixed answers for my current VB app.
I'm trying to construct a calculator to take a book's price, multiply it by the quantity, then add on my 15% discount. However, I keep getting a blue line when inputting my data...
Ok, so under my text box, I input quantitybox = integer.parse
My friend keeps telling me to do that.
Another one told me to change my calculate button from
Dim quantitybox as integer
Dim price as decimal
Dim TextBox1 as decimal
Dim boxdiscount as decimal
to
Try
Dim quantitybox as integer
Dim price as decimal
Dim TextBox1 as decimal
Dim boxdiscount as decimal
Catch Ex as Exception
End Try
This project went from easy to confusing. Mind helping?
JPnyc
04-26-2007, 03:23 PM
I think I'd need to see the entire code
Tony2005
04-27-2007, 09:06 AM
By the looks of it I'm assuming your using .net. The try-catch code will try to run the declare statements in between it and catch any exceptions that may be thrown. (although since your just declaring variables that probability of an exception rising is very low).
Basically I'm going to assume your form 3 text boxes
1 for book price (BookPriceTextBox )
1 for book quantity (QuantityTextBox )
1 for Discount (DiscountTextBox )
a button (CalculateButton )
and a label to show the result ((ResultLabel)
Now what you can do is create 3 variables to store the double/integer representation of the text box contents, 1 to store the result and another variable as a flag (which you'll understand later) so if you double click on the button it will create a sub to run when the button is pressed. Write the following code with in that sub.
Dim BookPrice as double
Dim Quantity as double
dim Discount as integer
dim Result as double
dim StopFlag as boolean
The next thing you need to do is to check if the textboxs contain numeric values (I.E 26, 32.6, 98) and display an error message if they don't
if not double.tryparse(BookPriceTextBox.text,BookPrice ) then
messagebox.show("Book Price is not a numeric value", "Error Message")
StopFlag = true
end if
You can probably figure out how to do the other two text boxes, just remember that one of them is an integer not a double. The double.tryparse will try to parse the textbox contents, if successful the result will be stored in the BookPrice varaible and will return true to the if statement and false if not
Now that you've checked the text boxes its time to calculate the result and this is where the StopFlag variable comes in to play. If at any point one of the 3 textboxes does not contain a numeric value (for example some one entered "dfg" instead of "34.99") the StopFlag will have been set to true, this will indicate to us not to continue with the calculation. See below:
if not StopFlag then
Result = (BookPrice*Quantity)* (100-Discount)
ResultLabel.text = Result.tostring
end if
Hope this helps, I haven't tried the code its just of the top of my head
SysOpt.com
Copyright Internet.com Inc. All Rights Reserved.