-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'。 –