11 Replies - 470 Views - Last Post: 14 November 2019 - 06:10 AM Rate Topic: -----

#1 deannas99   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 21-January 19

How to get the sum of numbers in a single textbox?

Posted 13 November 2019 - 05:58 PM

I am currently working with asp.net vb code. I am trying to add numbers together each time a calculate button is cleared. I currently have a single textbox that I use to enter messages sent in a payroll application. I want to calculate the sum of the numbers each time the calculate button is clicked. I only have the one textbox. So the user will be entering the messages into THE SAME TEXTBOX and each time a new number is entered it will be added together.

I've tried
tbTotalMessages += messages
to try and add them(I assumed that's how you do it cuz that's what was explained in class but I think I misunderstood) because all that does is add the same number so if i put "23" then each time the button is clicked it goes "23232323"

This is the code I have for the calculate button I also have a pieceworkWorker class

 Dim inputName As String = tbWorkersName.Text
 Dim inputMessages As String = tbMessagesSent.Text
 Dim messages As Integer = 0
 Dim errorMessage As String = ""
 Dim overallMessages As Integer = 0

        If radioButtonList.SelectedIndex = 0 Then
            Dim newWorker As PieceworkWorker

            If inputName.Trim = "" Then
                errorMessage += "Please enter a valid name"
            End If

            If Not (Integer.TryParse(inputMessages, messages)) AndAlso messages > -1 Then
                If errorMessage <> "" Then
                    errorMessage += Environment.NewLine
                End If

                errorMessage += "Please enter a valid number of text messages sent"
            End If

            If errorMessage <> "" Then
                Response.Write(errorMessage)
            Else
                newWorker = New PieceworkWorker(inputName, messages)
                newWorker.FindPay()
                tbLastWorkerPay.Text = newWorker.CalculatedPay.ToString()

            End If
        ElseIf radioButtonList.SelectedValue = 1 Then
            Dim newWorker As SeniorWorker

            If inputName.Trim = "" Then
                errorMessage += "Please enter a valid name"
            End If

            If Not (Integer.TryParse(inputMessages, messages)) AndAlso messages > -1 Then
                If errorMessage <> "" Then
                    errorMessage += Environment.NewLine
                End If

                errorMessage += "Please enter a valid number of text messages sent"
            End If

            If errorMessage <> "" Then
                Response.Write(errorMessage)
            Else
                newWorker = New SeniorWorker(inputName, messages)
                newWorker.FindPay()
                tbLastWorkerPay.Text = newWorker.CalculatedPay.ToString()
            End If
        End If

        tbNumberOfWorkers.Text = Val(tbNumberOfWorkers.Text) + 1.ToString()
        tbTotalMessages.Text += messages


Is This A Good Question/Topic? 0
  • +

Replies To: How to get the sum of numbers in a single textbox?

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15476
  • View blog
  • Posts: 62,006
  • Joined: 12-June 08

Re: How to get the sum of numbers in a single textbox?

Posted 13 November 2019 - 07:12 PM

The code is a little fuzzy, but looks like in there your input from a textbox is being treated as a string.

You would need to pull the value out, convert it to an integer (or some numerical data type), add it, and take that result and shove it where it needs to go.
Was This Post Helpful? 0
  • +
  • -

#3 deannas99   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 21-January 19

Re: How to get the sum of numbers in a single textbox?

Posted 13 November 2019 - 07:20 PM

View Postmodi123_1, on 13 November 2019 - 07:12 PM, said:

The code is a little fuzzy, but looks like in there your input from a textbox is being treated as a string.

You would need to pull the value out, convert it to an integer (or some numerical data type), add it, and take that result and shove it where it needs to go.



I have also tried do
tbTotalMessages += Integer.Parse(messages)
but that just says type string cannot be converted to type double, or something like that I'm not entirely sure how to pull the value out
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15476
  • View blog
  • Posts: 62,006
  • Joined: 12-June 08

Re: How to get the sum of numbers in a single textbox?

Posted 13 November 2019 - 07:27 PM

Is the datatype for tbTotalMessages a textbox? If so you cannot assign a value to the entire textbox object. You would need to assign it to the .text property.. which is a string.
Was This Post Helpful? 0
  • +
  • -

#5 deannas99   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 21-January 19

Re: How to get the sum of numbers in a single textbox?

Posted 13 November 2019 - 07:34 PM

View Postmodi123_1, on 13 November 2019 - 07:27 PM, said:

Is the datatype for tbTotalMessages a textbox? If so you cannot assign a value to the entire textbox object. You would need to assign it to the .text property.. which is a string.


Oh yes sorry I forgot to add that part i did try
 tbTotalMessages.Text += Integer.Parse(messages)


thats when i get the error
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15476
  • View blog
  • Posts: 62,006
  • Joined: 12-June 08

Re: How to get the sum of numbers in a single textbox?

Posted 13 November 2019 - 07:41 PM

Okay so think about what you are doing. Is messages an integer? Why would you be parsing that in an integer?
Was This Post Helpful? 0
  • +
  • -

#7 deannas99   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 21-January 19

Re: How to get the sum of numbers in a single textbox?

Posted 13 November 2019 - 07:46 PM

View Postmodi123_1, on 13 November 2019 - 07:41 PM, said:

Okay so think about what you are doing. Is messages an integer? Why would you be parsing that in an integer?

Because I read somewhere that in order to get them not to cocatenate I had to use Integer.Parse.

But that's what I'm not understanding cuz yeah the messages is an integer but the textbox is for a string, so I'm honestly just confused with the whole thing here
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15476
  • View blog
  • Posts: 62,006
  • Joined: 12-June 08

Re: How to get the sum of numbers in a single textbox?

Posted 13 November 2019 - 07:49 PM

You would use the integer.parse to take a string and convert it to a number. Do your math.. and when it comes to converting the integer back to a string use the '.tostring'.
Was This Post Helpful? 0
  • +
  • -

#9 deannas99   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 21-January 19

Re: How to get the sum of numbers in a single textbox?

Posted 13 November 2019 - 07:53 PM

View Postmodi123_1, on 13 November 2019 - 07:49 PM, said:

You would use the integer.parse to take a string and convert it to a number. Do your math.. and when it comes to converting the integer back to a string use the '.tostring'.

Oooh okay that makes sense. Though the problem still remains with how do I do the math from the one text box
Was This Post Helpful? 0
  • +
  • -

#10 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15476
  • View blog
  • Posts: 62,006
  • Joined: 12-June 08

Re: How to get the sum of numbers in a single textbox?

Posted 13 November 2019 - 07:56 PM

I think I am following you were the total is stored in a textbox, right? The input is in a textbox too?

Convert both textbox .text strings to integers.. do the math.. and .tostring to store it in the total .text field.
Was This Post Helpful? 0
  • +
  • -

#11 deannas99   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 21-January 19

Re: How to get the sum of numbers in a single textbox?

Posted 13 November 2019 - 08:04 PM

View Postmodi123_1, on 13 November 2019 - 07:56 PM, said:

I think I am following you were the total is stored in a textbox, right? The input is in a textbox too?

Convert both textbox .text strings to integers.. do the math.. and .tostring to store it in the total .text field.


I've attached the way the page looks in the asp.net web page. The user enters the the messages into the messages textbox and each time the calculate button is clicked the new number in the messages textbox is supposed to be added to the previous number that was typed into that same textbox. And then the calculated number goes into the total messages sent textbox

View Postdeannas99, on 13 November 2019 - 08:02 PM, said:

View Postmodi123_1, on 13 November 2019 - 07:56 PM, said:

I think I am following you were the total is stored in a textbox, right? The input is in a textbox too?

Convert both textbox .text strings to integers.. do the math.. and .tostring to store it in the total .text field.


I've attached the way the page looks in the asp.net web page. The user enters the the messages into the messages textbox and each time the calculate button is clicked the new number in the messages textbox is supposed to be added to the previous number that was typed into that same textbox. And then the calculated number goes into the total messages sent textbox


Ah the attachment didnt workk
Was This Post Helpful? 0
  • +
  • -

#12 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6831
  • View blog
  • Posts: 28,338
  • Joined: 12-December 12

Re: How to get the sum of numbers in a single textbox?

Posted 14 November 2019 - 06:10 AM

Post more representative code, including the event procedure(s) that the code is wrapped in.

If there remains an error, then quote the error details and advise us which line generates the error.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1