|
-
Jul 19th, 2006, 05:19 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] [2005] Challenge: Faster Cursor Position
Hello everyone,
I have a challenge for you:
A RichTextBox (EditorRichTextBox) is on the form with a lot of text in it, over a million characters, and I need to display the position of the cursor on a ToolStripLabel (CursorPositionToolsStripLabel) in the following format: "Line [current line]/[total number of lines], Column [current column]".
I have code that works fine, but it's not quite fast enough, it takes about 125ms to execute with 1 mb of text. I know that's not THAT slow, but it's just not fast enough. For example, when you keep your right arrow key down and move the cursor to the right, the position doesn't update during movement and cursor moves slowly.
The code I use now is this: (I placed it in the SelectionChanged-event)
VB Code:
Dim sText As String = Mid(EditorRichTextBox.Text, 1, _
EditorRichTextBox.SelectionStart)
Dim sLines() As String = Split(sText, vbLf)
Dim sAllLines() As String = Split(EditorRichTextBox.Text, vbLf)
Dim iLine As Integer = UBound(sLines) + 1
Dim iColumn As Integer = Len(sLines(UBound(sLines)))
Dim iLinesTotal As Integer = UBound(sAllLines) + 1
CursorPositionToolStripStatusLabel.Text = "Line " & iLine & "/" & _
iLinesTotal & ", Column " & iColumn
Does anyone know a faster way?
No matter how fool-proof your program is, there will always be a better fool.
Was a post helpful to you? Rate it!
-
Jul 19th, 2006, 06:46 AM
#2
Re: [2005] Challenge: Faster Cursor Position
Split is the slowest thing since a stalled Morris minor. You are completely killing your ram usage with that.
There's no use using split to count the lines when all you have to do is loop through and count the number of vbLF's. This won't use any extra ram and will be infinitely faster.
You can get the total lines and the current cursor line in a single loop, the column position will be the difference between the absolute cursor position and the position of the vbLF just to the left of the cursor.
Easy peasy. 
I'll let you have a go at coding that yourself first.
I don't live here any more.
-
Jul 19th, 2006, 07:22 AM
#3
Thread Starter
Fanatic Member
Re: [2005] Challenge: Faster Cursor Position
I didn't know Split was slow, thanks Wossname. I now have this. It's faster, 90ms, but still a bit too slow.
VB Code:
Dim sText As String = Mid(EditorRichTextBox.Text, 1, _
EditorRichTextBox.SelectionStart)
Dim iLine As Integer = 0
Dim iIndex As Integer = 0
Do
iLine += 1
iIndex = InStr(iIndex + 1, sText, vbLf)
Loop While iIndex > 0
Dim iColumn As Integer = sText.Length - sText.LastIndexOf(vbLf)
Dim iTotalLines = EditorRichTextBox.Lines.Length
CursorPositionToolStripStatusLabel.Text = "Line " & iLine & "/" & _
iTotalLines & ", Column " & iColumn
No matter how fool-proof your program is, there will always be a better fool.
Was a post helpful to you? Rate it!
-
Jul 19th, 2006, 08:56 AM
#4
Re: [2005] Challenge: Faster Cursor Position
You would get the line and column number like this:
VB Code:
Dim currentLineIndex As Integer = myRTB.GetLineFromCharIndex(myRTB.SelectionStart)
Dim lastLineIndex As Integer = myRTB.GetLineFromCharIndex(myRTB.TextLength)
Dim currentColumnIndex As Integer = myRTB.SelectionStart - myRTB.GetFirstCharIndexFromLine(currentLineIndex)
Me.Label1.Text = String.Format("Line {0}/{1}, Column {2}", currentLineIndex + 1, lastLineIndex + 1, currentColumnIndex + 1)
Is it just my imagination or have Microsoft wasted their money creating documentation for VS?
This is bound to be pretty fast because those methods call the SendMessage method of the control class, which directly calls the SendMessage API function.
-
Jul 19th, 2006, 01:21 PM
#5
Re: [2005] Challenge: Faster Cursor Position
Well there you go. Good call JMC. The only reason I didn't look in MSDN was because it was lunchtime and my sandwiches were getting warm(er)
I don't live here any more.
-
Jul 20th, 2006, 02:14 AM
#6
Thread Starter
Fanatic Member
Re: [2005] Challenge: Faster Cursor Position
Thank you Jmcilhinney, that was just what I needed. It now takes around 4ms to get the position, in stead of 90ms.
I did come across those functions of the RichTextBox, but didn't know how to use them. I should indeed have looked that up in MSDN.
Thanks again.
No matter how fool-proof your program is, there will always be a better fool.
Was a post helpful to you? Rate 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|