C# 使文本框中的一组字符表现得像一个字符 [英] C# Make a group of characters in a textbox behave like one character

查看:23
本文介绍了C# 使文本框中的一组字符表现得像一个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我在文本框中有像 sin(cos() 这样的关键字,我希望它们的行为像单个字符.

Basically, I have keywords like sin( and cos( in a textbox, which I want to have behave like a single character.

当我在下面提到整个字符串时,它指的是字符组(例如sin(")

When I mention the entire string below it is referring to the group of characters (for example "sin(")

sin(为例:

如果插入符号在这个位置(在 s 后面):

If the caret was in this position (behind the s):

如果你按下del,它会删除整个字符串.如果按下右箭头键,插入符号将跳转到 (.

If you pressed del, it would remove the entire string. If the right arrow key was pressed the caret would jump to after the (.

如果插入符号在这个位置(在 () 之后:

If the caret was in this position (after the ():

如果按下 backspace,整个字符串将被删除.如果按下左箭头键,插入符号会跳到 s 后面.

If backspace was pressed, the entire string would be removed. If the left arrow key was pressed, the caret would jump to behind the s.

感谢 John Skeet 指出这一点.

Thanks to John Skeet who pointed this out.

选择字符组的任何子串(例如 si from sin() 应该选择整个字符串.

Selecting any substring of the group of characters (for example si from sin() should select the entire string.

对不起,如果理解困难,我的想法有点难以用语言表达.

Sorry if it is difficult to understand, what I have in mind is a bit difficult to put into words.

编辑 2: 感谢 Darksheao 为我提供退格键和删除键的答案.我将 delete 段重新定位到 PreviewKeyDown 事件,因为它不适用于 KeyPress 事件.

EDIT 2: Thanks to Darksheao for providing me the answer for the backspace and delete keys. I relocated the delete segment to the PreviewKeyDown event, as it does not work with the KeyPress Event.

编辑 3: 使用 charCombinations 做事方式,这里是我如何实现左右键:

EDIT 3: Using the charCombinations way of doing things, here is how I did the implementation for the left and right keys:

#region Right
case Keys.Right:
{
    s = txt.Substring(caretLocation);
    foreach (string combo in charCombinations)
    {
       if (s.StartsWith(combo))
       {
            textBox1.SelectionStart = caretLocation + combo.Length - 1;
            break;
        }
    }
    break;
}
#endregion
#region Left
case Keys.Left:
    {
        s = txt.Substring(0, caretLocation);
        foreach (string combo in charCombinations)
        {
            if (s.EndsWith(combo))
            {
                textBox1.SelectionStart = caretLocation - combo.Length + 1;
                break;
            }
        }
        break;
    }
#endregion

剩下的就是鼠标实现.任何人?我还意识到用户可能会使用鼠标将插入符号放在这些字符串之一的中间,因此当发生这种情况时,需要将鼠标移动到字符串的开头.

All that remains is the mouse implementation. Anyone? I also realised that the user may use the mouse to place the caret in the middle of one of these strings, so when that happens the mouse needs to be moved to the start of the string.

推荐答案

这是一段代码,用于设置您希望被视为单个字符"的字符组合数组,并设置一个看起来像为他们.

Here is a section of code that sets up an array of the character combinations that you want to be seen as "single characters" and sets up a handler that looks for them.

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    string[] charCombinations = new string[2];
    charCombinations[0] = "sin(";
    charCombinations[1] = "cos(";
    string txt = this.textBox1.Text;
    int caretLocation = this.textBox1.SelectionStart;
    if (e.KeyCode == Keys.Delete)
    {
        //get text in front
        string s = txt.Substring(caretLocation);
        string notChecking = txt.Substring(0, caretLocation);
        foreach (string combo in charCombinations)
        {
            if (s.StartsWith(combo))
            {
                txt = notChecking + s.Substring(combo.Length - 1);
                break;
            }
        }
    }
    if (e.KeyCode == Keys.Back)
    {
        //get text in behind
        string s = txt.Substring(0, caretLocation);
        string notChecking = txt.Substring(caretLocation);
        foreach (string combo in charCombinations)
        {
            if (s.EndsWith(combo))
            {
                txt = s.Substring(0, s.Length - combo.Length + 1) + notChecking;
                caretLocation -= combo.Length - 1;
                break;
            }
        }
    }
    this.textBox1.Text = txt;
    //changing the txt feild will reset caret location
    this.textBox1.SelectionStart = caretLocation;
}

对此进行一些修改,以便您搜索 SelectionStart 将处理 Jon 评论中提到的内容.

Making some modifications to this so that you are searching around the SelectionStart will handle what was mentioned in Jon's comment.

参考:文本框事件参考如何在文本框中找到光标的位置?C#

这篇关于C# 使文本框中的一组字符表现得像一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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