Results 1 to 8 of 8

Thread: [RESOLVED] Work out sum from textbox

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2011
    Posts
    9

    Resolved [RESOLVED] Work out sum from textbox

    Hello,

    I have been having trouble figuring out how to calculate a sum that the user inputs in to a textbox. On my form I have a textbox and a button. I would like the user to be able to input a sum like "3.5 + 2.87" and then click the button and the form calculates the answer for them. I need to be able to convert the string in to a sum somehow and then calculate it.
    Any help would be much appreciated.

    Thanks in advance,

    Harry

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Work out sum from textbox

    Search the forum for TryParse. An example is written a few times each week, and has been for the last decade, so you will end up with THOUSANDS of examples. TryParse is what you need to use to do this correctly, because every other alternative will throw exceptions unless the user enters just the right thing. TryParse is forgiving (technically, Val() is also forgiving, but it will occasionally do things that you don't expect, so it is risky to use it).
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2011
    Posts
    9

    Re: Work out sum from textbox

    Thank you for your quick reply.
    I have had a look at tryparse but haven't had a lot of luck. I think I am having the problem because in my textbox the user will input a sign (+ or - or / or *) in to the textbox to make a sum.

    I tried this but I'm not sure I did it correctly:

    Code:
    Dim number As String = Sumtxtbox.Text 'The text in the textbox is "0.20 + 1.20"
                Dim total As Integer
                Double.TryParse(number, total)
                MsgBox(total)
    Thank you again for your advice.

    Thanks,

    Harry

  4. #4
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Work out sum from textbox

    You have 3 parts in there, so the first step is to split them out. Which given your example is fairly easy as you have spaces in between the parts. Once you have the 3 parts, as given above, you can TryParse the numbers, and then use a Select on the other part which should give you what you need.

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Work out sum from textbox

    Ah, I misunderstood the question. TryParse won't directly work in your case. What Grimfort suggested will work for most cases, but it becomes incredibly difficult in a general case. As long as you just allow two numbers and one operator, all is well, but what will stop the user from typing in something like:

    "2.57 + 3.64 * 4.02"

    There is no longer a simple solution for that. It gets even worse if the user enters parentheses. At that point you are parsing the string to see whether it is a valid equation, then evaluating it. That isn't a particularly simple task. Even the case of having two operators isn't all that easy, since you start dealing with operator precedence. In fact, for that matter, even the case of having a single operator is problematic, because you would have to deal with:

    "2.56+4.64"

    as well as

    "2.56 + 4.64"

    so you are at the mercy of the user typing things in correctly. Are you sure you have to do it that way?
    My usual boring signature: Nothing

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Work out sum from textbox

    this is the simplest way:

    vb Code:
    1. Dim dt As New DataTable
    2. MsgBox(dt.Compute(Sumtxtbox.Text, "").ToString)

  7. #7

    Thread Starter
    New Member
    Join Date
    Dec 2011
    Posts
    9

    Re: Work out sum from textbox

    Thank you so much for all your help!
    Much appreciated.

    I have used .paul.'s solution and it works perfectly!


    Thanks again,

    Harry

  8. #8
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: [RESOLVED] Work out sum from textbox

    That is in fact a great idea, well noted.

Tags for this Thread

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