Microsoft KB Archive/138095

= How to Store the Value of Check Boxes in a Grid to an Array =

Article ID: 138095

Article Last Modified on 12/11/1999

-

APPLIES TO


 * Microsoft Visual FoxPro 3.0 Standard Edition

-



This article was previously published under Q138095



SUMMARY
Sometimes you may find it useful to place a check box on a grid. There will be a check box for each record in the grid, so you need a place to store the value of each check box. Usually the value of the check box is stored in a field in the table that the grid is based on. If a user doesn't want the value of the check boxes stored in the table, then an array can be created to store the check box values. This works well, but keep in mind that an array can only hold 65,000 elements. This would not work on a table with more records than that.



Step-by-Step Procedure
 Create a form and add a table to the data environment. Place a grid on the form. Right-click the grid, and then click Builder. Add two fields to the grid. On the Properties sheet, add 1 to the ColumnCount of the grid. This makes the ColumnCount property equal to 3. While in the Properties sheet, select the Text1 object from the third column. Click the form title bar to allow the focus to stay on the Text1 object. Then press the DELETE key to delete the Text1 object from the grid. While the grid is still in edit mode, choose a check box object from the Forms Control toolbar and place it under the third column header where the Text1 object was just deleted. The check box should now be part of the grid. In the Properties sheet, choose the third Column object and change the Bound and Sparse properties to False (.F.). On the Form menu, click New Property. Type xyz(1) in the property box, and save it.  In the Load event of the form, place this code: DIMENSION thisform.xyz(RECCOUNT) FOR x = 1 TO RECCOUNT thisform.xyz(x) = 0 ENDFOR This dimensions the array xyz to the number of records in the open table. The FOR loop initializes the elements of the array, which clears the value of the check boxes so that they are not checked. </li> Set the ControlSource for the check box to thisform.xyz(RECNO). This places a 1 or 0 in the array element that relates to that particular record.</li> To check that this works correctly, place the command WAIT WINDOW STR(thisform.xyz(RECNO)) in the Valid clause of the check box. Every time the check box is selected or cleared, the value of 0 (unchecked) or 1 (checked) appears in the wait window.</li></ol>

If the number of records in the grid that are checked need to be counted, use a FOR loop to count the number of array elements that contain 1. The following code placed in the Click Event of a command button will count the number of checked boxes in the grid: counter=0 FOR x = 1 TO RECCOUNT IF thisform.xyz(x) = 1 counter = counter + 1 ENDIF ENDFOR WAIT WINDOW STR(counter)

Additional query words: VFoxWin

Keywords: KB138095

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.