I have to add line numbering to my application (C++/CLI – .net2.0) which is a code editor for specialized programming language.
The problem is in a performance of my solution. I did it like this, for every textChanged event of my _rtbCode control this function is fired:
void DocumentElement::SetupLineNumbersCount() {
StringBuilder ^builder = gcnew StringBuilder();
if(_tbLineNumbers->Lines->Length > _rtbCode->Lines->Length) {
for(int i = 0; i <= _rtbCode->Lines->Length;) {
if(i != 0) builder->Append("\r\n");
builder->Append(++ i);
builder->Append(".");
}
_tbLineNumbers->Text = builder->ToString();
}
else if(_tbLineNumbers->Lines->Length < _rtbCode->Lines->Length) {
builder->Append(_tbLineNumbers->Text);
for(int i = _tbLineNumbers->Lines->Length; i < _rtbCode->Lines->Length;) {
builder->Append("\r\n");
builder->Append(++ i);
builder->Append(".");
}
_tbLineNumbers->Text = builder->ToString();
}
}
Where _tbLineNumbers is a TextBox where line numbers are put.
This is soooo slow (I’ve compared this with some other code editors). I tried to paste couple lines (around 10000) and I have huge delay of reaction.
(I already checked How to print line numbers for textbox in c#)
Would it be better to update the line number TextBox on Scroll/Resize events of the code TextBox?
This way, you’d only need to care about the visible set of lines, and typing in the code window wouldn’t require a refresh of the line numbers.
GetFirstVisibleLineIndex
GetLastVisibleLineIndex
These may help you out too!