Reputation: 29792
I'm a little confused with a small pice of code - I created my own solution to the problem asked here - on KeyUp event I perform some check ups and then add a space or remove a character:
private void myTextbox_KeyUp1(object sender, System.Windows.Input.KeyEventArgs e)
{
string text = myTextbox.Text;
if (text.Length > 3)
{
if (e.Key == System.Windows.Input.Key.Back)
{
if (text.Length % 5 == 4) text = text.Substring(0, text.Length - 1);
}
else if ((text.Length - (text.Length / 5)) % 4 == 0) text += " ";
}
myTextbox.Text = text;
myTextbox.SelectionStart = text.Length;
}
The code works just fine. But if I only change the interior if statement to something like this (combine two if statements):
if (e.Key == System.Windows.Input.Key.Back && text.Length % 5 == 4)
text = text.Substring(0, text.Length - 1);
The code stops working when I have more than 10 characters and then try to delete, I'm not able to delete the tenth character (space).
Upvotes: 0
Views: 72
Reputation: 418
I think it is because, by combining the 2 if statements, you go into the else if, thus adding a space
10 - (10/5) % 4
does equal 0, so add a space.
Upvotes: 1