0

在我的表单中,几乎有 30-35 个控件,有些是启用的,有些是禁用的,例如如果该人未婚并且如果用户选择他们已婚,则禁用配偶姓名,则启用该控件。

最初,提交按钮被禁用,但一旦所有启用的字段都填满,我希望提交按钮自动启用。

为此,我已经完成了这段代码。

if ((nametxt.Text != null) && (f_nametxt.Text != null) &&
    (m_nametxt.Text != null) && (gotra_txt.Text != null) &&
    (panthcb.Text != null) && (fhntext.Text != null) &&
    (edulvlcb.Text != null) && (bloodcb.Text != null) &&
    (MarritalStatus != null) && (s_nametxt.Text != null) &&
    (s_edulvlcb.Text != null) && (s_bgcb.Text != null) &&
    (ressi_addresstxt.Text != null) && (ressi_phnotxt.Text != null) &&
    (mobi_notxt.Text != null) && (office_addresstxt.Text != null) &&
    (occup_typetxt.Text != null) && (occup_naturecb.Text != null) &&
    (office_phno1txt.Text != null) && (office_mobnotxt.Text != null))
{
   submit_addbtn.Enabled = true;
}

我不知道这是否正确以及我应该在哪里(以何种形式)这样做。

请告诉我并帮助我。

这是文本框的代码完成keydown事件,此事件未触发

 private void mobi_notxt_KeyDown(object sender, KeyEventArgs e)
    {
        foreach (Control c in this.Controls)
        {
            TextBox txt = c as TextBox;
            if (txt != null)
            {
                if (!(txt.Enabled && txt.Text != ""))
                {
                    allFilled = false;
                    break;
                }
            }
        }

        if (allFilled)
        {
            submit_addbtn.Enabled = true;
        }
        else
        {
            submit_addbtn.Enabled = false;
        }
    }
4

1 回答 1

1

您可以遍历所有 TextBox 控件并检查它们是否已启用。如果是,请检查 Text 属性。

bool allFilled = true;

foreach (Control c in this.Controls)
{
    TextBox txt = c as TextBox;
    if (txt != null)
    {
        if (txt.Enabled && txt.Text == "")
        {
            allFilled = false;
            break;
        }
    }
}

if (allFilled) 
{
    button1.Enabled = true;
} else
{
    button1.Enabled = false;
}

因此,如果每个启用的字段都包含某些内容,则您的 allFilled 布尔值将为真,而在另一种情况下则为假。

您可以将其分配给您喜欢的任何事件。例如,在您分配给所有文本框的按键事件中

于 2013-04-30T11:06:58.317 回答