Enable only one whitespace in textbox.

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2009
Posts: 123
Reputation: vinnijain is an unknown quantity at this point 
Solved Threads: 10
vinnijain vinnijain is offline Offline
Junior Poster

Enable only one whitespace in textbox.

 
0
  #1
Oct 8th, 2009
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.............
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,973
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 286
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso
 
1
  #2
Oct 8th, 2009
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.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 123
Reputation: vinnijain is an unknown quantity at this point 
Solved Threads: 10
vinnijain vinnijain is offline Offline
Junior Poster
 
0
  #3
Oct 8th, 2009
Originally Posted by ddanbe View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,254
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 579
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
0
  #4
Oct 8th, 2009
This will block a double space, ie: "a<space><space>b" but allow "a<space>b<space>c":
  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:
  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. }
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 123
Reputation: vinnijain is an unknown quantity at this point 
Solved Threads: 10
vinnijain vinnijain is offline Offline
Junior Poster
 
0
  #5
Oct 8th, 2009
Thanks a lot...........
It worked.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,973
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 286
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso
 
1
  #6
Oct 8th, 2009
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
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,254
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 579
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
1
  #7
Oct 8th, 2009
Originally Posted by ddanbe View Post
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
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 123
Reputation: vinnijain is an unknown quantity at this point 
Solved Threads: 10
vinnijain vinnijain is offline Offline
Junior Poster
 
0
  #8
Oct 12th, 2009
I am facing one problem in this code:
This will block a double space, ie: "a<space><space>b" but allow "a<space>b<space>c":
  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,973
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 286
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso
 
0
  #9
Oct 12th, 2009
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?
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 210
Reputation: avirag is an unknown quantity at this point 
Solved Threads: 15
avirag avirag is offline Offline
Posting Whiz in Training
 
1
  #10
Oct 12th, 2009
Hello vinnijain.........
try this on textBox1_TextChanged event........
  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...........
Reply With Quote Quick reply to this message  
Reply

Tags
textbox

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC