Saturday, February 19, 2011

Line information/number from Textbox in Silverlight

Hi

Area : Textbox in Silverlight

Question: I need to know what "line number" that is currently edited.

I've tried: As a workaround I've tried splitting with textBox.Split("\r") and counting matches on Regex with similar performance. Performance during the first 2000 lines are acceptable but then it gets to slow.

Why: I have a Textbox and a Listbox side-by-side. The item index in the listbox corresponds to line number in the textbox and the content (of the ListboxItem) is a "processed" version of the corresponding line in textbox.

Alternative: A more performance friendly strategy than my hacks.

From stackoverflow
  • As I see it, you don't really need to use string.Split or Regex. Just iterate over the string and count '\r's up 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++;
    

    This way, you'll find the line number without creating lots and lots of objects in memory...

0 comments:

Post a Comment