focus() method fails with second show form

tcl4p

Well-known member
Joined
Feb 29, 2008
Messages
48
Programming Experience
1-3
I have a simple login field, which works fine with the exception of one quirk. The form is set up with the tab order to go to the UserID first and the Password text box second. This works fine when the form is used the first time, but when the form is used the second time the focus in placed in the Password field. If the form is used a third time, focus is moved the OK button, that is each time moves to the next tab order. I even tried to set the focus in the Load event as in Me.txtUserID.Focus(), but this doesn't change the behavior. Again, I should note that I close the form and not hide the form.

Can anyone explain this behavior and how to fix it? I this a result of the form not closing down correctly with the me.close code. I remember in vb6 you had to set the form to nothing in the unload method. Is this necessary in .net and if so how is this done?

Thanks,
Tom
 
Assuming your form is called frmLogin, try :-

VB.NET:
Using fLogin as new frmLogin
    fLogin.ShowDialog()
End Using
 
Thanks, that appeared to work, but I would sure like to know why this should make a difference.

Thanks,
Tom
 
When you Close a form, you havent disposed it. So when you open/show your form again using your code, what you see is the 1st form.

By coding it with "Using ... End Using", you are effectively writing fLogin.Dispose which releases all references to the 1st form. When you run the code again, it creates a completely new form that is in no way related to the 1st one. "Using ... End Using" is a good way to remember to dispose of objects when you no longer need them.

Hope that helps.
 
Back
Top