ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
This will block a double space, ie: "ab" but allow "abc":
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
TextBox tb = (TextBox)sender;
if ((e.KeyChar == ' ') && (tb.Text.Length > 0))
{
if (tb.Text[tb.Text.Length - 1] == ' ')
e.Handled = true;
}
}
To allow only a single space:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
TextBox tb = (TextBox)sender;
if ((e.KeyChar == ' ') && (tb.Text.Contains(' ')))
e.Handled = true;
}
sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
I think you haven't read the post properly.
I am talking about whitespace (enabling spacebar to be pressed once ) not the decimal.
I think there is a misunderstanding here between to non native English speaking persons. By whitespace, I understand any character that you don't see on the screen. A tab character is whitespace for instance. You seem to think(correct me if I'm wrong) that hitting the spacebar produces whitespace. Wrong! It produces a space character which is considered whitespace. So I thought my suggestion (by adapting it) would provide you with an answer. But perhaps you did not read my answer to it's full extent;)
ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
I think there is a misunderstanding here between to non native English speaking persons. By whitespace, I understand any character that you don't see on the screen. A tab character is whitespace for instance. You seem to think(correct me if I'm wrong) that hitting the spacebar produces whitespace. Wrong! It produces a space character which is considered whitespace. So I thought my suggestion (by adapting it) would provide you with an answer. But perhaps you did not read my answer to it's full extent;)
I looked at your code snippet to see which event to handle because I knew one of the events still processed the key if you set .handled = true :P So I adapted your snippet to the code I posted
sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
Suggest you revisit the code snippet I gave you.
Change all the code that handles a decimal point into code that handles a space. If you like to input letters instead of numbers more changes are needed, but all this cannot be that hard to accomplish, now is it?
ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661