I have a TextBox
control. After pressing space, I want, instead of whitespace, to put "|"
. This is my code:
void TextBox1KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Space)
{
textBox1.AppendText("|");
}
}
Problem is it doesn't write only "|"
, but "| "
: After the pipe char, it puts a space and the cursor moves after that space. I tried to use this method:
textBox1.Text = textBox1.Text.Substring(0, (textBox1.TextLength - 1));
but it doesn't work because it deletes "|"
instead of the space.
And another question is: I want to disable inserting " " when Space is pressed when textbox is empty. When u want to press space while textbox is empty, nothing happens, textbox will stay empty and cursor stay at the begining of textbox
i try this
void TextBox1KeyDown(object sender, KeyEventArgs e)
{if (textBox1.Text.Length==0 && e.KeyCode == Keys.Space) {
textBox1.Text=string.Empty; }
}
It doesnt work cause same reason like my previous question. That event is handled after " " is already added so textbox is not empty
PS: sorry for my english and thanks to whoever edit my post and fix my grammatical and spellig errors
onKeyRelease
– nbro