Results 1 to 11 of 11

Thread: Selected character Column and Row position [RESOLVED]

  1. #1

    Thread Starter
    Fanatic Member Ruku's Avatar
    Join Date
    Jul 2002
    Location
    Canada
    Posts
    655

    Resolved Selected character Column and Row position [RESOLVED]

    How do I find the selected text -column- and -row- position of a textbox?

    I could go through all the text everytime I change the place of the cursor, and look for carriage return line feeds, but that would'nt be quite efficient...

    I need to do it quickly, and I'm sure there's a way to do it with a function I don't know... it's just to simple not to exist.

    Last edited by Ruku; Mar 30th, 2006 at 09:04 PM.

    Using VB.NET 2005/.NET 2.0, NetBeans IDE 5, Fujitsu Cobol85,
    Website: http://DreamForgery.com

  2. #2
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: character column and row

    What do you mean by column? A textbox has lines, but columns?

  3. #3

    Thread Starter
    Fanatic Member Ruku's Avatar
    Join Date
    Jul 2002
    Location
    Canada
    Posts
    655

    Re: character column and row

    ok... on a multilined textbox...

    suposed I'd have textbox1.text=

    "asdsadasd
    asdasd
    asdasd
    x Some text here"

    and suppose my cursor is located right before the x right next to "Some text here"...

    x should be on Ln 4, Col 1.

    How can I do this without going through the entire text for carriage returns?

    Using VB.NET 2005/.NET 2.0, NetBeans IDE 5, Fujitsu Cobol85,
    Website: http://DreamForgery.com

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

    Re: Selected character Column and Row position [UNRESOLVED]

    The RichTextBox has some functionality built in to do things like this but the TextBox doesn't. You'll have to do it manually. You can use the Lines property to help you, which is the result of splitting the Text property on line breaks. You can get the index of the caret via the SelectionStart property. You would start counting lines by subtracting then length of the next line from the value of SelectionStart. Once the character index you have is less than the length of the next line you have your line number, and the column number is the charaacter index you are left with. Don't forget to subtract two for the the line break each time as well. Note that this will not take word wrap into account.
    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

  5. #5

    Thread Starter
    Fanatic Member Ruku's Avatar
    Join Date
    Jul 2002
    Location
    Canada
    Posts
    655

    Re: Selected character Column and Row position [UNRESOLVED]

    ya, I figured the column thing, it's efficient...

    but, no, I can't really use the lines... because there is no function that returns the line selected... so I have to go through the entire document for lines... and since I work with huge documents...

    I'll look up the richtextbox... thx


    Using VB.NET 2005/.NET 2.0, NetBeans IDE 5, Fujitsu Cobol85,
    Website: http://DreamForgery.com

  6. #6

    Thread Starter
    Fanatic Member Ruku's Avatar
    Join Date
    Jul 2002
    Location
    Canada
    Posts
    655

    Re: Selected character Column and Row position [UNRESOLVED]

    got it!

    thx!!

    VB Code:
    1. rtfText.GetLineFromCharIndex(rtfText.SelectionStart)

    Using VB.NET 2005/.NET 2.0, NetBeans IDE 5, Fujitsu Cobol85,
    Website: http://DreamForgery.com

  7. #7
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Selected character Column and Row position [UNRESOLVED]

    Well, if anybody is interested, I think i found the way to do it with a common textbox, by using SendMessage.
    I make it start from line = 0 at the top, column 0 at left (it would be real columns just if using a fixed length font), else we should call it text length from beginning of the the caret line to caret position.
    Add a button and press on it to see caret position.
    VB Code:
    1. Private Declare Function SendMessage Lib "user32" _
    2.                     Alias "SendMessageA" _
    3.                     (ByVal hwnd As Integer, _
    4.                     ByVal wMsg As Integer, _
    5.                     ByVal wParam As Integer, _
    6.                     ByVal lParam As Integer) As Integer
    7.  
    8.     Private Const EM_LINEFROMCHAR As Integer = &HC9
    9.  
    10.     Private Sub Button1_Click(ByVal sender As System.Object, _
    11.                         ByVal e As System.EventArgs) Handles Button1.Click
    12.         Dim intLine As Integer = SendMessage(TextBox1.Handle.ToInt32, EM_LINEFROMCHAR, -1&, 0&)
    13.         Dim intColumn As Integer
    14.  
    15.         If intLine = 0 Then
    16.             intColumn = TextBox1.SelectionStart
    17.         Else
    18.             'I used + 2 because vbNewLine length = 2..
    19.             intColumn = TextBox1.SelectionStart - _
    20.                         (TextBox1.Text.Substring(0, TextBox1.SelectionStart).LastIndexOf(vbNewLine) + 2)
    21.         End If
    22.         MsgBox("Line = " & CStr(intLine) & ", Column = " & CStr(intColumn))
    23.     End Sub
    Off course, if you let the text wrap and go to next line without a carriage return, this will think it's the same line.
    Last edited by jcis; Mar 30th, 2006 at 09:10 PM.

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

    Re: Selected character Column and Row position [UNRESOLVED]

    Quote Originally Posted by Ruku
    there is no function that returns the line selected
    I don't think you understand what I mean. Let me be explicit.
    VB Code:
    1. Dim rowIndex As Integer = 0
    2.         Dim colIndex As Integer = Me.TextBox1.SelectionStart
    3.  
    4.         For Each line As String In Me.TextBox1.Lines
    5.             'Check whether the caret is in the current line.
    6.             If colIndex <= line.Length Then
    7.                 'The caret is not in the current line.
    8.                 Exit For
    9.             Else
    10.                 rowIndex += 1
    11.  
    12.                 'Subtract the length of the current line including the line break.
    13.                 colIndex -= (line.Length + 2)
    14.             End If
    15.         Next line
    16.  
    17.         MessageBox.Show(String.Format("Row: {0}, Column: {1}", rowIndex + 1, colIndex + 1))
    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

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

    Re: Selected character Column and Row position [UNRESOLVED]

    Quote Originally Posted by Ruku
    got it!

    thx!!

    VB Code:
    1. rtfText.GetLineFromCharIndex(rtfText.SelectionStart)
    Be aware that that method DOES take into account word wrap. That means that if you have a single long string with no line breaks then you can still get a row index greater than zero. This is quite probably what you want but I thought that it was important to be aware of the distinction between lines in your string and lines in your control.
    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

  10. #10
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Selected character Column and Row position [UNRESOLVED]

    Quote Originally Posted by jmcilhinney
    I don't think you understand what I mean. Let me be explicit.
    VB Code:
    1. Dim rowIndex As Integer = 0
    2.         Dim colIndex As Integer = Me.TextBox1.SelectionStart
    3.  
    4.         For Each line As String In Me.TextBox1.Lines
    5.             'Check whether the caret is in the current line.
    6.             If colIndex <= line.Length Then
    7.                 'The caret is not in the current line.
    8.                 Exit For
    9.             Else
    10.                 rowIndex += 1
    11.  
    12.                 'Subtract the length of the current line including the line break.
    13.                 colIndex -= (line.Length + 2)
    14.             End If
    15.         Next line
    16.  
    17.         MessageBox.Show(String.Format("Row: {0}, Column: {1}", rowIndex + 1, colIndex + 1))
    My god, Jm! No need for API with code like that, much better!

  11. #11

    Thread Starter
    Fanatic Member Ruku's Avatar
    Join Date
    Jul 2002
    Location
    Canada
    Posts
    655

    Re: Selected character Column and Row position [UNRESOLVED]

    neat, I would have gone for every character... heh, jump by line and compare if <... nice going there !

    thx to everyone on this!
    (And I did need wordwrap...)


    Using VB.NET 2005/.NET 2.0, NetBeans IDE 5, Fujitsu Cobol85,
    Website: http://DreamForgery.com

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