alvo alvo - 6 months ago 19
Vb.net Question

Finding the sum of characters in a text box

I have a text box with characters got from a calculation in my code . the textbox specifically contains only integers... Is there a way I can sum up the integers in the text box? Eg. If my textbox has 123456, the code should find the sum of 1+2+3+4+5+6 and then display the sum in another text box. Thank you in advance

Answer Source

VB6

Public Sub Calculate()
    Dim i       As Integer
    Dim sum     As Integer
    Dim length  As Integer
    i = 1
    sum = 0
    length = Len(TextBox1.Text)

    While i <= length
        sum = sum + Mid(TextBox1.Text, i, 1)
        i = i + 1
    Wend

    TextBox2.Text = sum
End Sub