简体   繁体   English

设置文本框插入符号的最后可能位置

[英]Set last possible position of textbox caret

I have a textbox and a TextChanged event with it that, whenever a user types something, a backslash will automatically be added to the end of the textbox IF there isn't one already. 我有一个textbox和一个TextChanged事件,每当用户输入内容时,如果没有一个反斜杠将自动添加到文本框的末尾。

This works fine except, if the user places the cursor at the end of the textbox AFTER the backslash and types, it moves the backslash to the left, adds the new text, and then another backslash at the end. 这样可以正常工作,除非用户将光标放在反斜杠和类型后的文本框末尾,它会将反斜杠向左移动,添加新文本,然后在结尾处添加另一个反斜杠。

For instance, user enters C and C\\ appears. 例如,用户输入C并出现C\\ Now, if they click after the \\ and type a , C\\a\\ appears, when what I want is just Ca\\ . 现在,如果他们点击\\并键入a ,则出现C\\a\\ ,当我想要的只是Ca\\

So I need a way to set the last position of the cursor in the textbox to right before the backslash, if the user tries to set it after the backslash. 所以我需要一种方法来将文本框中光标的最后位置设置在反斜杠之前,如果用户尝试在反斜杠之后设置它。

Here's the code for the textchanged event: 这是textchanged事件的代码:

private void batchRootFolderText_TextChanged(object sender, EventArgs e)
{
    if (!batchRootFolderText.Text.EndsWith("\\"))
    {
        batchRootFolderText.Text = batchRootFolderText.Text + "\\";
    }
} 
private void batchRootFolderText_TextChanged(object sender, EventArgs e){
    if (!batchRootFolderText.Text.EndsWith("\\")){
        String temp = batchRootFolderText.Text;
        temp = temp.Substring(0, MyString.Length - 1);
        batchRootFolderText.Text = temp + "\\";
    }
} 

Tried cutting the end of the string off then replacing it? 尝试切断弦的末端然后更换它?

You can add a variable that will keep track of where the backslash is and "move" it to the end when required: 您可以添加一个变量来跟踪反斜杠的位置,并在需要时将其“移动”到最后:

int backSlashIndex = -1;

private void batchRootFolderText_TextChanged(object sender, EventArgs e)
{
    if (!batchRootFolderText.Text.EndsWith("\\"))
    {
        if(backSlashIndex != -1)
        {
            var fullText = batchRootFolderText.Text;

            var beforeBackslashText = fullText.Substring(0, backSlashIndex);
            var afterBackslashText = fullText.Substring(backSlashIndex + 1, fullText.Length - beforeBackslashText.Length - 1);

            batchRootFolderText.Text = beforeBackslashText + afterBackslashText;
        }

        batchRootFolderText.Text = batchRootFolderText.Text + "\\";
        backSlashIndex = batchRootFolderText.Text.Length - 1;
    }
}

Remember to add handling for cases when characters are removed by a user or when a user types the backslash character by themselves. 当用户删除字符或用户自行键入反斜杠字符时,请记住添加处理。

You can set the cursor position with the SelectionStart property: https://msdn.microsoft.com/en-us/library/system.windows.controls.textbox.selectionstart(v=vs.110).aspx 您可以使用SelectionStart属性设置光标位置: https//msdn.microsoft.com/en-us/library/system.windows.controls.textbox.selectionstart(v = vs.110).aspx

In the SelectionChanged Event ( https://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.textboxbase.selectionchanged(v=vs.110).aspx ) you can check, if the caret is at the end of the text and if the text ends with a backslash and adjust the position. 在SelectionChanged事件( https://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.textboxbase.selectionchanged(v=vs.110).aspx )中,您可以检查,如果插入符号是在文本的最后,如果文本以反斜杠结束并调整位置。

if (textBox.SelectionStart == textBox.Length - 1 && textBox.Text.EndsWith(@"\")
    textBox.SelectionStart = textBox.Length - 2;

This code should do the work. 这段代码应该做的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM