Results 1 to 11 of 11

Thread: Getting the current/selected line num in a multiline textbox

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2009
    Posts
    15

    Getting the current/selected line num in a multiline textbox

    I'm trying to get the selected line num (or anything which helps me to identify the line) in a multi line texbox.
    The meaning for current or selected line is for the line that the user's characteristic (I found that word on the dictionary) is placed on.

    thanks for helpers =]

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Getting the current/selected line num in a multiline textbox

    You need to understand what a "line" is in a mutiline textbox. A line is defined by the invisible CRLF character (carriage return/line feed) in the string that tells whatever control is displaying the string that there should be a new line there.

    However, in a multiline textbox, when you type, and the text you type is too long for 1 line, and it goes to the next line, this is NOT a new line as far as the string is concerned. No CRLF character is inserted into the string here. You have to actually hit the enter key to go to the next line for a CRLF to be there.

    That being said, a multiline textbox has a .lines property, which is an array of strings, each array element is each line in the string, but again the lines are determined by CRLF, so it likely will not match exactly to what line you would THINK it is based on looking at the actual textbox control.

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Getting the current/selected line num in a multiline textbox

    try this:

    vb Code:
    1. TextBox1.GetLineFromCharIndex(TextBox1.SelectionStart)

  4. #4

    Thread Starter
    New Member
    Join Date
    Jan 2009
    Posts
    15

    Re: Getting the current/selected line num in a multiline textbox

    Quote Originally Posted by kleinma View Post
    You need to understand what a "line" is in a mutiline textbox. A line is defined by the invisible CRLF character (carriage return/line feed) in the string that tells whatever control is displaying the string that there should be a new line there.

    However, in a multiline textbox, when you type, and the text you type is too long for 1 line, and it goes to the next line, this is NOT a new line as far as the string is concerned. No CRLF character is inserted into the string here. You have to actually hit the enter key to go to the next line for a CRLF to be there.

    That being said, a multiline textbox has a .lines property, which is an array of strings, each array element is each line in the string, but again the lines are determined by CRLF, so it likely will not match exactly to what line you would THINK it is based on looking at the actual textbox control.
    Is there any way to choose a line by the way it's showed on the textbox?
    Or maybe there's a way to make the textbox to add a CRLF character after finishing a line?

    thanks for helping

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2009
    Posts
    15

    Re: Getting the current/selected line num in a multiline textbox

    Quote Originally Posted by .paul. View Post
    try this:

    vb Code:
    1. TextBox1.GetLineFromCharIndex(TextBox1.SelectionStart)
    Mmm I don't think it works, maybe you can give me an example for removing the current or selected line?

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Getting the current/selected line num in a multiline textbox

    it gets the line that has the caret in.

    you havent explained exactly what you're trying to do yet?

    for instance:

    vb Code:
    1. msgbox(TextBox1.GetLineFromCharIndex(TextBox1.text.indexof("Hello")))

    would return the line number of the first line that contains "Hello"

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Getting the current/selected line num in a multiline textbox

    to remove a line

    vb Code:
    1. Dim lines As New List(Of String)(TextBox1.Lines)
    2. lines.RemoveAt(lineNumber)
    3. TextBox1.Lines = lines.ToArray

  8. #8

    Thread Starter
    New Member
    Join Date
    Jan 2009
    Posts
    15

    Re: Getting the current/selected line num in a multiline textbox

    Ok.
    First of all I want to find the line which is selected in the textbox (means the characteristic is on it).
    Then, after it was found I can remove, edit or anything else..

    .paul. has posted this way:

    Form1.TextBox1.Lines().GetValue(Form1.TextBox1.GetLineFromCharIndex(TextBox1.SelectionStart))

    which gets the current selected line, but if the line got no text it may be a problem.

    I'm not sure which way is the best, but if there isn't any other way, I will use .paul.'s way.

    thanks for all :P

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Getting the current/selected line num in a multiline textbox

    this will get the currently selected line:

    vb Code:
    1. dim str as string = TextBox1.Lines(TextBox1.GetLineFromCharIndex(TextBox1.SelectionStart))

    to check its not nothing:

    vb Code:
    1. if str.trim <> "" then
    2.     'not an empty line
    3. end if

  10. #10
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Getting the current/selected line num in a multiline textbox

    How do you define what is "selected" in the textbox? Does the user just highlight text with the mouse? Or is it just where the blinking cursor happens to be is what you consider to be selected? Normally in terms of something being selected in a textbox, that means it is text that is highlighted.

    The word "characteristic" that you keep using I think you are using incorrectly... I haven't heard that as any sort of programming term.

  11. #11

    Thread Starter
    New Member
    Join Date
    Jan 2009
    Posts
    15

    Re: Getting the current/selected line num in a multiline textbox

    Quote Originally Posted by kleinma View Post
    How do you define what is "selected" in the textbox? Does the user just highlight text with the mouse? Or is it just where the blinking cursor happens to be is what you consider to be selected? Normally in terms of something being selected in a textbox, that means it is text that is highlighted.

    The word "characteristic" that you keep using I think you are using incorrectly... I haven't heard that as any sort of programming term.
    Hhh I know, the smart dictionary told me that xD
    And yes, for the selected line I mean for the line which the blinking cursor is placed in it.

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