Microsoft KB Archive/305985

= How To Add Functionality to Your Dynamically Built Grid =

Article ID: 305985

Article Last Modified on 6/29/2004

-

APPLIES TO


 * Microsoft Visual FoxPro 3.0 Standard Edition
 * Microsoft Visual FoxPro 3.0b Standard Edition
 * Microsoft Visual FoxPro 5.0 Standard Edition
 * Microsoft Visual FoxPro 5.0a
 * Microsoft Visual FoxPro 6.0 Professional Edition
 * Microsoft Visual FoxPro 7.0 Professional Edition

-



This article was previously published under Q305985



SUMMARY
Although a grid has its own events, the events do not always fire when you would expect them to. Because a grid is a container control, the objects in the container receive the events. Therefore, to add functionality to a grid, you must add the method code to the text boxes (or other controls) that display the data. However, you cannot add code to objects at run time.



MORE INFORMATION
Because you cannot add code to objects at run time, you must use a custom class for your text boxes. The grid, however, does its best to display the table as if you were browsing it, so you need to be careful about the order in which you do things. You can also add code to custom header classes; for example, code to sort the grid by the column that you click.

The following code displays a message box when you double-click the displayed data. If you double-click below or to the right of the data, the grid DblClick event fires instead. PUBLIC loForm

CD HOME

USE labels IN 1 SELECT 25 && Temporally select blank work area.
 * !* Make sure that the grid doesn't pick this up automatically

loForm = CREATEOBJECT(&quot;Form&quot;) loForm.AddObject(&quot;grdLabels&quot;, &quot;grdName&quot;)

WITH loForm.grdLabels .AddObject(&quot;colName&quot;, &quot;Column&quot;) *!* We now have a Grid with a single column containing a header *!* and a textbox. .colName.Width = 200

*!* If this is set before the columns are defined, the *!* grid picks up the columns it thinks it needs. .RecordSourceType = 1 .RecordSource = &quot;labels&quot; .colName.ControlSource = &quot;labels.name&quot; .colName.Header1.Caption = &quot;Label name&quot;

*!* Now we add our custom textbox control, and remove the original. .colName.AddObject(&quot;txtName&quot;, &quot;txtGrid&quot;) .colName.txtName.BorderStyle = 0 .colName.CurrentControl = &quot;txtName&quot; .colName.RemoveObject(&quot;Text1&quot;)

*!* The default visibility for all objects added with AddObject is .F.  .colName.Visible = .T.   .Visible = .T. ENDWITH

loForm.Visible = .T.

DEFINE CLASS txtGrid AS TextBox PROCEDURE DblClick =MESSAGEBOX(STR(RECNO)) ENDPROC ENDDEFINE

DEFINE CLASS grdName AS Grid PROCEDURE DblClick =MESSAGEBOX(&quot;You didn't click on the data.&quot;) ENDPROC ENDDEFINE

