0

When the user enters a character with the TextBox in focus, I do not want the character to show up on the TextBox at all and I don't want to use the Clear() method as there may be other text in the TextBox I don't want erased. so far I've tried:

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;

}

This leaves the last letter entered in writeBox1. I need it to delete all characters entered. I've also tired:

writeValues.Replace(writeValues, "");
WriteBox1.Text = writeValues;
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
james
  • 47
  • 6

1 Answers1

1

Try setting Handled property on eventargs. Set Handled to true to cancel the KeyPress event. This keeps the control from processing the key press.

example :

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

realnero
  • 994
  • 6
  • 12