当用户输入一个字符与文本框的焦点,我不希望字符显示在文本框上,我不想使用Clear()方法,因为可能有其他文本在文本框我不想擦除。到目前为止,我已经试过了:
private void WriteBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13) // enter key pressed
{
WriteBox1.Text = "";
}
// Code to write Serial....
String writeValues = WriteBox1.Text;
String withoutLast = writeValues.Substring(0, 1);
WriteBox1.Text = withoutLast;
}
这留下最后一个字母输入在writeBox1。我需要它删除输入的所有字符。我也累了:
writeValues.Replace(writeValues, "");
WriteBox1.Text = writeValues;
尝试在eventargs上设置已处理属性。将Handled设置为true以取消KeyPress事件。这可以防止控件处理按键。
的例子:
private void keypressed(Object o, KeyPressEventArgs e)
{
// The keypressed method uses the KeyChar property to check
// whether the ENTER key is pressed.
// If the ENTER key is pressed, the Handled property is set to true,
// to indicate the event is handled.
if (e.KeyChar == (char)Keys.Return)
{
e.Handled = true;
}
}
https://msdn.microsoft.com/ru-ru/library/system.windows.forms.keypresseventargs.handled%28v=vs.110%29.aspx