C# 如何操作:我按';G';在文本框和i';我看';A';?

C# 如何操作:我按';G';在文本框和i';我看';A';?,c#,windows-mobile,compact-framework,windows-ce,C#,Windows Mobile,Compact Framework,Windows Ce,如何做到这一点: 当我在表格中的文本框中按'G'时,我会看到'A' 使用C代码(windows CE或windows mobile) 提前谢谢我想你应该处理按键事件。检查按下的键是否为G,如果是,则拒绝输入并在文本框中输入A。尝试此操作(字符将附加到现有文本中: private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { if (e.KeyCha

如何做到这一点:

当我在表格中的文本框中按
'G'
时,我会看到
'A'

使用C代码(windows CE或windows mobile)


提前谢谢

我想你应该处理按键事件。检查按下的键是否为G,如果是,则拒绝输入并在文本框中输入A。尝试此操作(字符将附加到现有文本中:

    private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
    {
        if (e.KeyChar == 'G')
        {
            // Stop the character from being entered into the control
            e.Handled = true;
            textBox1.Text += 'A';
        }
    }

textBox1.Text+='A';
相比,我更喜欢
textBox1.AppendText(“A”);
性能。只需将10000个字符放入文本框,让计时器每100ms添加一个随机字符。在第一个测试中使用
+=
方法。在第二个测试中使用
AppendText()
解决方案。问题是一个字符串是不可变的,因此整个字符串将从文本框中取出,并附加一个字符,然后整个字符串将返回到文本框。这告诉文本框丢弃其全部内容,然后使用新的内容,千,这会导致严重的闪烁。我的方法不是更好吗?
    TextBox t = new TextBox();
    t.KeyPress += new KeyPressEventHandler(t_KeyPress);


    void t_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == 'G')
            e.KeyChar = 'A';
    }