PDA

Click to See Complete Forum and Search --> : Closing an dialog box that has focus


steve_rm
Mar 30th, 2005, 03:07 AM
Hello

I have a very difficult problem to explain. I have a password dialog box so that people can change there password. The dialog box will request for their current password, anc check that is is valid. Then request them to enter their new password and then re-enter their new password. The passwords are checked using the tab button when they leave a text box on the (leave event). When they have done all this they click the ok button and the new password is set. If however, they cancel using the cancel button. Then the dialog box will not close. This is the code below so you can better understand my problem.

This is the code for when the user enters the password, and presses the tab key to go to the next text box. When this event fires it will check the password is correct. Enter new password and re-enter password uses the same idea. I will not post it here as the code is the same.


private void txtEnterPassword_Leave(object sender, System.EventArgs e)
{
string queryValidatePwd = "SELECT Username, Password FROM AdminUsers WHERE Username = '" + lblLoginName.Text + "' AND Password = '" + txtEnterPassword.Text + "' ";

try
{
OleDbCommand cmdValidatePwd = cnnChangePassword.CreateCommand();
cmdValidatePwd.CommandType = CommandType.Text;
cmdValidatePwd.CommandText = queryValidatePwd;

OleDbDataAdapter daValidatePwd = new OleDbDataAdapter(cmdValidatePwd);
DataTable dtValidatePwd = new DataTable("AdminUsers");

int count = daValidatePwd.Fill(dtValidatePwd);
if ( daValidatePwd.Fill(dtValidatePwd) == 1 ) //Password is correct
{
txtEnterNewPassword.Visible = true;
txtEnterNewPassword.Focus(); //Get focus of next text box.
}
else
{
MessageBox.Show("Password is incorrect for this username - please try again","Incorrect Password",MessageBoxButtons.OK,MessageBoxIcon.Warning);
txtEnterPassword.Clear();
txtEnterPassword.Focus();
}
dtValidatePwd.Dispose();
daValidatePwd.Dispose();
}
catch ( OleDbException ex )
{
MessageBox.Show(ex.Message);
}
catch ( Exception ex )
{
MessageBox.Show(ex.Message);
}
}


Code for when the user closes the dialog box. The the other textboxes have focus you cannot close this form. I have tired using this.close() as well, but still does not work.


private void btnCancel_Click(object sender, System.EventArgs e)
{
this.Dispose();
}


Thanks for you help in advance,

Steve

Aaron Young
Apr 1st, 2005, 05:38 PM
The problem is you're trying to use the Leave event to trigger the process
and that leaves the user no way to cancel if they happen to give the control focus.

If you really want to use the Leave event then you should give the user some way
to bypass the process, i.e. if there is no entered text.

That really isn't very good practice though, so I recommend that you instead trigger the
process via an OK button.

Regards,

- Aaron.

steve_rm
Apr 2nd, 2005, 12:53 AM
Thanks for you reply.

Do you have any code sample to enforce you answer. That would be most gratefull.

Thanks in advance,

Steve

Pirate
Apr 6th, 2005, 04:05 AM
Validate event is perfect for this situation . Read about its cancel event in MSDN.