Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
VFP version that saves the value of control with focus
Message
From
10/06/2003 06:36:59
 
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00798195
Message ID:
00798280
Views:
31
Hello Brenda.

At what version of VFP will it save the value of the control that currently has focus. I am looking at an older program and it has the following logic for every control and in every SAVE method. Is there a version of VFP where this type of logic is no longer needed?

Actually, this code:
FOR EACH CONTROL
csource = this.controlsource
&csource = this.value
is not needed at all and I have no idea what the programmer was trying to do here < s >

If your save method is called from a toolbar button or a menu, you have to take action to save the content in the buffer of the current active control. The reason for this is that the underlying data to which a control is bound is not updated from its value until the Valid() fires.

However, if the control does NOT lose focus, the event never fires. Unfortunately, this is precisely what happens when you use a toolbar (or a menu option) to determine the next action. The reason is that neither a toolbar, nor a menu, ever receive focus in Visual FoxPro, so the focus never leaves the control and the Valid never fires.

The reason for this behavior is so that Toolbars and Menus will not interfere in the normal operation of screens. It would be very irritating indeed if all the user wanted to do was to use the EDIT options of the menu to copy some text and the Valid (and LostFocus) of the current control immediately fired. After all, there is no way for Visual FoxPro to know WHICH menu item, or toolbar button, the user is about to choose!
The solution is to ensure that in your SAVE routines you force the currently active control to lose focus - thereby firing the Valid and LostFocus events. The simplest way is just to reset focus to the same control:

_SCREEN.ActiveForm.ActiveControl.SetFocus()

However, if there is code in the Valid() method of the active control, we cannot merely set focus to it. If we do, and its Value is invalid, what this is what happens: the error message is displayed, but the rest of the code continues to run and saves the bad data anyway. So we cannot just issue a SetFocus(). We must check the return value from the active control’s Valid() method first.

Furthermore, setting focus to the active control is only adequate when the control in question is not contained in a grid. The reason is that the GRID will be the ActiveControl, not the contained object. So to cover this situation you need some additional code as follows:
FOR EACH loColumn IN loActiveControl.Columns
  IF loActiveControl.ActiveColumn = loColumn.ColumnOrder
    loControl = EVAL( 'loColumn.'+ loColumn.CurrentControl )
    IF NOT EMPTY( loControl.Valid() )
      loControl.SetFocus()
      EXIT
    ENDIF
  ENDIF
ENDFOR
One thing to remember is that when you reset focus to a control it is not JUST the Valid that fires, the entire sequence of events will fire again, i.e. Valid(), LostFocus(), When() and GotFocus().
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform