none
how to add values of different textboxes and put the value on another textbox? RRS feed

  • Question

  • i tried to do:

    textboxtotal.text = Cdbl(textbox1.text)+Cdbl(textbox2.text)+Cdbl(textbox3.text)+... 

    , but didn't work, i tried to create variables ex: val1, val 2, val3 and put val1 = Cdbl(textbox1.text) and tried to add the variables, but didn't work either D:, can anyone help?


    Wednesday, June 20, 2018 5:50 PM

Answers

  • dude... my code is working fine, i tried your function Regards Les, i told up there that doesnt work with me, saying again, i dont need to change the code that i already have, because is doing exactly what i want. And i already got it, :D so, ty for your "effort".
    • Marked as answer by Gustavo Nikov Sunday, June 24, 2018 7:49 PM
    Thursday, June 21, 2018 6:04 AM

All replies

  • Hi

    You are doing well. You have the correct idea.

    Take each TextBox.Text, convert to a numerical value, add them together and put the sum of them into another TextBox.Text - as a String.

    The one thing to consider is that if a TextBox contains anything that is NOT numerical, then an exception would occur. One way to deal with that is to use a Function to take a String and Return a numerical value - OR a zero if it can't convert to a valid numerical value.

    Such a Function could be like this:

    Function GetDouble(s As String) As Double
      Dim v As Double = 0.0
      If Double.TryParse(s, v) Then Return v
      Return 0.0
    End Function

    and could be used like this

        Dim tot As Double = 0.0
        tot = GetDouble(TextBox1.Text) + GetDouble(TextBox2.Text) + GetDouble(TextBox3.Text) ' etc .....
    
        TextBox4.Text = tot.ToString
    

    If you test this out, compare trying your version with some non-numerical input versus this version and you will see the difference.


    Regards Les, Livingston, Scotland

    Wednesday, June 20, 2018 6:31 PM
  • hi! so, Ieshay, i thought for this case too, but, my empty textboxes i filled with 0, so no need to this function, and even so, is not working D:
    Wednesday, June 20, 2018 6:45 PM
  • Here is a working conceptual example using a control List(Of TextBox). This can be expanded to get as many TextBox controls as you want.

    Private Sub Demo()
        Dim tbList As New List(Of TextBox) From
            {
                New TextBox With {.Name = "TextBox1", .Text = "2"},
                New TextBox With {.Name = "TextBox2", .Text = "12"},
                New TextBox With {.Name = "TextBox3"}
            }
    
        Dim total = Array.ConvertAll(
            tbList.Select(Function(tb) tb.Text).ToArray,
            Function(s)
                Dim x As Double = 0
                If Double.TryParse(s, x) Then
                    Return x
                Else
                    Return 0
                End If
            End Function).Sum
    
        Console.WriteLine(total)
    End Sub


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, June 20, 2018 6:53 PM
    Moderator
  • hi! so, Ieshay, i thought for this case too, but, my empty textboxes i filled with 0, so no need to this function, and even so, is not working D:

    Hi

    Even if you put 0 into the TextBoxes, you can never be sure what a user will put in them.

    What do you mean 'is not working'?  I assure you the code I posted does work OK.


    Regards Les, Livingston, Scotland


    • Edited by leshay Wednesday, June 20, 2018 6:56 PM
    Wednesday, June 20, 2018 6:55 PM
  • the textbox is for readonly... he can not change '-', will stay 0, see? my code should be working... D:
    Wednesday, June 20, 2018 7:44 PM
  • the textbox is for readonly... he can not change '-', will stay 0, see? my code should be working... D:

    Hi

    OK then, I suggest you use breakpoints to examine the various values you are getting in your variables and see if they are what you expect.


    Regards Les, Livingston, Scotland

    Wednesday, June 20, 2018 7:55 PM
  • the values on the textboxes r appearing, i have 10 textboxes and i make the math and filled those 10, but, even so the add dont works D:
    Wednesday, June 20, 2018 10:06 PM
  • Hi

    Show ALL your code please. (use a code block for the code)


    Regards Les, Livingston, Scotland

    Wednesday, June 20, 2018 10:09 PM
  • the values on the textboxes r appearing, i have 10 textboxes and i make the math and filled those 10, but, even so the add dont works D:
    What do you mean 'don't works'?    Show the whole code, show the values in the textboxes, show the result you expect to get, and show the result you are actually getting.
    Wednesday, June 20, 2018 10:18 PM
  • when the textboxes r filled with the values calculated, the textboxtotal, that's supposed to be the one that add the values of the others, stay the same. This is the code that sends the values to the boxes:

    Private Sub CALCULAR(sender As Object, e As EventArgs) Handles CALCULAR1.Click


            'Concentration time calculation from button1
            Precip24 = P24.Text
            Compr1 = L1.Text
            Decliv1 = S1.Text
            Manning1 = N1.Text
            Operacao1 = 5.474 * ((Manning1 * Compr1) ^ 0.8) / (Precip24 ^ 0.5 * Decliv1 ^ 0.4)

            'textbox receives the value of operacao1

            'next time that someone press the button, the value of operacao1 goes to tc2.text and so on

            count += 1
            If count >= 11 Then MsgBox("O Limite foi alcançado")
            CType((Resultados.Controls("tc" & count.ToString())), TextBox).Text = Operacao1


            If MsgBox("Cálculo realizado com sucesso") Then
                P24.Text = 0
                L1.Text = 0
                S1.Text = 0
                N1.Text = 0
            End If

        End Sub

    it's working just fine, the values r filling correctly, but the add that isn't working.

    Wednesday, June 20, 2018 10:31 PM
  • Hi

    Well, you choose not to understand that strings cannot be used in math calculations. If you use the debugging tools to check the various values I suspect you would find the issues.

    You didn't want to use the example function I posted which would have helped you.

    The very first thing you should do is to put Option Strict On and Option Explicit On at the very top of your code - then fix all the issues highlighted.


    Regards Les, Livingston, Scotland

    Wednesday, June 20, 2018 10:47 PM
  • it's working just fine, the values r filling correctly, but the add that isn't working.

    That code will throw an error at the line
            CType((Resultados.Controls("tc" & count.ToString())), TextBox).Text = Operacao1

    You should not be using code like that.  Which text box is the total text box, and what is the code that puts the value in that total text box?

    You should not be using strings (Manning1, Compr1, Precip24, Decliv1) in a mathematical expression.

    Wednesday, June 20, 2018 11:25 PM
  • they'r not strings... i put then as Double, and the code is working good, i just wanna i way that i can utilize Operacao1 and Operacao2 at the sime, if one is not calculated and the other yes... then take the value of the one that was calculated.
    Thursday, June 21, 2018 12:30 AM
  • they'r not strings... i put then as Double, and the code is working good, i just wanna i way that i can utilize Operacao1 and Operacao2 at the sime, if one is not calculated and the other yes... then take the value of the one that was calculated.

    Hi

    You have been given all the advice you need to solve your issues.

    I bet you haven't even tried any of the advice!

    I bet you didn't even try out the code I posted!

    I bet you haven't taken the advice about Option Strict ....!

    I bet your stubbornness gets you nowhere!


    Regards Les, Livingston, Scotland

    Thursday, June 21, 2018 12:47 AM
  • they'r not strings... i put then as Double, and the code is working good
    The Text property of a TextBox is a String.  Therefore you are using strings.  It will create problems. Fix it before trying to deal with your other issues, because it just complicates the whole process of trying to get the logic right.
    Thursday, June 21, 2018 1:23 AM
  • Hi Gustavo,

    Did you mean that when you entered the second time, did the value not be passed to the next text box? I think it is okay. If this is the problem, please take a look at my code.

    Public Class Form1
        Dim Precip24 As Double
        Dim Compr1 As Double
        Dim Decliv1 As Double
        Dim Manning1 As Double
        Dim Operacao1 As Double
        Dim count As Integer = 11
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        End Sub
        Private Sub CALCULAR(sender As Object, e As EventArgs) Handles Button1.Click
            Precip24 = CDbl(p24.Text)
            Compr1 = CDbl(L1.Text)
            Decliv1 = CDbl(S1.Text)
            Manning1 = CDbl(N1.Text)
            count += 1
            Operacao1 = 5.474 * ((Manning1 * Compr1) ^ 0.8) / (Precip24 ^ 0.5 * Decliv1 ^ 0.4)
            If count >= 11 Then MsgBox("O Limite foi alcançado")
            CType((Me.Controls("textbox" & count.ToString())), TextBox).Text = Operacao1
            If MsgBox("Cálculo realizado com sucesso") Then
                P24.Text = 0
                L1.Text = 0
                S1.Text = 0
                N1.Text = 0
            End If
        End Sub
    End Class

    Best regards,
    Alex Li

    Thursday, June 21, 2018 2:55 AM
  • Hi Gustavo,

    Did you mean that when you entered the second time, did the value not be passed to the next text box? I think it is okay. If this is the problem, please take a look at my code.

    Public Class Form1
        Dim Precip24 As Double
        Dim Compr1 As Double
        Dim Decliv1 As Double
        Dim Manning1 As Double
        Dim Operacao1 As Double
        Dim count As Integer = 11
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        End Sub
        Private Sub CALCULAR(sender As Object, e As EventArgs) Handles Button1.Click
            Precip24 = CDbl(p24.Text)
            Compr1 = CDbl(L1.Text)
            Decliv1 = CDbl(S1.Text)
            Manning1 = CDbl(N1.Text)
            count += 1
            Operacao1 = 5.474 * ((Manning1 * Compr1) ^ 0.8) / (Precip24 ^ 0.5 * Decliv1 ^ 0.4)
            If count >= 11 Then MsgBox("O Limite foi alcançado")
            CType((Me.Controls("textbox" & count.ToString())), TextBox).Text = Operacao1
            If MsgBox("Cálculo realizado com sucesso") Then
                P24.Text = 0
                L1.Text = 0
                S1.Text = 0
                N1.Text = 0
            End If
        End Sub
    End Class

    Best regards,
    Alex Li

    Hi

    You have also decided to do math calculations using Strings!


    Regards Les, Livingston, Scotland

    Thursday, June 21, 2018 3:20 AM
  • dude... my code is working fine, i tried your function Regards Les, i told up there that doesnt work with me, saying again, i dont need to change the code that i already have, because is doing exactly what i want. And i already got it, :D so, ty for your "effort".
    • Marked as answer by Gustavo Nikov Sunday, June 24, 2018 7:49 PM
    Thursday, June 21, 2018 6:04 AM