-2
我的问题是动态更改两个键入字母的组合,并将它们转换为新的替换一个字母。假设我输入“cx”,我想立即将替换变为“ĉ”。有人可以给我提供这个代码吗?由于c#richTextBox动态更改为输入
我的问题是动态更改两个键入字母的组合,并将它们转换为新的替换一个字母。假设我输入“cx”,我想立即将替换变为“ĉ”。有人可以给我提供这个代码吗?由于c#richTextBox动态更改为输入
假设的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.Add(“cx”,“ĉ”); replacements.Add(“Cx”,“Ĉ”); replacements.Add(“cX”,“ĉ”); replacements.Add(“CX”,“Ĉ”); replacements.Add(“gx”,“ĝ”); replacements.Add(“Gx”,“Ĝ”); replacements.Add(“gX”,“ĝ”); replacements.Add(“GX”,“Ĝ”); –
是否有一种简单的方法来冻结richTextBox_TextChanged的活动,因为我想将所有内容都转换回'cx',CX等等。 –
对于大写字母问题,请将'RichTextBoxFinds.None'更改为'RichTextBoxFinds.MatchCase'。 –