Microsoft KB Archive/168835

From BetaArchive Wiki
< Microsoft KB Archive
Revision as of 12:29, 21 July 2020 by X010 (talk | contribs) (Text replacement - "&" to "&")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Knowledge Base

How To Add Copy and Paste Functionality to Grid Control

Article ID: 168835

Article Last Modified on 6/29/2004


  • Microsoft Visual FoxPro 5.0 Standard Edition
  • Microsoft Visual FoxPro 5.0a
  • Microsoft Visual FoxPro 6.0 Professional Edition

This article was previously published under Q168835


When a Grid control object based on the Grid32.ocx is placed on a form, the cut, copy, and paste menu items are not available. Additionally, the hot keys, such as CTRL+V, do not function. This article shows how to use properties of the Grid control to emulate these editing functions.


Since neither the system menu options nor the shortcut keys for cut, copy, and paste work within the Grid control, the Grid32.OCX object properties must be used to accomplish these functions. To emulate these editing functions, the Clip, Col, Row, and Text properties of the Grid control must be used.

The example provided below uses a TextBox control and a Grid control to demonstrate how to copy, cut, and paste a text string between the two objects programmatically.

The following steps create a form containing a Grid control that has copy, cut, and paste functionality:

  1. Create a form and place a TextBox and an OLE Container Control on it.
  2. In the OLE Container Control, click Insert Control and choose the Grid Control object.
  3. Right-click the Grid control and choose Grid properties. Click the General tab and change both the Row and Col properties to 3 and click Apply.
  4. In the Init of the form, place the following code:

           ** This code places text values in the Grid object **
           Thisform.OleControl1.Row=1 && Resets focus to first cell.
  5. Add three command buttons to the form and set the following properties and events as directed below:

            Caption="Copy to Grid"
            Click Event:
            Caption="Copy From Grid"
            Caption="Cut From Grid"
            Click Event:
  6. Save the form and run it.
  7. Type "Hello" (without the quotes) in the text box and press "Copy to Grid." The word "Hello" appears in the Grid control.
  8. Click on the cell containing "Test2" with the mouse and then select "Copy from Grid." "Test2" appears in the text box.
  9. Click on the cell containing "Hello" and select "Cut From Grid." "Hello" appears in the text box and the contents of the first cell of the Grid control disappear.


For additional information on the properties of the Grid control, invoke the Visual Basic Help file by clicking the Grid control and pressing the F1 key.

Keywords: kbhowto KB168835