从文本框在Silverlight行信息/号码文本框、号码、信息、Silverlight

2023-09-05 02:57:33 作者:天黑路滑人心杂

区: 文本框在Silverlight

Area : Textbox in Silverlight

问: 我需要知道什么是行数当前编辑的。

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

我已经试过: 作为一种变通方法我试过分裂与textBox.Split(\ r),并计算匹配的正则表达式有类似的性能。在2000线的性能是可以接受的,但随后变得缓慢。

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.

为什么: 我有一个文本框和一个列表框并排侧。该项目索引列表框中对应于文本框和(在ListBoxItem中的)内容的行号是一个加工的版本在文本框中的相应线路。

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.

推荐答案

在我看来,你并不真的需要使用 string.Split 正则表达式。只是遍历字符串和计数'\ r' s提高到插入符位置。

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...