Line Information / Text Field Number in Silverlight

Scope: Text Box in Silverlight

Question: I need to know what the edited line number is.

I tried: As a workaround, I tried to split using textBox.Split ("\ r") and counting matches in Regex with similar performance. Performance in the first 2000 lines is acceptable, but then it slows down.

Why: I have a text box and a list side by side. The index of the item in the list corresponds to the line number in the text field, and the content (ListboxItem) is the "processed" version of the corresponding line in the text field.

Alternative: A more effective strategy than my hacks.

+3
source share
2 answers

As I see, you do not need to use string.Splitor Regex. Just iterate over the line and count '\r'to the caret position.

var s = ...the string...
var r = 0;
var c = ...caret position...

for (var i = 0; i < c; i++)
  if (s[i] == '\r')
    r++;

Thus, you will find the line number without creating a large number of objects in memory ...

+2
source

All Articles