(WinRT) 如何获取 TextBox 插入符号索引? [英] (WinRT)How to get TextBox caret index?

查看:20
本文介绍了(WinRT) 如何获取 TextBox 插入符号索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我在 Windows Store App(WP 8.1)中获取 TextBox 的插入索引时遇到了一些问题.
我需要在按下按钮时在文本中插入特定符号.我试过这个:


I got some problems with getting caret index of TextBox in Windows Store App(WP 8.1).
I need to insert specific symbols to the text when button is pressed. I tried this:

    text.Text = text.Text.Insert(text.SelectionStart, "~");



但是这段代码将符号插入到文本的开头,而不是插入符号所在的位置.



But this code inserts symbol to the beginning of text, not to the place where caret is.

感谢 Ladi,我更新了我的代码.但现在我遇到了另一个问题:我正在构建 HMTL 编辑器应用程序,所以我的默认 TextBlock.Text 是:

I updated my code thanks to Ladi. But now I got another problem: I'm building HMTL editor app so my default TextBlock.Text is:

    <!DOCTYPE html>\r\n<html>\r\n<head>\r\n</head>\r\n<body>\r\n</body>\r\n</html>


例如,当用户在第 3 行插入符号时,符号在插入符号前插入 2 个符号;当插入符号在第 4 行时,前面有 3 个符号,依此类推.当符号插入到第一行时,插入工作正常.

这是我的插入代码:


So for example when user inserts symbol to line 3, symbol is inserted 2 symbols before caret; 3 syms before when caret is in line 4 and so on. Inserting works properly when symbol is inserted to the first line.

Here's my inserting code:

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);


那么如何解决呢?
我猜换行符有问题.


So how to solve this?
I guess new line chars making trouble.

推荐答案

对于您的第一个问题,我假设您在访问 SelectionStart 之前更改了 TextBox.Text.当您设置 text.Text 时,text.SelectionStart 会重置为 0.

For your first issue I assume you changed the TextBox.Text before accessing SelectionStart. When you set the text.Text, text.SelectionStart is reset to 0.

关于您的第二个问题与新线路有关.

Regarding your second issue related to new lines.

您可以说您所观察到的是有意为之.由于这里(见备注部分).另一方面,方法 string.Insert 不关心这方面,并将\r\n"视为两个字符.

You could say that what you observe is by design. SelectionStart will count one "\r\n" as one character for reasons explained here (see Remarks section). On the other hand, method string.Insert does not care about that aspect and counts "\r\n" as two characters.

您需要稍微更改您的代码.您不能使用 SelectionStart 的值作为插入位置.您需要为 SelectionStart 的这种行为计算插入位置.

You need to change slightly your code. You cannot use the value of SelectionStart as the insert position. You need to calculate the insert position accounting for this behavior of SelectionStart.

这是一个带有潜在解决方案的详细代码示例.

Here is a verbose code sample with a potential solution.

// 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"之外的其他组合类似的行为.如果是,您需要更新上面的代码.

Also you should make sure that SelectionStart does not exhibit similar behavior with other combinations beside "\r\n". If it does you will need to update the code above.

这篇关于(WinRT) 如何获取 TextBox 插入符号索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆