Ask a questionAsk a question
 

Answersum multiline textbox

  • Monday, October 12, 2009 6:07 PMamorales132 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    hi a have to do a project for my class(so totally a noob in vb2008) and i need to sum the values of all lines in a multi line textbox  and displayy them in a second text box.
    i will really appreciate any help you guys can lend me.

Answers

  • Wednesday, October 14, 2009 3:16 AMMartin Xie - MSFTMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Hi amorales,

    Welcome to MSDN forums!

    Please check the following code sample and try to understand it.

    Prerequisites:
    1. Drag & drop TextBox1, TextBox2 and Button1 onto Form1.
    2. You need to input valid numeric value at each line into multi-line TextBox1.

    Public Class Form1

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            TextBox1.Multiline = True

        End Sub

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            Dim sum As Double

            Dim line As String

            Try

                For Each line In TextBox1.Lines

                    sum = sum + Double.Parse(line.Trim)

                Next

                TextBox2.Text = sum

            Catch ex As Exception

                MessageBox.Show(ex.Message)

            End Try

        End Sub

    End Class




    Best regards,
    Martin Xie


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.

All Replies