|
-
Nov 17th, 2011, 08:31 PM
#1
Thread Starter
Member
Problems with Inputbox
I want to make it so when cancel is used it exits the loop but I need the Input as a decimal. Thanks for any help.
Code:
Do
txtItems.Text = intCounter.ToString
intCounter = intCounter + 1
msgInput = CDec(InputBox("Enter Item Price", "Item Price", ""))
decSubTotal = msgInput + decSubTotal
txtItemPrice.Text = msgInput.ToString("C2")
txtSubTotal.Text = decSubTotal.ToString("C2")
If msgInput = 0 Then
Exit Do
End If
Loop
decTax = CDec(decSubTotal * 0.065)
decTotal = decTax + decSubTotal
txtSalexTax.Text = decTax.ToString("C2")
txtTotalSale.Text = decTotal.ToString("C2")
-
Nov 17th, 2011, 09:03 PM
#2
Re: Problems with Inputbox
The primary problem with InputBox is that it exists. You should basically avoid using it if you possibly can. If you have to use it because the assignment says so, which I suspect is the case here, then you are stuck. Otherwise, you should create your own form that matches the rest of your UI and display it as a modal dialogue.
Assuming that you must use InputBox then here is some pseudo-code:
Code:
Do
Call InputBox and get result
If result is empty
Exit Do
Else
Try to convert input to number and use as required
End If
Loop
Unless you have been told that you can assume that the user input will always be a valid number, you should be using Decimal.TryParse to validate and convert rather than CDec, which will throw an exception if the data is not a valid number.
-
Nov 17th, 2011, 09:16 PM
#3
Thread Starter
Member
Re: Problems with Inputbox
 Originally Posted by jmcilhinney
The primary problem with InputBox is that it exists. You should basically avoid using it if you possibly can. If you have to use it because the assignment says so, which I suspect is the case here, then you are stuck. Otherwise, you should create your own form that matches the rest of your UI and display it as a modal dialogue.
Assuming that you must use InputBox then here is some pseudo-code:
Code:
Do
Call InputBox and get result
If result is empty
Exit Do
Else
Try to convert input to number and use as required
End If
Loop
Unless you have been told that you can assume that the user input will always be a valid number, you should be using Decimal.TryParse to validate and convert rather than CDec, which will throw an exception if the data is not a valid number.
Thank you very much, yeah I was gonna go back and use tryparse to validate it after I finished most of it.
-
Nov 18th, 2011, 09:00 PM
#4
Thread Starter
Member
Re: Problems with Inputbox
Could I somehow convert it all to a decimal after the string goes in?
-
Nov 18th, 2011, 09:28 PM
#5
Re: Problems with Inputbox
 Originally Posted by Josh704
Could I somehow convert it all to a decimal after the string goes in?
That's what Decimal.TryParse does.
-
Nov 19th, 2011, 12:41 PM
#6
Thread Starter
Member
Re: Problems with Inputbox
If I put the tryparse before this then it would convert it to a decimal before?
msgInput = CDec(InputBox("Enter Item Price", "Item Price", ""))
-
Nov 19th, 2011, 09:04 PM
#7
Re: Problems with Inputbox
How could you put the TryParse before that line when that line is the one that gets the user input? All you have to do is read my pseudo-code and do what it says. Get the input first, then check whether it's empty, then try to convert it if it's not.
-
Nov 21st, 2011, 10:06 AM
#8
Thread Starter
Member
Re: Problems with Inputbox
 Originally Posted by jmcilhinney
How could you put the TryParse before that line when that line is the one that gets the user input? All you have to do is read my pseudo-code and do what it says. Get the input first, then check whether it's empty, then try to convert it if it's not.
Yeah I see what your saying, I ended up figuring it out though. Thank you for the help!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|