Adding a checkbox to a Visual Foxpro grid

[Add a control to a grid] By default, a Visual FoxPro grid appears with a textbox in each column. A column in a grid is a container and can hold any other control - even a large and complex container such as a pageframe or even another grid. A simple example is to replace the textbox with a checkbox as shown in the example below:

The technique to replace the textbox with another control is simple, but not obvious. The problem is that you need to delete the textbox but you seem to have no way of selecting the textbox on screen. The column in a FoxPro container inside the grid container but there is no drilldown from one to the other. The trick is to use the Properties Window to make that selection:

  • Right-click on the grid.
  • Select Edit to allow selections within the grid.
  • Click on the checkbox in the Form Controls toolbox - but see the note about custom controls in the next paragraph.
  • Move the mouse and click on the column in the grid.
  • Nothing will change on screen - the column will still appear to hold a textbox.
  • Go to the Properties Window and look at that column.
  • You should see that the column holds two controls, the original textbox and the new checkbox.
  • Click on the textbox to select it.
  • Now click on the form. Be careful. Click on the header of the form rather than on any control within the form. You have just selected the textbox in the Properties Window and you do not want to change that selection by clicking on the body of the form or on any control within the form.
  • Press the Delete key.
  • The textbox should disappear to show the checkbox that has been hiding behind it.

Adding a control from a custom library

When you are adding controls to a form you have the options of clicking on the Form Controls toolbar or of dragging a control from a class library on the Classes tab of the Project Manager. This second option does not work when you are trying to add a control to a grid. You have to put the control you want onto the Form Controls toolbar.Use the technique described here to add custom controls to the toolbar.

Aligning the checkbox

By default the checkbox will appear against the left-hand edge of the column. This is usually wrong and if you want to see it in another position then the grid in VFP 9 has a new property which lets you set the alignment of the control in the column.

In earlier versions the workaround is to create a container class, centre the checkbox manually inside this container and then add the container to the column. The container itself will be left-justified but if the container is made transparent and borderless then the checkbox will appear to be centred in the column.

Highlighting the selected rows

In this example the customer wanted a really clear way of seeing which entries had been selected. The grid's DynamicForeColor property allowed me to grey out those rows which were not selected.