Results 1 to 4 of 4

Thread: [RESOLVED] (Question) Write in a specific line of a Textbox control

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2013
    Posts
    5

    Resolved [RESOLVED] (Question) Write in a specific line of a Textbox control

    Suppose that i want to add new lines to a text inside a textbox control
    Like this

    Code:
    TextBox2.Lines(13) = "111" & vbNewLine & "222"
    So line 13 change to "111"
    and create a new line (14) with this text "222"
    so the previous line 14 now become line 15.

    Can someone tell me which properties or function can I use to do this?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: (Question) Write in a specific line of a Textbox control

    You use the Lines property, but you don't do it like that. Lines is not "live". You have to get an array from the Lines property, modify it and then assign it back. In this case, because you want to add a new line, I'd suggest using a List(Of String) in between too:
    Code:
    Dim arr = TextBox2.Lines
    Dim lst As New List(Of String)(arr)
    
    lst(13) = "111"
    lst.Insert(14, "222")
    
    arr = lst.ToArray()
    TextBox2.Lines = arr
    That can be condensed, of course:
    Code:
    Dim lst As New List(Of String)(TextBox2.Lines)
    
    lst(13) = "111"
    lst.Insert(14, "222")
    
    TextBox2.Lines = lst.ToArray()

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: (Question) Write in a specific line of a Textbox control

    By the way, to be clear, both arrays and collections are zero-based, so the element or item at index 13 is actually the 14th element or item in the list.

  4. #4

    Thread Starter
    New Member
    Join Date
    Mar 2013
    Posts
    5

    Re: (Question) Write in a specific line of a Textbox control

    Quote Originally Posted by jmcilhinney View Post
    You use the Lines property, but you don't do it like that. Lines is not "live". You have to get an array from the Lines property, modify it and then assign it back. In this case, because you want to add a new line, I'd suggest using a List(Of String) in between too:
    Code:
    Dim arr = TextBox2.Lines
    Dim lst As New List(Of String)(arr)
    
    lst(13) = "111"
    lst.Insert(14, "222")
    
    arr = lst.ToArray()
    TextBox2.Lines = arr
    That can be condensed, of course:
    Code:
    Dim lst As New List(Of String)(TextBox2.Lines)
    
    lst(13) = "111"
    lst.Insert(14, "222")
    
    TextBox2.Lines = lst.ToArray()
    Thanks you, It helped me a lot. Sorry for my bad english and my silly posts.

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