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

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

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

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

commented: You are too good for this world! :) +4

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

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.