Read TextBox line by line

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ravenfrost
    New Member
    • Apr 2007
    • 9

    #1

    Read TextBox line by line

    Hello,

    I have a text box that I am entering items into line by line. I want to read in the data one line at a time into a variable through a loop. I know the textbox does not have a readline attribute, but is there any way I can read a textbox line by line, or should I be using a different control to do so? I'm using VB.Net. Thanks.

    Sean Merron
    Last edited by ravenfrost; May 17 '07, 02:28 PM. Reason: Specifying Language.. VB.NET
  • TRScheel
    Recognized Expert Contributor
    • Apr 2007
    • 638

    #2
    Originally posted by ravenfrost
    Hello,

    I have a text box that I am entering items into line by line. I want to read in the data one line at a time into a variable through a loop. I know the textbox does not have a readline attribute, but is there any way I can read a textbox line by line, or should I be using a different control to do so? I'm using VB.Net. Thanks.

    Sean Merron

    I would assume it to have a Environment.New Line at the end of each line, so you could split by that. I will come back to this thread in like 30 minutes when I get some time to look it up exactly for you.

    Comment

    • TRScheel
      Recognized Expert Contributor
      • Apr 2007
      • 638

      #3
      Originally posted by TRScheel
      I would assume it to have a Environment.New Line at the end of each line, so you could split by that. I will come back to this thread in like 30 minutes when I get some time to look it up exactly for you.

      Ya, doing this:

      Code:
      MyMultiLineTextBox.Text.Split(Environment.NewLine)
      will return an array of strings, one for each line.
      Last edited by TRScheel; May 17 '07, 03:01 PM. Reason: missed the .Text

      Comment

      • ravenfrost
        New Member
        • Apr 2007
        • 9

        #4
        Thanks I'll give it a whirl!

        Comment

        • ravenfrost
          New Member
          • Apr 2007
          • 9

          #5
          Only problem I'm having is that it's keeping the carriage returns within the string. Any way to get rid of those?

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Use .Trim() on each line?
            Code:
            string[] mylines = mytextbox.Split(Environment.Newline);
            for(int i=0;i<mylines.Length;i++)
            {
               string currentstring=mylines[i].Trim();
            }

            Comment

            • ravenfrost
              New Member
              • Apr 2007
              • 9

              #7
              Fore some reason the trim isn't removing the carriage return. The first variable in teh array doesn't ahve it, but all other variables have a carraige return before them.

              Code:
                                  Dim strComputer, arrComputers() As String
              arrComputers = txtEndpoints.Text.Split(Environment.NewLine)
              Dim i As Integer = 0
              For i = LBound(arrComputers) To UBound(arrComputers)
                   strComputer = Trim(arrComputers(i))
                   Call MsgBox(strComputer)
              Next

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                Ah ha!
                There is probably both a \n and a \r and your environment.new line is only removing \r.
                Investigate Trim() and Split() and find out where to handle that.
                OR
                Code:
                string mytext=mytextbox.text;
                mtext.Replace("\r","");
                then do a .Split("\n"); and see where it gets you?

                Comment

                Working...