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

This will block a double space, ie: "a<space><space>b" but allow "a<space>b<space>c":

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

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

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

Comments
You are too good for this world! :)

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":

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

Edited 7 Years Ago by vinnijain: n/a

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?

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

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

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;

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

This question has already been answered. Start a new discussion instead.