简体   繁体   English

当文本框已填充数据时如何启用禁用的按钮

[英]How to enable the disabled button when the textbox is already filled with data

private void UserForm_Load(object sender, EventArgs e)
{
    if (txtUsername.Text != "" || txtPassword.Text != "" || txtID.Text != "" || txtFirstName.Text != "" || txtMiddleName.Text != "" || txtLastName.Text != "" || txtDep.Text != "")
    {
        btnNext.Enabled = true;
    }

    else
    {
        btnNext.Enabled = false;
    }
}

I was trying to make a form where the button is disabled and once the textboxes filled up with data, the button will be enabled. 我试图创建一个禁用按钮的表单,一旦textboxes填充了数据,该按钮将被启用。

The problem is, the button is disabled even the textboxes is already filled with data. 问题是,即使文本框已经填充了数据,该按钮也被禁用。

That is happening because you might be populating the control later and checking the text boxes first in the code. 之所以发生这种情况,是因为您稍后可能会填充控件,并在代码中首先检查文本框。

The code to enable and disable the controls should be called after the controls get populated with the values. 在用值填充控件之后,应调用启用和禁用控件的代码。 So, you just need to call your code at right time. 因此,您只需要在正确的时间调用您的代码。

That's because you are not checking if you should enable the button OnChange of the text field each time. 这是因为您没有检查是否应每次启用文本字段的按钮OnChange。 You are checking only on UserForm_Load(object sender, EventArgs e) 您只在UserForm_Load(object sender, EventArgs e)

This is what you need: 这是您需要的:

private void textBox__TextChanged(object sender, EventArgs e)
{
    if (txtUsername.Text != "" || txtPassword.Text != "" || txtID.Text != "" || txtFirstName.Text != "" || txtMiddleName.Text != "" || txtLastName.Text != "" || txtDep.Text != "")
    {
        btnNext.Enabled = true;
    }

    else
    {
        btnNext.Enabled = false;
    }
}

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

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