5
Answers

textbox restricting spaces using c# code

sai

sai

7y
260
1
Hi ,
    How to restrict spaces in a textbox by the user using c# code.Thanks 
Answers (5)
1
Manas Mohapatra

Manas Mohapatra

NA 29.3k 3.3m 7y
You can try like that(bind the event to textbox):
  1. private void textBox1_KeyPress(object sender, KeyPressEventArgs e)  
  2. {  
  3.     if ((e.KeyChar >= 65 && e.KeyChar <= 91) || (e.KeyChar >= 97 && e.KeyChar <= 123))  
  4.     {  
  5.         if (i && e.KeyChar == 69)  
  6.         {  
  7.             i = false;  
  8.         }  
  9.         else if (e.KeyChar!=69)  
  10.         {  
  11.             e.Handled = true;  
  12.         }  
  13.     }  
  14. }  
1
Ramesh Palanivel

Ramesh Palanivel

NA 9.5k 138.4k 7y
Hi sai, Try this code in textbox keypress event.. It will work...
1
sai

sai

NA 155 26k 7y
Hi , Thanks for reply ,this code is not working under button click event
1
Sundar

Sundar

NA 9.6k 94.1k 7y
if (textBox1.Text.Length==0 && e.KeyChar==' ')
{
e.Handled = true;
}
else
{
e.Handled = false;
}
 
 
try this code. this may helps you. thanks in advance  
0
Amar Srivastava

Amar Srivastava

NA 681 16.3k 7y
Better to use the TextBox.TextChanged Event.
 
private void myTextBox_TextChanged(object sender, EventArgs e)
{
      TextBox tb = sender as TextBox;
      if (tb != null)
      {
            YourButton.Enabled = !string.IsNullOrWhiteSpace(tb.Text);
      }
}