Microsoft KB Archive/139874

= How to Disable a Button & Move the Focus to a Specified Object =

Article ID: 139874

Article Last Modified on 8/25/1999

-

APPLIES TO


 * Microsoft Visual FoxPro 3.0 Standard Edition

-



This article was previously published under Q139874



SUMMARY
When using a command button to set the focus to another object, you may sometimes want to disable the command button and move the focus to another object until another event occurs or a value is entered in a field. This article shows by example how to do it.



MORE INFORMATION
The most obvious solution, doesn't work. You can't use the SetFocus method followed by "Enabled=.F." in the Click event of a command button. When the Click event of the command button is activated, the command button becomes disabled, but the focus is not moved to the specified control. The focus moves to the next object in the Tab Order instead of to the specified object.

This occurs because the Default event processing occurs after user-defined code is run. When the default event procedure for the Click event is run, it discovers that the control is no longer enabled and defaults to setting the focus to the next object in the Tab order, so the user-specified SetFocus method is nullified.

To move the focus to the object specified in the Click event of the command button, you need to use the NoDefault clause. This results in the focus being moved to the correct object.

Step-by-Step Example
 Create a form, and add three command buttons to the form. Label the buttons Button 1, Button 2, and Button 3.  In the Click event of Button 1, add this code: ThisForm.Command3.SetFocus ThisForm.Command1.Enabled=.F.                            Run the form. Click Button 1, and you will see it become disabled. However, the focus is set to Button 2 (the next item in the tab order). Close the form.  Modify the form to add NoDefault to the first line of the Button 1 Click event: NoDefault ThisForm.Command3.SetFocus ThisForm.Command1.Enabled=.F.                            Rerun the form, and click Button 1. The focus should now be on Button 3.</ol>

<div class="references_section">