0

当用户输入一个文本框处于焦点的字符时,我根本不希望该字符显示在文本框上,并且我不想使用 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;
4

1 回答 1

1

尝试在 eventargs 上设置 Handled 属性。将 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

于 2015-05-27T08:22:34.547 回答