如何做到这一点:我在文本框上按'G',我会看到'A'?
我想你应该处理KeyPress事件。检查按下的按键是否为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”);' – Oliver 2010-09-14 11:45:16
@Oliver任何具体的原因? – 2010-09-14 14:24:34
性能。只需在您的文本框中放入10,000个字符并让计时器每100ms添加一个随机字符,在第一次测试中使用'+ ='方法。 'AppendText()'的解决方案,问题是一个字符串是不可变的,所以整个字符串将从文本框中取出一个单独的字符并且这个整个字符串将被返回给TextBox,这告诉Box扔掉它的全部内容,并采取新的,这将导致令人讨厌的闪烁。 – Oliver 2010-09-15 09:14:57
TextBox t = new TextBox();
t.KeyPress += new KeyPressEventHandler(t_KeyPress);
void t_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 'G')
e.KeyChar = 'A';
}
这更优雅:) - +1 – 2010-09-14 07:09:23
在控制台或形式 – chriszero 2010-09-14 07:05:34