|
-
Mar 30th, 2006, 07:21 PM
#1
Thread Starter
Fanatic Member
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.
-
Mar 30th, 2006, 07:45 PM
#2
Re: character column and row
What do you mean by column? A textbox has lines, but columns?
-
Mar 30th, 2006, 07:55 PM
#3
Thread Starter
Fanatic Member
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?
-
Mar 30th, 2006, 08:26 PM
#4
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.
-
Mar 30th, 2006, 08:37 PM
#5
Thread Starter
Fanatic Member
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
-
Mar 30th, 2006, 09:04 PM
#6
Thread Starter
Fanatic Member
Re: Selected character Column and Row position [UNRESOLVED]
got it!
thx!!
VB Code:
rtfText.GetLineFromCharIndex(rtfText.SelectionStart)
-
Mar 30th, 2006, 09:06 PM
#7
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:
Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" _
(ByVal hwnd As Integer, _
ByVal wMsg As Integer, _
ByVal wParam As Integer, _
ByVal lParam As Integer) As Integer
Private Const EM_LINEFROMCHAR As Integer = &HC9
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim intLine As Integer = SendMessage(TextBox1.Handle.ToInt32, EM_LINEFROMCHAR, -1&, 0&)
Dim intColumn As Integer
If intLine = 0 Then
intColumn = TextBox1.SelectionStart
Else
'I used + 2 because vbNewLine length = 2..
intColumn = TextBox1.SelectionStart - _
(TextBox1.Text.Substring(0, TextBox1.SelectionStart).LastIndexOf(vbNewLine) + 2)
End If
MsgBox("Line = " & CStr(intLine) & ", Column = " & CStr(intColumn))
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.
-
Mar 30th, 2006, 09:09 PM
#8
Re: Selected character Column and Row position [UNRESOLVED]
 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:
Dim rowIndex As Integer = 0
Dim colIndex As Integer = Me.TextBox1.SelectionStart
For Each line As String In Me.TextBox1.Lines
'Check whether the caret is in the current line.
If colIndex <= line.Length Then
'The caret is not in the current line.
Exit For
Else
rowIndex += 1
'Subtract the length of the current line including the line break.
colIndex -= (line.Length + 2)
End If
Next line
MessageBox.Show(String.Format("Row: {0}, Column: {1}", rowIndex + 1, colIndex + 1))
-
Mar 30th, 2006, 09:12 PM
#9
Re: Selected character Column and Row position [UNRESOLVED]
 Originally Posted by Ruku
got it!
thx!!
VB Code:
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.
-
Mar 30th, 2006, 09:16 PM
#10
Re: Selected character Column and Row position [UNRESOLVED]
 Originally Posted by jmcilhinney
I don't think you understand what I mean. Let me be explicit.
VB Code:
Dim rowIndex As Integer = 0
Dim colIndex As Integer = Me.TextBox1.SelectionStart
For Each line As String In Me.TextBox1.Lines
'Check whether the caret is in the current line.
If colIndex <= line.Length Then
'The caret is not in the current line.
Exit For
Else
rowIndex += 1
'Subtract the length of the current line including the line break.
colIndex -= (line.Length + 2)
End If
Next line
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!
-
Mar 30th, 2006, 09:29 PM
#11
Thread Starter
Fanatic Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|