(WinRT)如何获取文本框插入符号索引?
(WinRT)How to get TextBox caret index?
我在 Windows Store App(WP 8.1) 中获取 TextBox 的插入符号索引时遇到了一些问题。
我需要在按下按钮时向文本插入特定符号。
我试过这个:
text.Text = text.Text.Insert(text.SelectionStart, "~");
但是这段代码将符号插入到文本的开头,而不是插入符号所在的位置。
更新
感谢 Ladi,我更新了我的代码。但现在我遇到了另一个问题:我正在构建 HMTL 编辑器应用程序,所以我的默认 TextBlock.Text 是:
<code><!DOCTYPE html>\r\n<html>\r\n<head>\r\n</head>\r\n<body>\r\n</body>\r\n</html>
因此,例如,当用户将符号插入第 3 行时,符号会在插入符号之前插入 2 个符号;当插入符在第 4 行时,前 3 个符号,依此类推。当符号插入到第一行时,插入工作正常。
这是我的插入代码:
Index = HTMLBox.SelectionStart;
HTMLBox.Text = HTMLBox.Text.Insert(Index, (sender as AppBarButton).Label);
HTMLBox.Focus(Windows.UI.Xaml.FocusState.Keyboard);
HTMLBox.Select(Index+1,0);
那么如何解决呢?
我猜换行符会惹麻烦。
对于您的第一期,我假设您在访问 SelectionStart
之前更改了 TextBox.Text
。当您设置 text.Text
时,text.SelectionStart
将重置为 0。
关于您的第二个与换行相关的问题。
您可以说您观察到的是设计使然。 SelectionStart
将把一个“\r\n”算作一个字符,原因已解释为 here(请参阅备注部分)。另一方面,方法 string.Insert
不关心那个方面,并将“\r\n”计为两个字符。
您需要稍微更改一下代码。您不能使用 SelectionStart
的值作为插入位置。您需要计算插入位置以解释 SelectionStart
.
的这种行为
这是一个带有潜在解决方案的冗长代码示例。
// normalizedText will allow you to separate the text before
// the caret even without knowing how many new line characters you have.
string normalizedText = text.Text.Replace("\r\n", "\n");
string textBeforeCaret = normalizedText.Substring(0, text.SelectionStart);
// Now that you have the text before the caret you can count the new lines.
// that need to be accounted for.
int newLineCount = textBeforeCaret.Count(c => c == '\n');
// Knowing the new lines you can calculate the insert position.
int insertPosition = text.SelectionStart + newLineCount;
text.Text = text.Text.Insert(insertPosition, "~");
此外,您还应该确保 SelectionStart 不会与“\r\n”旁边的其他组合表现出类似的行为。如果是这样,您将需要更新上面的代码。
我在 Windows Store App(WP 8.1) 中获取 TextBox 的插入符号索引时遇到了一些问题。
我需要在按下按钮时向文本插入特定符号。
我试过这个:
text.Text = text.Text.Insert(text.SelectionStart, "~");
但是这段代码将符号插入到文本的开头,而不是插入符号所在的位置。
更新
感谢 Ladi,我更新了我的代码。但现在我遇到了另一个问题:我正在构建 HMTL 编辑器应用程序,所以我的默认 TextBlock.Text 是:
<code><!DOCTYPE html>\r\n<html>\r\n<head>\r\n</head>\r\n<body>\r\n</body>\r\n</html>
因此,例如,当用户将符号插入第 3 行时,符号会在插入符号之前插入 2 个符号;当插入符在第 4 行时,前 3 个符号,依此类推。当符号插入到第一行时,插入工作正常。
这是我的插入代码:
Index = HTMLBox.SelectionStart;
HTMLBox.Text = HTMLBox.Text.Insert(Index, (sender as AppBarButton).Label);
HTMLBox.Focus(Windows.UI.Xaml.FocusState.Keyboard);
HTMLBox.Select(Index+1,0);
那么如何解决呢?
我猜换行符会惹麻烦。
对于您的第一期,我假设您在访问 SelectionStart
之前更改了 TextBox.Text
。当您设置 text.Text
时,text.SelectionStart
将重置为 0。
关于您的第二个与换行相关的问题。
您可以说您观察到的是设计使然。 SelectionStart
将把一个“\r\n”算作一个字符,原因已解释为 here(请参阅备注部分)。另一方面,方法 string.Insert
不关心那个方面,并将“\r\n”计为两个字符。
您需要稍微更改一下代码。您不能使用 SelectionStart
的值作为插入位置。您需要计算插入位置以解释 SelectionStart
.
这是一个带有潜在解决方案的冗长代码示例。
// normalizedText will allow you to separate the text before
// the caret even without knowing how many new line characters you have.
string normalizedText = text.Text.Replace("\r\n", "\n");
string textBeforeCaret = normalizedText.Substring(0, text.SelectionStart);
// Now that you have the text before the caret you can count the new lines.
// that need to be accounted for.
int newLineCount = textBeforeCaret.Count(c => c == '\n');
// Knowing the new lines you can calculate the insert position.
int insertPosition = text.SelectionStart + newLineCount;
text.Text = text.Text.Insert(insertPosition, "~");
此外,您还应该确保 SelectionStart 不会与“\r\n”旁边的其他组合表现出类似的行为。如果是这样,您将需要更新上面的代码。