TextBox验证即使在禁用时也能正常工作

时间:2016-07-30 06:39:26

标签: c# visual-studio

我已经对文本框进行了验证以检查它们是否为空,但即使禁用了文本框,也会检查条件并根据验证消息进行显示

  

文本框不能为空

if ((txt1Yes1.Text.Equals(string.Empty) || txt1Yes2.Text.Equals(string.Empty))
         MessageBox.Show("Please Enter All The Details");

我已经写过这个,只有在启用了文本框的情况下才能进行检查。

2 个答案:

答案 0 :(得分:0)

无论是否禁用if控件,您都可以添加额外的TextBox条件。

void validateDetails()
    {
        if (txt1Yes1.Enabled && txt1Yes2.Enabled)
        {
            if ((txt1Yes1.Text.Equals(string.Empty) || txt1Yes2.Text.Equals(string.Empty)))
            {
                MessageBox.Show("Please Enter All The Details");
            }
        }
    }

要检查TextBox控件是否已禁用(切换),我已使用CheckedChanged(...)控件的checkBox事件。

private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
        foreach (Control control in this.Controls)
        {
            if (control is TextBox)
            {
                TextBox tbox = control as TextBox;
                if (checkBox1.Checked)
                {
                    tbox.Enabled = false;
                }
                else
                    tbox.Enabled = true;
            }
        }
    }

答案 1 :(得分:0)