Results 1 to 4 of 4

Thread: Write text on specific line in a textbox

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2007
    Posts
    27

    Write text on specific line in a textbox

    I am trying to write text on a specific line in a textbox for example on the forth line)

    I've tried :

    TextBox1.Lines = TextBox1.Lines.Length

    but get this error:

    Value of type 'Integer' cannot be converted to '1-dimensional array of String'.

    I assume this is beacause TextBox1.Lines is an array of all the text in every line in the textbox.

    Multiline is set to true.

    How to fix this?

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

    Re: Write text on specific line in a textbox

    vb.net Code:
    1. Dim lines As String() = myTextBox.Lines
    2.  
    3. lines(3) = myString
    4. myTextBox.Lines = lines
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Nov 2007
    Posts
    27

    Re: Write text on specific line in a textbox

    Thanks for the reply,

    I get this error:

    System.IndexOutOfRangeException was unhandled
    Message="Index was outside the bounds of the array."

    The textbox where I am trying to write text to is empty.

    vb.net Code:
    1. Dim lines As String() = TextBox1.Lines
    2.  
    3. lines(0) = "test"
    4.  
    5. TextBox1.Lines = lines

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Write text on specific line in a textbox

    Well, that's kind of important information. How can you write to the fourth line of a TextBox that has no lines in it? There has to be at least four lines for there to be a fourth line.

    If you want to add a new line to a TextBox you can do this:
    vb.net Code:
    1. myTextBox.AppendText(Environment.NewLine)
    You can also assign any String array you like to the Lines property of a TextBox to overwrite all existing text. The array doesn't have to have come from the Lines property in the first place:
    vb.net Code:
    1. Dim lines As String() = {"Line 1", "Line 2", "Line 3"}
    2.  
    3. myTextBox.Lines = lines
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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