validating textbox in windows form applications

2019-01-20 20:39发布

问题:

My scenario is:

Not allowing spaces at starting position of textbox after enter one or more characters text box allows spaces

Below not applicable to my scenario.

  1. private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.Handled = (e.KeyChar == (char)Keys.Space))
        {
            MessageBox.Show("Spaces are not allowed");
        }
    }
    
  2. textBox1.Text.TrimStart()
    

回答1:

I believe that lazyDBA's answer is correct for your requirements, so with the message box something like:

if (textBox1.Text.Length == 0)
{
   if (e.Handler = (e.KeyChar == (char)Keys.Space))
   {
       MessageBox.Show("space not allowed!");
   }  
}`


回答2:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e) \
{ 
if(textBox1.Text.Length == 0)
    {
    if (e.Handled = (e.KeyChar == (char)Keys.Space)) 
        { 
        MessageBox.Show("Spaces are not allowed at start"); 
        } 
    }
}


回答3:

As you have not described briefly and if I am not wrong you wanted to trim the spaces at beginning right ?

then my answer is, you can handle it many way and one possible way is to handle this in the following way also. Some sample code I have written here in below, you can check with that, its running perfectly in my sample application:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((textBox1.SelectionStart == 0) && (e.KeyChar == (char)Keys.Space))
            {
                e.Handled = true;
            }
        }

private void textBox1_TextChanged(object sender, EventArgs e)
    {
           //Store the back up of Current Cursor Possition.
           int cusorpos = textBox1.SelectionStart;

           if (false == string.IsNullOrEmpty(textBox1.Text))
           {
                 if (textBox1.Text[0] == ' ')
                 {
                       //Trim Spaces at beginning.
                       textBox1.Text = textBox1.Text.TrimStart(' ');

                       //Set the Cursor position to current Position. 
                       textBox1.SelectionStart = cusorpos;
                  }
           }
    }

as you can see here i wrote two events its because if any body paste a text with spaces at beginning, in your textbox control then it will work perfectly to remove the spaces from the beginning.



回答4:

You say option number one is not applicable. How about a version of option number one that uses the same event and similar code but inside a second IF statement. This keeps spaces from working unless there are other characters in the text box.

if (textBox1.Text.Length == 0)
{
 e.Handled = (e.KeyChar == (char)Keys.Space)) 
}


回答5:

Try

 private void textBox1_KeyPress(object sender, KeyPressEventArgs e)         
 {             
     if (e.Handled = (e.KeyChar == (char)Keys.Space))             
     {                 
        if(((TextBox)sender).Text.Replace(" ","") == "")
        {
             MessageBox.Show("Spaces are not allowed");  
             ((TextBox)sender).Text = string.Empty;
        }           
     }          
 } 


回答6:

Try this in the KeyPress event

if ((sender as TextBox).Text.Length <= 0)
    e.Handled = (e.KeyChar == (char)Keys.Space);
else
    e.Handled = false;

In case you have to make it work also in case if the user after typing in some text moves to the start of the TextBox field and attempts to enter space then this will disable that too

void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
     if ((sender as TextBox).SelectionStart == 0)
          e.Handled = (e.KeyChar == (char)Keys.Space);
     else
          e.Handled = false;
}


回答7:

you should use the regex

        string strRegex = @"^([\w]+.*)$";
        string strTargetString =  textBox1.Text;

        if (!Regex.IsMatch(strTargetString, strRegex))
        {
            // show error that spase not allow at the bigin of string
        }