我想知道我怎麼能得到這個詞是當前光標是在WPF的RichTextBox。我知道RichTextBox具有選擇屬性。但是,這隻會給我在RichTextBox中突出顯示的文本。相反,我想知道即使整個單詞沒有突出顯示,光標所在的單詞也是如此。
任何提示將不勝感激。
非常感謝。方式來獲得這個詞光標上,在WPF RichTextBox控件
4
A
回答
1
您可以通過CaretPosition
獲取光標的當前位置。
不幸的是,沒有簡單的方法將字符拖到插入位置的左側/右側。我知道從RichTextBox中獲取文本的唯一方法是this answer,這有點複雜。但它會完成必要的。
2
好吧,爲了解決這個問題,我蠻橫強迫它。
我用 curCaret.GetTextInRun(LogicalDirection.Backward)
和 curCaret.GetTextInRun(LogicalDirection.Forward)
與preCaretString.LastIndexOf(" ")
和postCaretString.IndexOf(" ")
加上其他隔離分開的字一起,得到了子。
最後我添加了字符串的前半部分和字符串的後半部分以獲取當前的光標字。
我打賭有這樣的聰明的方式,但至少在這個問題解決了
2
附加此功能,任意RichTextBox的,現在被稱爲testRTB,並查看結果輸出窗口:
private void testRTB_MouseUp(object sender, MouseButtonEventArgs e)
{
TextPointer start = testRTB.CaretPosition; // this is the variable we will advance to the left until a non-letter character is found
TextPointer end = testRTB.CaretPosition; // this is the variable we will advance to the right until a non-letter character is found
String stringBeforeCaret = start.GetTextInRun(LogicalDirection.Backward); // extract the text in the current run from the caret to the left
String stringAfterCaret = start.GetTextInRun(LogicalDirection.Forward); // extract the text in the current run from the caret to the left
Int32 countToMoveLeft = 0; // we record how many positions we move to the left until a non-letter character is found
Int32 countToMoveRight = 0; // we record how many positions we move to the right until a non-letter character is found
for (Int32 i = stringBeforeCaret.Length - 1; i >= 0; --i)
{
// if the character at the location CaretPosition-LeftOffset is a letter, we move more to the left
if (Char.IsLetter(stringBeforeCaret[i]))
++countToMoveLeft;
else break; // otherwise we have found the beginning of the word
}
for (Int32 i = 0; i < stringAfterCaret.Length; ++i)
{
// if the character at the location CaretPosition+RightOffset is a letter, we move more to the right
if (Char.IsLetter(stringAfterCaret[i]))
++countToMoveRight;
else break; // otherwise we have found the end of the word
}
start = start.GetPositionAtOffset(-countToMoveLeft); // modify the start pointer by the offset we have calculated
end = end.GetPositionAtOffset(countToMoveRight); // modify the end pointer by the offset we have calculated
// extract the text between those two pointers
TextRange r = new TextRange(start, end);
String text = r.Text;
// check the result
System.Diagnostics.Debug.WriteLine("[" + text + "]");
}
將Char.IsLetter(...)更改爲Char.IsLetterOrDigit(...)或其他適當的選項,具體取決於是否希望保留數字。
提示:將其解壓縮到獨立程序集中的擴展方法中,以便在需要時訪問它。
0
由於單詞被空格分開,所以您可以遍歷插入符號周圍的遍歷,直到找到空格。即使您的RichTextBox
甚至包含不同的字體和字體大小,此功能也可以正常工作。
public string GetWordByCaret(LogicalDirection direction)
{
// Get the CaretPosition
TextPointer position = this.CaretPosition;
TextPointerContext context = position.GetPointerContext(direction);
string text = string.Empty;
// Iterate through the RichTextBox based on the Start, Text and End of nearby inlines
while (context != TextPointerContext.None)
{
// We are only interested in the text here
//, so ignore everything that is not text
if (context == TextPointerContext.Text)
{
string current = position.GetTextInRun(direction);
// The strings appended based on whether they are before the caret or after it...
// And well...I love switches :)
switch (direction)
{
case LogicalDirection.Backward:
{
int spaceIndex = current.LastIndexOf(' ');
// If space is found, we've reached the end
if (spaceIndex >= 0)
{
int length = current.Length - 1;
if (spaceIndex + 1 <= length)
{
text = current.Substring(spaceIndex + 1, length - spaceIndex) + text;
}
return text;
}
else
text = current + text;
}
break;
default:
{
int spaceIndex = current.IndexOf(' ');
// If space is found, we've reached the end
if (spaceIndex >= 0)
{
int length = current.Length;
if (spaceIndex <= length)
{
text += current.Substring(0, spaceIndex);
}
return text;
}
else
text += current;
}
break;
}
}
// Move to the next position
position = position.GetNextContextPosition(direction);
// Get the next context
if (position != null)
context = position.GetPointerContext(direction);
else
context = TextPointerContext.None;
}
return text;
}
現在你可以得到你喜歡這個字的意思了。
string before = GetWordByCaret(LogicalDirection.Backward);
string after = GetWordByCaret(LogicalDirection.Forward);
string word = before + after; // :)
相關問題
- 1. 如何獲得這個光標的列?
- 2. WPF,RichTextBox在獲取光標位置的正確textproperties問題
- 3. WPF中的RichTextBox中的光標位置
- 4. 得到一個WPF RichTextBox的文本上畫一個臥式線
- 5. 如何設置WPF RichTextBox的光標位置上開始
- 6. 隱藏光標wpf WebBrowser控件?
- 7. C#獲取光標線的RichTextBox
- 8. Silverlight中的RichTextBox獲得的光標位置
- 9. JavaScript在光標前得到詞
- 10. WPF拖動光標在默認光標
- 11. 我如何獲得這個div來覆蓋閃光燈控制(jw播放器)?
- 12. 在光標下方鍵入並獲取文本框當前詞
- 13. 格式的RichTextBox在WPF
- 14. 獲取WPF Richtextbox中TextPointer的XY座標
- 15. 獲取這個詞在Windows
- 16. 設置RTF文本WPF RichTextBox控件
- 17. Symfony2.3更好的方式來獲得EntityManager的一個控制器
- 18. Pythonic方式來獲得`package.module.Class`
- 19. 如何獲得Delphi中的光標下的控件?
- 20. WPF RichTextBox - 禁用編輯,但保持光標
- 21. 在這種情況下,如何獲得「while」比光標快?
- 22. 熱得到相對於控件左上角的光標位置?
- 23. 如何在WPF RichTextBox中找到第N個單詞的TextRange?
- 24. WPF TimePicker控件只獲得時間
- 25. WPF移除選擇/鼠標光澤效果在GridView控件
- 26. WPF RichTextBox - 獲取當前行
- 27. 嘗試重置在WPF格式文本框控件的光標位置
- 28. WinForm控件上的光標(c#,WinForm 4.0)
- 29. WPF:替代RichTextBox樣式
- 30. 在QGraphicScene上拖放 - 在控件中心的鼠標光標
非常感謝您的回覆。我非常感謝您指向CaretPosition。但是,正如您所描述的那樣,似乎沒有簡單的方法來確定光標所在的單詞的開頭,因爲Document.ContentStart和Document.ContentEnd指向句子的開始和句子的結束。可能是我需要從TextPointer位置搜索空白空間。 (但後來我會與2字節的單詞拼寫,沒有空間來確定單獨的單詞) – 2012-07-31 04:04:07
我想這個問題已經過了很長一段時間... http://stackoverflow.com/questions/10904614/extract-text -from-脫字符號位置的textarea – 2012-07-31 04:30:28