944,147 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 3377
  • C# RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 8th, 2009
0

Enable only one whitespace in textbox.

Expand Post »
Can anyone tell me how can I enable one and only one whitespace to be entered in the textbox such that user can press spacebar only once while entering the string in the textbox and not more than once...........

Kindly provide me some solution.............
Similar Threads
Reputation Points: 11
Solved Threads: 12
Junior Poster
vinnijain is offline Offline
145 posts
since Jul 2009
Oct 8th, 2009
1
Re: Enable only one whitespace in textbox.
This snippet http://www.daniweb.com/code/snippet217265.html allows you to enter only one decimal point. It should be easy adaptable to what you want.
Reputation Points: 2035
Solved Threads: 645
Senior Poster
ddanbe is offline Offline
3,740 posts
since Oct 2008
Oct 8th, 2009
0
Re: Enable only one whitespace in textbox.
Click to Expand / Collapse  Quote originally posted by ddanbe ...
This snippet http://www.daniweb.com/code/snippet217265.html allows you to enter only one decimal point. It should be easy adaptable to what you want.
I think you haven't read the post properly.
I am talking about whitespace (enabling spacebar to be pressed once ) not the decimal.
Reputation Points: 11
Solved Threads: 12
Junior Poster
vinnijain is offline Offline
145 posts
since Jul 2009
Oct 8th, 2009
0
Re: Enable only one whitespace in textbox.
This will block a double space, ie: "a<space><space>b" but allow "a<space>b<space>c":
C# Syntax (Toggle Plain Text)
  1. private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
  2. {
  3. TextBox tb = (TextBox)sender;
  4. if ((e.KeyChar == ' ') && (tb.Text.Length > 0))
  5. {
  6. if (tb.Text[tb.Text.Length - 1] == ' ')
  7. e.Handled = true;
  8. }
  9. }

To allow only a single space:
C# Syntax (Toggle Plain Text)
  1. private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
  2. {
  3. TextBox tb = (TextBox)sender;
  4. if ((e.KeyChar == ' ') && (tb.Text.Contains(' ')))
  5. e.Handled = true;
  6. }
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Oct 8th, 2009
0
Re: Enable only one whitespace in textbox.
Thanks a lot...........
It worked.
Reputation Points: 11
Solved Threads: 12
Junior Poster
vinnijain is offline Offline
145 posts
since Jul 2009
Oct 8th, 2009
1
Re: Enable only one whitespace in textbox.
Quote originally posted by vinnijain ...
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
Reputation Points: 2035
Solved Threads: 645
Senior Poster
ddanbe is offline Offline
3,740 posts
since Oct 2008
Oct 8th, 2009
1
Re: Enable only one whitespace in textbox.
Click to Expand / Collapse  Quote originally posted by ddanbe ...
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 So I adapted your snippet to the code I posted
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Oct 12th, 2009
0
Re: Enable only one whitespace in textbox.
I am facing one problem in this code:
Quote ...
This will block a double space, ie: "a<space><space>b" but allow "a<space>b<space>c":
C# Syntax (Toggle Plain Text)
  1. C# Syntax (Toggle Plain Text)
  2.  
  3. private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
  4. {
  5. TextBox tb = (TextBox)sender;
  6. if ((e.KeyChar == ' ') && (tb.Text.Length > 0))
  7. {
  8. if (tb.Text[tb.Text.Length - 1] == ' ')
  9. e.Handled = true;
  10. }
  11. }

Problem is that when I am going backspace or when I am moving backwards using arrowkeys , in that case spacebar is disabled. Not even a single space is enabled.
Kindly provide some solution..........
Last edited by vinnijain; Oct 12th, 2009 at 1:54 am.
Reputation Points: 11
Solved Threads: 12
Junior Poster
vinnijain is offline Offline
145 posts
since Jul 2009
Oct 12th, 2009
0
Re: Enable only one whitespace in textbox.
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?
Reputation Points: 2035
Solved Threads: 645
Senior Poster
ddanbe is offline Offline
3,740 posts
since Oct 2008
Oct 12th, 2009
1
Re: Enable only one whitespace in textbox.
Hello vinnijain.........
try this on textBox1_TextChanged event........
C# Syntax (Toggle Plain Text)
  1. TextBox t = (TextBox)sender;
  2. int index = t.Text.IndexOf(" ");
  3. while (index != -1) {
  4. t.Text = t.Text.Replace(" ", " ");
  5. index = t.Text.IndexOf(" ");
  6. }
  7. t.SelectionStart = t.Text.Length;

Hope it help you...........
Reputation Points: 31
Solved Threads: 36
Posting Whiz
avirag is offline Offline
312 posts
since Jun 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Read a part of a tab delimited data in a Text File :C#
Next Thread in C# Forum Timeline: Client-Server Project





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC