【问题标题】:validating textbox in windows form applications在 Windows 窗体应用程序中验证文本框
【发布时间】:2011-08-24 16:20:20
【问题描述】:

我的情况是:

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

以下不适用于我的场景。

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

【问题讨论】:

标签: c# winforms validation textbox spaces


【解决方案1】:
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"); 
        } 
    }
}

【讨论】:

    【解决方案2】:

    我相信lazyDBA 的回答对您的要求是正确的,所以消息框类似于:

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        【解决方案4】:

        试试

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

        【讨论】:

          【解决方案5】:

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

          【讨论】:

            【解决方案6】:

            你应该使用正则表达式

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

            【讨论】:

              【解决方案7】:

              由于您没有简要描述,如果我没记错的话,您想修剪开头的空格吗?

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

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

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

              【讨论】:

                猜你喜欢
                • 2011-08-24
                • 1970-01-01
                • 2011-05-11
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多