【问题标题】:Checking textBox in Windows Forms在 Windows 窗体中检查文本框
【发布时间】:2020-01-30 08:46:26
【问题描述】:

我是 Windows 窗体的新手,我有一个继续按钮和三个文本框。该按钮被禁用。现在用户首先必须在启用按钮之前填写所有三个文本框。

请帮帮我。

谢谢。

【问题讨论】:

  • 查看Control.TextChanged 事件。如果这没有帮助,请在您的问题中添加更多详细信息,说明您的实际问题是什么。

标签: c# winforms button textbox disable


【解决方案1】:

您可以向所有文本框添加TextChanged 事件,在处理程序中检查是否所有三个文本框都已填充,如果是 -> 按钮已启用。

【讨论】:

  • 感谢您这么快回答。 S对不起,我很新什么是处理程序
【解决方案2】:

您可以为此目的使用 TextChange 事件并在所有文本框上使用相同的方法,例如 -

然后像这样检查

private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(textBox1.Text) && !string.IsNullOrEmpty(textBox2.Text))
            {
                button1.Enabled = true;
            }
        }

【讨论】:

    【解决方案3】:

    这样就可以了。

    将一些方法连接到文本框的 TextChanged 事件(这可以在设计器中完成),但我已将其放在 InitializeComponent() 下的表单构造函数中;

    public Form1()
    {
        InitializeComponent();
    
        textBox1.TextChanged += TextBox_TextChanged;
        textBox2.TextChanged += TextBox_TextChanged;
        textBox3.TextChanged += TextBox_TextChanged;
    }
    

    然后我的文本更改方法只是调用一个验证方法并在此方法返回 true 时启用按钮。

    private void TextBox_TextChanged(object sender, EventArgs e)
    {
          button1.Enabled = ValidateTextBoxes();
          //Anything else you might want to do...
    }
    

    最后是我的验证方法。如果任何文本框为空,则再次非常简单,返回 false。

    private bool ValidateTextBoxes()
    {
        if (string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text)
                || string.IsNullOrEmpty(textBox3.Text))
        {
           return false;
        }
    
        //Any other validation you may want... e.g length, regex pattern etc.
    
        return true;
    }
    

    【讨论】:

      猜你喜欢
      • 2018-03-01
      • 2012-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-11
      • 1970-01-01
      相关资源
      最近更新 更多