954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Enable only one whitespace in textbox.

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.............

vinnijain
Junior Poster
145 posts since Jul 2009
Reputation Points: 11
Solved Threads: 12
 

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.

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 
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.

vinnijain
Junior Poster
145 posts since Jul 2009
Reputation Points: 11
Solved Threads: 12
 

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
 

Thanks a lot...........
It worked.

vinnijain
Junior Poster
145 posts since Jul 2009
Reputation Points: 11
Solved Threads: 12
 
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
 

I am facing one problem in this code:
This will block a double space, ie: "ab" but allow "abc":

C# Syntax (Toggle Plain Text)

      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;
         }
         }


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..........

vinnijain
Junior Poster
145 posts since Jul 2009
Reputation Points: 11
Solved Threads: 12
 

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
 

Hello vinnijain.........
try this on textBox1_TextChanged event........

TextBox t = (TextBox)sender;
            int index = t.Text.IndexOf("  ");
            while (index != -1) {
                t.Text =  t.Text.Replace("  ", " ");
                index = t.Text.IndexOf("  ");
            }
            t.SelectionStart = t.Text.Length;


Hope it help you...........:)

avirag
Posting Whiz
313 posts since Jun 2009
Reputation Points: 31
Solved Threads: 36
 

Hello vinnijain......... try this on textBox1_TextChanged event........

TextBox t = (TextBox)sender;
            int index = t.Text.IndexOf("  ");
            while (index != -1) {
                t.Text =  t.Text.Replace("  ", " ");
                index = t.Text.IndexOf("  ");
            }
            t.SelectionStart = t.Text.Length;

Hope it help you...........:)


Thanks for your reply..........
But when I am using left arrow key and writes something then its taking cursor to the end again and again which is not proper........

vinnijain
Junior Poster
145 posts since Jul 2009
Reputation Points: 11
Solved Threads: 12
 
Thanks for your reply.......... But when I am using left arrow key and writes something then its taking cursor to the end again and again which is not proper........


OKey.......
Well now you just try this......
it will defiantly help you........

TextBox t = (TextBox)sender;
            int index = t.Text.IndexOf("  ");
            int selectionStart = t.SelectionStart;
            
            
            while (index != -1)
            {
                t.Text = t.Text.Replace("  ", " ");
                index = t.Text.IndexOf("  ");
            }
            t.SelectionStart = selectionStart;
avirag
Posting Whiz
313 posts since Jun 2009
Reputation Points: 31
Solved Threads: 36
 
Thanks for your reply.......... But when I am using left arrow key and writes something then its taking cursor to the end again and again which is not proper........


And also mark this thread solved if it help you.........:)

avirag
Posting Whiz
313 posts since Jun 2009
Reputation Points: 31
Solved Threads: 36
 

Thanks avirag..............
It worked............

vinnijain
Junior Poster
145 posts since Jul 2009
Reputation Points: 11
Solved Threads: 12
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: