validating textbox in windows form applications

只谈情不闲聊 提交于 2019-11-28 14:52:47
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"); 
        } 
    }
}

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!");
   }  
}`

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)) 
}
FIre Panda

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

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

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
        }

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!