Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validation Within Grid Textbox 1

Status
Not open for further replies.

Auguy

Programmer
May 1, 2004
1,206
US
I have a grid with several columns that use the standard textbox of the grid. One of these columns I do not want to be empty. The following valid works as long as I stay within the grid by tabbing or clicking with the mouse. But if I click outside the grid (Such as button), I still get the warning message, but the focus is now outside of the grid with the field still empty. The user can then click the save button (Bad). I tried removing the warning form thinking it was causing the problem, but it didn't help. Is there something wrong with this validation, or do I need to take a diferent approach?

Code:
* In Valid of Grid TextBox
With This
  * Empty value not allowed
  If Empty(.Value)
    ... Custom Warning Message (Form) Displayed Here
    .Value = cOriginalValue
    Return .F.
  Endif
  ... Rest of Code Here
Endwith
 
I have a grid with several columns that use the standard textbox of the grid. One of these columns I do not want to be empty. The following valid works as long as I stay within the grid by tabbing or clicking with the mouse. But if I click outside the grid (Such as button), I still get the warning message, but the focus is now outside of the grid with the field still empty.

You must be using VFP 6 because this one was fixed in version 7.

This is what I had to say on the subject in "1001 Things You Wanted to Know About VFP"

Gotcha! Grid's valid fires before the valid of the current control

Notice that when moving from a grid to another object on the form, the grid's Valid fires before the valid of the active cell. This is a bug. Normally you would expect to use the control's Valid method to determine whether that control can lose focus. However, when a control is contained within a grid, there are two levels at which focus operates. First, between the control and other controls that are also within the grid. In this context things behave normally.

Second there is the issue of which form object has focus. This bug will bite you when focus is being moved between the grid and another object. It is, as you would expect, the grid’s Valid method that determines whether the grid can lose focus. However, because the grid’s Valid is called before that of any contained control, a contained control cannot prevent the grid from losing focus even though its own validation has failed.

This can and will allow bad data to be entered into the grid. Fortunately, there is an easy workaround, which ensures that the grid’s Valid explicitly calls and returns the result of any contained control’s validation. So if the text box in the first column of the grid has code in its Valid, this line in the grid's Valid method takes care of the problem:

RETURN This.Column1.Text1.Valid()


Marcia G. Akins
 
Marcia,

Thanks for the very detailed answer and the work around. I will try it out ASAP. I apologize for forgetting to include that this was from a VFP 6 app.

Auguy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top