繁体   English   中英

如何在 c# winforms 中的所有文本框不为空后启用按钮?

[英]How to enable button after all textboxes are not empty in c# winforms?

在我的所有文本框都不为空之后,如何将按钮属性设置为 enabled=true? 我正在学习编程,我的应用程序很简单。

当我的一个文本框有文本时,我知道如何启用此属性,但事实并非如此。

用例是用户需要将数据放入两个文本框中,然后才能单击 btn。 如何以最简单的方式验证所有表单然后启用按钮?

只有 2 tb: https://i.imgur.com/JUslNWE.png

您需要创建一个TextBox_TextChanged事件并订阅所有文本框。

private void TextBox_TextChanged(object sender, EventArgs e)
{
    int notEmptyTextBoxCount = 0;
    int textBoxCount = 0;
    foreach (var item in Controls)
    {
        if (item is TextBox txtb)
        {
            textBoxCount++;
            if (txtb.Text != String.Empty)
                notEmptyTextBoxCount++;
        }
    }
    if (textBoxCount == notEmptyTextBoxCount)
        button.Enabled = true;
    else
        button.Enabled = false;
}

感谢大家的所有反馈。

我设法做到了这种方式:

private void ValidateTextBoxes()
    {
        if (loginTextBox.Text.Length != 0 && passTextBox.Text.Length != 0)
        {
            generateHashBtn.Enabled = true;
        }
        else
        {
            generateHashBtn.Enabled = false;
        }
    }

    private void TextBox1_TextChanged(object sender, EventArgs e)
    {
        ValidateTextBoxes();
    }

    private void TextBox2_TextChanged(object sender, EventArgs e)
    {
        ValidateTextBoxes();
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM