C# c richTextBox动态更改为键入

C# c richTextBox动态更改为键入,c#,selectionchanging,C#,Selectionchanging,我的问题是动态地更改两个打印字母的组合,并将它们转换为新的替换字母。假设我输入cx,我想立即替换为ĉ。有人能给我一个密码吗?谢谢假设WinForms 下面是一个简单的例子: public partial class Form1 : Form { private const int WM_SETREDRAW = 0xB; [System.Runtime.InteropServices.DllImport("user32.dll")] public static exte

我的问题是动态地更改两个打印字母的组合,并将它们转换为新的替换字母。假设我输入cx,我想立即替换为ĉ。有人能给我一个密码吗?谢谢

假设WinForms

下面是一个简单的例子:

public partial class Form1 : Form
{

    private const int WM_SETREDRAW = 0xB;

    [System.Runtime.InteropServices.DllImport("user32.dll")]
    public static extern int SendMessage(IntPtr hWnd, Int32 wMsg, bool wParam, Int32 lParam);

    private Dictionary<string, string> replacements = new Dictionary<string, string>();

    public Form1()
    {
        InitializeComponent();

        replacements.Add("cx", "ĉ");
        replacements.Add("ae", "æ");
        // etc...
    }

    private void richTextBox1_TextChanged(object sender, EventArgs e)
    {
        if (cbAutoReplacements.Checked)
        {
            int prevStart = richTextBox1.SelectionStart;
            int prevLength = richTextBox1.SelectionLength;
            SendMessage(richTextBox1.Handle, WM_SETREDRAW, false, 0);

            int index;
            int start;
            foreach (KeyValuePair<string, string> pair in replacements)
            {
                start = 0;
                index = richTextBox1.Find(pair.Key, start, RichTextBoxFinds.MatchCase);
                while (index != -1)
                {
                    richTextBox1.Select(index, pair.Key.Length);
                    richTextBox1.SelectedText = pair.Value;
                    index = richTextBox1.Find(pair.Key, ++start, RichTextBoxFinds.MatchCase);
                }
            }

            richTextBox1.Select(prevStart, prevLength);
            SendMessage(richTextBox1.Handle, WM_SETREDRAW, true, 0);
            richTextBox1.Invalidate();
        }
    }

}

谢谢你的合作。它几乎工作得很好。为了达到我需要的程度,它不能把大写字母改成小写字母等等。。。我将所有替换定义为:replacements.Addcx,ĉ;replacements.AddCx,Ĉ;替换。AddcX,ĉ;replacements.AddCX,Ĉ;替换。Addgx,ĝ;替换。AddGx,Ĝ;替换。AddgX,ĝ;替换。AddGX,Ĝ;有没有一种简单的方法来冻结richTextBox\u TextChanged的活动,因为我想将所有内容转换回“cx”、cx等。。。再次。对于大写字母问题,请将RichTextBoxFinds.None更改为RichTextBoxFinds.MatchCase。要让用户能够打开/关闭自动替换功能,请在表单中添加一个复选框,并在代码中选中该复选框。太好了。。。差不多完成了。您的解决方案已经足够好了,但仍然缺少一点,因为文本可以键入,但无法编辑而不会导致错误。如果我们忘记在已经打印的字母之间编辑一个字母,可以进行替换,但会在文本中产生一个换行符。