Results 1 to 7 of 7

Thread: [RESOLVED] How to split a multiline textbox into individual lines

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2013
    Posts
    16

    Resolved [RESOLVED] How to split a multiline textbox into individual lines

    I am working on an assignment where I have to enter data as such:

    First Name Last Name
    Street Address
    City, State Zip
    Home Phone
    Work Phone

    ...In a single text box, so I switched to a multiline text box.
    I want to break the string into lines, error check the lines, and then cast each individual item into a field variable.

    Here is my current code: (with lots of errors)

    Code:
    Option Explicit On
    Option Strict On
    'Acme Info
    Public Class Form1
    
        Private Sub btnConc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConc.Click
            Dim strInfo As String = txtInfo.Text
            Dim strLine1 As String
            Dim strLine2 As String
            Dim strLine3 As String
            Dim strLine4 As String
            Dim strLine5 As String
            Dim strFName As String
            Dim strLName As String
            Dim strAddress As String
            Dim strCity As String
            Dim strState As String
            Dim strZip As String
            Dim strHPhone As String
            Dim strWPhone As String
    
            strLine1 = strInfo.Substring(0, vbNewLine)
            strInfo = strInfo.Remove(0, vbNewLine)
            strLine2 = strInfo.Substring(0, vbNewLine)
            strInfo = strInfo.Remove(0, vbNewLine)
            strLine3 = strInfo.Substring(0, vbNewLine)
            strInfo = strInfo.Remove(0, vbNewLine)
            strLine4 = strInfo.Substring(0, vbNewLine)
            strInfo = strInfo.Remove(0, vbNewLine)
            strLine5 = strInfo.Substring(0, vbNewLine)
            strInfo = strInfo.Remove(0, vbNewLine)
    
    
        End Sub
    End Class
    Any suggestions?

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: How to split a multiline textbox into individual lines

    The multiline textbox has a problem in this case: It doesn't really have multiple lines.

    If the user enters their data in such a way that they get carriage returns, then all is well. You could actually Split on the newline and do all that separation in a single step, but it will work either way (oddly enough, dday just started a thread that has code that does exactly that, so you could look at it if you wanted). However, if the textbox isn't wide enough for any particular line, then the text is going to wordwrap. That's still ok with your code, unless the user sees it wrap and doesn't add in the carriage return. When it wraps in that fashion, it will have multiple lines, but you can't determine where one ends and the next begins. You really HAVE to ensure that the user always ends a line with a carriage return, which is hard to do (cause the users are difficult). As long as the textbox is quite wide, such that nobody will enter anything that will wrap under normal circumstances, then all should be well.
    My usual boring signature: Nothing

  3. #3
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: How to split a multiline textbox into individual lines

    The textbox control already has the "Lines" property, which returns a string array whose elements are individual lines in the control, so no splitting needed. However, as SH pointed out, using a single textbox for full contact info is not a very good idea. Save yourself some headache and use multiple textboxes.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Feb 2013
    Posts
    16

    Re: How to split a multiline textbox into individual lines

    The code is not working though, it freezes up the program when I try to run it, is there a way to end a line using the carriage return?

  5. #5
    Member MrCrow's Avatar
    Join Date
    Feb 2013
    Posts
    41

    Lightbulb Re: How to split a multiline textbox into individual lines

    mstevens33,

    Take out the multiline textbox and use a listbox instead. Then, add the necessary textboxes and this code should work for your purposes:

    Code:
    Private Sub btnConc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConc.Click
            Dim name As String = txtName.Text
            Dim street As String = txtStreet.Text
            Dim city_st_zip As String = txtCityStZip.Text
            Dim home_ph As String = txtHomePh.Text
            Dim work_ph As String = txtWorkPh.Text
            lstOutput.Items.Clear()
            lstOutput.Items.Add(name)
            lstOutput.Items.Add(street)
            lstOutput.Items.Add(city_st_zip)
            lstOutput.Items.Add(home_ph)
            lstOutput.Items.Add(work_ph)
    End Sub

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Feb 2013
    Posts
    16

    Re: How to split a multiline textbox into individual lines

    This is for a class assignment, and the teacher is requiring a single text box. However, I did get it to work using a split command, and setting up an array.

    Code:
    Option Explicit On
    Option Strict On
    'Acme Info
    Public Class Form1
    
        Private Sub btnConc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConc.Click
            Dim strInfo As String
            Dim aryInfo() As String
            Dim I As Integer
            Dim strFName As String
            Dim strLName As String
            Dim strAddress As String
            Dim strCity As String
            Dim strState As String
            Dim strZip As String
            Dim strHPhone As String
            Dim strWPhone As String
    
            strInfo = txtInfo.Text
    
            aryInfo = strInfo.Split(CChar(vbCrLf))
    That code works perfectly for the project.
    Thanks for all the suggestions, one last question though, does anyone know how to test a string to see if it contains any numbers?

  7. #7
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: How to split a multiline textbox into individual lines

    String.IndexOf allows you to search for a string within a string so this is one method of course you would have to loop it from 0 to 9 to see if it contains any numbers.
    IsNumeric() will test a string to see if that string is a numeric value

    TryParse will also try to convert a string to a number and will let you know if it is not a valid number for the data type you are trying to parse.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width