I need to be able to check one textbox and another one on keypress. If both textbox characters are over 4 characters then a button will become enabled. So far... I have....

private void txtUser_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (txtUser.Text.Length >= 4 && txtUser.Text.Length < 20)
            {
                btnLogin.Enabled = true;
                btnLogin.Image = iUltimate.Properties.Resources.lock_open;
            }
            else
            {
                btnLogin.Enabled = false;
                btnLogin.Image = iUltimate.Properties.Resources._lock;
            }
        }

        private void txtPass_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (txtPass.Text.Length >= 4 && txtPass.Text.Length < 20)
            {
                btnLogin.Enabled = true;
                btnLogin.Image = iUltimate.Properties.Resources.lock_open;
            }
            else
            {
                btnLogin.Enabled = false;
                btnLogin.Image = iUltimate.Properties.Resources._lock;
            }

        }

Recommended Answers

All 6 Replies

If both textboxes have 4 - 20 characters the button is enabled.

private void textbox_keyUp(object sender, KeyEventArgs e) //add event to both textboxes
        {
            if (txtUser.Text.Length >= 4 && txtUser.Text.Length < 20)
            {
                if (txtPass.Text.Length >= 4 && txtPass.Text.Length < 20)
                {
                    btnLogin.Enabled = true;
                }
                else
                {
                    btnLogin.Enabled = false;
                }
            }
            else 
            {
                btnLogin.Enabled = false;
            }
        }

Then just add textbox_keyUp(); where the 2 keypresses are?

If both textboxes have 4 - 20 characters the button is enabled.

private void textbox_keyUp(object sender, KeyEventArgs e) //add event to both textboxes
        {
            if (txtUser.Text.Length >= 4 && txtUser.Text.Length < 20)
            {
                if (txtPass.Text.Length >= 4 && txtPass.Text.Length < 20)
                {
                    btnLogin.Enabled = true;
                }
                else
                {
                    btnLogin.Enabled = false;
                }
            }
            else 
            {
                btnLogin.Enabled = false;
            }
        }

He's right on the combine- go into the designer, go to the textbox, click the events, make sure they both go to the same code. Use

private void textbox_keyUp(object sender, KeyEventArgs e) //add event to both textboxes
{
    if (txtUser.Text.Length >= 4 && txtUser.Text.Length < 20
        && txtPass.Text.Length >= 4 && txtPass.Text.Length < 20)
        btnLogin.Enabled = true;
    else
        btnLogin.Enabled = false;
}

It just simplifies it.

Thank You

The button only gets unlocked when one of the text boxes reaches 5 characters.

Probably because you have it in the KeyPress event. Try adding the code into a KeyUp event instead. And are you adding the same even to both textboxes?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.