在 Windows 窗体应用程序中验证文本框

本文关键字:验证 文本 应用程序 Windows 窗体 | 更新日期: 2023-09-27 17:56:13

我的场景是:

输入一个或多个字符后,文本框的起始位置不允许有空格 文本框允许空格

以下不适用于我的方案。

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

在 Windows 窗体应用程序中验证文本框

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"); 
        } 
    }
}
我相信

lazyDBA 的答案对您的要求是正确的,因此消息框如下所示:

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

你说选项一不适用。 使用相同事件和类似代码但在第二个 IF 语句中的第一个选项版本怎么样? 这样可以防止空格起作用,除非文本框中有其他字符。

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

试试

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

KeyPress事件中试试这个

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

如果您还必须使其工作,以防用户在输入某些文本后移动到TextBox字段的开头并尝试输入空格,那么这也将禁用它

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

你应该使用正则表达式

        string strRegex = @"^(['w]+.*)$";
        string strTargetString =  textBox1.Text;
        if (!Regex.IsMatch(strTargetString, strRegex))
        {
            // show error that spase not allow at the bigin of string
        }

正如您没有简要描述的那样,如果我没错的话,您想在开头修剪空格,对吗?

那么我的回答是,你可以用多种方式处理它,一种可能的方法是也通过以下方式处理它。我在下面写的一些示例代码,你可以检查一下,它在我的示例应用程序中完美运行:

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

正如你在这里看到的,我写了两个事件,因为如果任何正文在开头粘贴带有空格的文本,在你的文本框控件中,那么它将完美地从开头删除空格。