0

テキストボックスの文字を変更するにはどうすればよいですか?

たとえば、ユーザーが「L」と入力すると、「N」と入力されます。私は axshockwavflash を使用していて、フラッシュと c# の間の接続を機能させる必要があるため、フラッシュには他の言語には定義されていない多くの単語があり、それを置き換える必要があります。 . 私は以下のように使用しました:

private void textBox1_TextChanged(object sender, EventArgs e)
{
    textBox1.Text.Replace("ی","ي");
}
4

3 に答える 3

0

マッピングに必要なすべてのエントリを保持する辞書を定義し、次のKeyPressようにイベントを処理できます。

Dictionary<char,char> dict = new Dictionary<char,char>();
//Initialize your dict, this should be done somewhere in your form constructor
dict['L'] = 'N';
dict['A'] = 'B';
//....
//KeyPress event handler for your textBox1
private void textBox1_KeyPress(object sender, KeyPressEventArgs e){
   char c;
   if(dict.TryGetValue(e.KeyChar, out c)) e.KeyChar = c;
}

:ユーザーが不要なテキストを貼り付けないようにする場合は、メッセージをキャッチして、WM_PASTEクリップボードからテキストを取得し、次のように修正したテキストをクリップボードに戻すことができます。

public class NativeTextBox : NativeWindow {
    public Dictionary<char, char> CharMappings;
    public NativeTextBox(Dictionary<char,char> charMappings){
      CharMappings = charMappings;
    }
    protected override void WndProc(ref Message m) {
        if (m.Msg == 0x302){ //WM_PASTE
            string s = Clipboard.GetText();
            foreach (var e in CharMappings){
              s = s.Replace(e.Key, e.Value);
            }                
            Clipboard.SetText(s);
        }
        base.WndProc(ref m);
    }
}
//Then hook it up like this (place in your form constructor)
new NativeTextBox(dict).AssignHandle(textBox1.Handle);
于 2013-11-02T20:49:03.953 に答える