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;
            }

        }

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;
            }
        }

Edited 6 Years Ago by ereruh: n/a

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.

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?

Edited 6 Years Ago by ereruh: n/a

This question has already been answered. Start a new discussion instead.