Click to See Complete Forum and Search --> : SetFocus not working
J Fowler
October 7th, 2001, 10:53 AM
I'm attempting to reset the focus back to a text box after I reset a form. I use the command:
txtItem.SetFocus and it generates Runtime error 5, invalid procedure call or argument. Why?
~~~~~~~~~~~~
James Fowler
Andrew R.
October 7th, 2001, 12:09 PM
You should do it in Form_Activate but not in Form_Load.
J Fowler
October 7th, 2001, 12:14 PM
Okay, but it will be called several times during a form's life. How can I get it to work every time? I'm including it in a subroutine that is called in many places to reset the form.
~~~~~~~~~~~~
James Fowler
John G Duffy
October 7th, 2001, 12:46 PM
As long as the Form is loaded and the control you are trying to setfocus to is enabled it should work with no problems.
If you Unload the Form or disable the control then you will get the error you mentioned.
You mentioned it as being done from a subroutine. If the Subroutine is not part of the From then include the Form name in front of the setfocus like this
Form1.Text1.SetFocus
John G
J Fowler
October 7th, 2001, 12:52 PM
The subroutine is part of the same form that the text field is in. The text field is never disabled.
~~~~~~~~~~~~
James Fowler
John G Duffy
October 7th, 2001, 03:06 PM
Is the form visible at the time of the .SetFocus? If not, that error will occur.
John G
srinika
October 7th, 2001, 09:54 PM
send the relevent code
ramayansunil
October 8th, 2001, 12:14 AM
Hi,
I think u have written the code in the form load event. At the time of loading the form in to the memory it cant give the focus directly to a control. Bcos without the form gets loaded the control wont be loaded.
It is better u give the code text1.setfocus in the form_activate event.
have a nice time
sunil.
Clearcode
October 8th, 2001, 04:49 AM
public Sub SafeSetFocus(byval ctlIn as Control)
If ctlIn.Visible And ctlIn.Enabled then
ctlIn.SetFocus
End If
End Sub
-------------------------------------------------
Ex. Datis: Duncan Jones
Merrion Computing Ltd
http://www.merrioncomputing.com
Check out the new downloads - ImageMap.ocx is the VB control that emulates an HTML image map, EventVB.OCX for adding new events to your VB form and adding System Tray support simply, MCL Hotkey for implemenmting system-wide hotkeys in your application...all with source code included.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.