En supposant WinForms ...
Voici un exemple rapide:
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();
}
}
}
Merci pour votre collaboration. Cela a fonctionné presque bien. Pour atteindre le point dont j'ai besoin, je ne pouvais pas changer les lettres majuscules en lettres minuscules et ainsi de suite ... J'ai difiné tous les remplacements comme suit: remplacements.Add ("cx", "ĉ"); remplacements.Add ("Cx", "Ĉ"); remplacements.Add ("cX", "ĉ"); remplacements.Ajouter ("CX", "Ĉ"); remplacements.Ajouter ("gx", "ĝ"); remplacements.Ajouter ("Gx", "Ĝ"); remplacements.Ajouter ("gX", "ĝ"); remplacements.Ajouter ("GX", "Ĝ"); –
Existe-t-il un moyen facile de geler l'activité de richTextBox_TextChanged puisque je veux reconvertir tout le contenu en 'cx', CX, etc ... à nouveau. –
Pour le problème des majuscules, remplacez 'RichTextBoxFinds.None' par' RichTextBoxFinds.MatchCase'. –