Adding a checkbox to a Visual Foxpro grid

[Add a control to a Visual FoxPro grid] By default, a Visual FoxPro grid holds a textbox in each column. Each column in a grid is a container and can hold any other control. It can hold a container container control such as a pageframe and can even be used to hold another grid. These are extreme examples, a simple more useful example is to replace the textbox with a checkbox as shown in the screenshot here.

The technique to replace the textbox with another control is a simple one, but it's not at all obvious. The first step is to delete the textbox but the problem is that you seem to have no way of selecting the textbox on screen. The column is a VFP container inside the grid container but neither a click nor a right-click lets you drilldown from one to the other. The trick is to use the list of controls in 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 and you are still unable to select it.
  • Go to the Properties Window and look at the controls within that column.
  • You should now 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 from the grid 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 in the column 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.

Access Tips

FoxPro Tips

General Tips


Related Items

Visual FoxPro Autocomplete

Autocomplete in Visual Fox Pro

The textbox class in Visual FoxPro 9 has a new Autocomplete property which lets the user choose from the previous values that have been entered into that textbox.

Read More

FoxPro Functions

FoxPro has always had functions like FREAD and FWRITE to read and write files at a low level. They can handle files which defeat the newer STRTOFILE and FILETOSTR functions in Visual FoxPro.

Read More

Database development

Alvechurch Data specialise in Microsoft Access and Visual FoxPro databases and have fifteen years experience developing databases for small business use.

Read More

Access and Visual FoxPro Training

Microsoft Access and Visual FoxPro training courses, tutorials and workshops offered by Alvechurch Data in Worcestershire.

Read More