Results 1 to 6 of 6

Thread: How to get sum of 12 textboxes in txtTotalAmount.Text?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2014
    Posts
    287

    How to get sum of 12 textboxes in txtTotalAmount.Text?

    Hi,

    I have 12 textboxes on the form. I need to get the total sum of those 12 boxes in one textbox (txtTotalAmount.Text). Text Box names are like txtJAN.text, txtFEB.Text .................... txtDEC.Text).

    What is the easiest way to do it.

    Thanks.

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

    Re: How to get sum of 12 textboxes in txtTotalAmount.Text?

    Don't use textboxes. Anything in a textbox is a string, so you'd have to convert it, or add code to restrict data entry. Restricting data entry doesn't work, so as long as you use a textbox, your code can fail. A NumericUpDown will work better. The Value property of that is type Decimal, and there is no means to get anything that is not a number from that.

    If you will always be summing just 12 boxes, you can use whatever means that you want. Directly adding 12 items in a single line will be the fastest execution you can get, and as far as writing the code, nothing else would necessarily be any faster. You could put the controls into a List(of NumericUpDown), then iterate through the list, but you might end up writing as much code to get the NUD into the list as you would take just writing out the addition. If the controls are all in the same container (whether a groupbox, panel, tab page, or the form itself), then they will already be in the Controls collection of that container, but if so, they'll be Controls, not NUD (or textboxes if you insist on using those), which means there will be a bit of code to turn them into the right control such that you can get at the value.

    So, I'd just write out the addition, unless the number of controls can change. If the number of controls can change, then I'd create a List(of WhateverControl) and add them all to that.
    My usual boring signature: Nothing

  3. #3
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    626

    Re: How to get sum of 12 textboxes in txtTotalAmount.Text?

    This should be enough

    Code:
    Dim sum = 0
            For Each c In Controls.OfType(Of TextBox)()
                If IsNumeric(c.Text) Then
                    sum += CInt(c.Text)
                End If
    
            Next
    Last edited by Delaney; Mar 23rd, 2021 at 02:53 PM. Reason: optimize code
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    10,275

    Re: How to get sum of 12 textboxes in txtTotalAmount.Text?

    I would highly recommend taking Shaggy's advice in using the appropriate control.

    However, as a purely function example, take a look at this:
    Code:
    Dim values = {txtJAN.Text, txtFEB.Text, ..., txtDEC.Text}
    Dim inValidValues = values.Where(Function(value) Not Double.TryParse(value, New Double()))
    If (inValidValues.Any()) Then
        Console.WriteLine("The following are not valid numbers: {0}", String.Join(", ", invalidValues))
        Return
    End If
    Dim sum = values.Select(Function(value) Convert.ToDouble(value)).Sum()
    Console.WriteLine(sum)
    I does the following:
    1. Store the values in a colleciton
    2. Query the values for ones that cannot be converted to a Double
    3. If there are any invalid values, do something, but stop execution from continuing
    4. If all the values are valid, then convert them all to a Double and then return the sum

  5. #5
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    626

    Re: How to get sum of 12 textboxes in txtTotalAmount.Text?

    Quote Originally Posted by Shaggy Hiker View Post
    Don't use textboxes.
    Or use a numeric textbox https://www.vbforums.com/showthread.php?887081-Numeric-only-textbox-including-scientific-notation

    I really have no shame to do a bit of advertisement . well, it is still a string and you need to convert it but it prevent to enter anything but numeric and scientific notation.
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    36,154

    Re: How to get sum of 12 textboxes in txtTotalAmount.Text?

    That's the quickest to write, so long as all the controls are found in the same Controls collection. It won't have the best possible performance, but that won't matter in most cases. You might improve it slightly with:
    Code:
    Dim sum = 0
    dim i As Integer
            For Each c In Controls.OfType(Of TextBox)()
                If integer.TryParse(c.Text,i) Then
                    sum += i
                End If
    
            Next
    If you switch to a NUD, then it would be:
    Code:
    Dim sum = 0
            For Each c In Controls.OfType(Of NumericUpDown)()
                sum += CInt(c.Value)
             Next
    Still requires that the controls all be in the same Controls Collection. Somewhat faster would be this:

    Code:
    Dim sum As Decimal
            For Each c In Controls.OfType(Of NumericUpDown)()
                sum += c.Value
             Next
    My usual boring signature: Nothing

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