2013-04-05 25 views
6

Có một số khá giải pháp tương đương GetPositionAtOffset() chỉ tính vị trí chèn văn bản thay vì tất cả các ký hiệu không?GetPositionAtOffset tương đương cho chỉ văn bản?

Động lực dụ trong C#:

TextRange GetRange(RichTextBox rtb, int startIndex, int length) { 
    TextPointer startPointer = rtb.Document.ContentStart.GetPositionAtOffset(startIndex); 
    TextPointer endPointer = startPointer.GetPositionAtOffset(length); 
    return new TextRange(startPointer, endPointer); 
} 

Edit: Cho đến bây giờ tôi "giải quyết" nó theo cách này

public static TextPointer GetInsertionPositionAtOffset(this TextPointer position, int offset, LogicalDirection direction) 
{ 
    if (!position.IsAtInsertionPosition) position = position.GetNextInsertionPosition(direction); 
    while (offset > 0 && position != null) 
    { 
     position = position.GetNextInsertionPosition(direction); 
     offset--; 
     if (Environment.NewLine.Length == 2 && position != null && position.IsAtLineStartPosition) offset --; 
    } 
    return position; 
} 
+1

Wow ai đó REALLY không muốn RichTextBox được sử dụng .. cảm ơn! – gjvdkamp

Trả lời

2

Theo như tôi biết, không có. Đề nghị của tôi là bạn tạo phương thức GetPositionAtOffset của riêng bạn cho mục đích này. Bạn có thể kiểm tra PointerContext các TextPointer tiếp giáp với bằng cách sử dụng:

TextPointer.GetPointerContext(LogicalDirection); 

Để có được TextPointer sau đó trỏ đến một PointerContext khác nhau:

TextPointer.GetNextContextPosition(LogicalDirection); 

Một số mẫu mã tôi đã sử dụng trong một dự án gần đây, điều này đảm bảo rằng ngữ cảnh con trỏ là loại Văn bản, bằng cách lặp cho đến khi tìm thấy một. Bạn có thể sử dụng điều này trong quá trình triển khai của mình và bỏ qua số gia tăng nếu nó được tìm thấy:

// for a TextPointer start 

while (start.GetPointerContext(LogicalDirection.Forward) 
          != TextPointerContext.Text) 
{ 
    start = start.GetNextContextPosition(LogicalDirection.Forward); 
    if (start == null) return; 
} 

Hy vọng bạn có thể sử dụng thông tin này.

+1

Rất đẹp. Không biết TextPointerContext cho đến bây giờ :) –

+0

Rất vui khi được trợ giúp. Cho tôi biết nếu bạn cần thứ gì khác! Săn bắn tốt. – JessMcintosh

0

Không thể tìm ra giải pháp hiệu quả cho vấn đề này trong một thời gian dài. Đoạn mã tiếp theo hoạt động trong trường hợp của tôi với hiệu suất cao nhất. Hy vọng nó sẽ giúp ai đó là tốt.

TextPointer startPos = rtb.Document.ContentStart.GetPositionAtOffset(searchWordIndex, LogicalDirection.Forward); 
startPos = startPos.CorrectPosition(searchWord, FindDialog.IsCaseSensitive); 
if (startPos != null) 
{ 
    TextPointer endPos = startPos.GetPositionAtOffset(textLength, LogicalDirection.Forward); 
    if (endPos != null) 
    { 
     rtb.Selection.Select(startPos, endPos); 
    } 
} 

public static TextPointer CorrectPosition(this TextPointer position, string word, bool caseSensitive) 
{ 
    TextPointer start = null; 
    while (position != null) 
    { 
     if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text) 
     { 
      string textRun = position.GetTextInRun(LogicalDirection.Forward); 

      int indexInRun = textRun.IndexOf(word, caseSensitive ? StringComparison.InvariantCulture : StringComparison.InvariantCultureIgnoreCase); 
      if (indexInRun >= 0) 
      { 
       start = position.GetPositionAtOffset(indexInRun); 
       break; 
      } 
     } 

     position = position.GetNextContextPosition(LogicalDirection.Forward); 
    } 

    return start; } 
Các vấn đề liên quan