Results 1 to 8 of 8

Thread: Problems with Inputbox

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2011
    Posts
    56

    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")

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2011
    Posts
    56

    Re: Problems with Inputbox

    Quote Originally Posted by jmcilhinney View Post
    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.

  4. #4

    Thread Starter
    Member
    Join Date
    Nov 2011
    Posts
    56

    Re: Problems with Inputbox

    Could I somehow convert it all to a decimal after the string goes in?

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Problems with Inputbox

    Quote Originally Posted by Josh704 View Post
    Could I somehow convert it all to a decimal after the string goes in?
    That's what Decimal.TryParse does.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Member
    Join Date
    Nov 2011
    Posts
    56

    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", ""))

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Member
    Join Date
    Nov 2011
    Posts
    56

    Re: Problems with Inputbox

    Quote Originally Posted by jmcilhinney View Post
    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
  •  



Click Here to Expand Forum to Full Width