Editable MSFLEXGRID Control-Derived Class

Environment: Windows 9x, Me, 2000, NT 4, XP; Microsoft Visual C++ 6.0

How to Make a MS FlexGrid Control Editable

There is not much documentation about MS FlexGrid; however, the technique suggested by Microsoft is to use an auxiliary Edit control for doing the task (see Reference 1).

The idea is simple. When you want to edit the text in a cell, you only need to retieve the cell's rectangle and place an edit control over it. The edit control will manage all the keyboard input. When the editing is finished, the text in the edit control is written into the cell, replacing the previous text. Then, the edit box is hidden until you want to edit other cell again.

The first problem is that the cell's rectangle is returned in TWIPS units. The TWIPS must be converted to pixels. Because a TWIP is 1/20 of a point and 1/1440 of an inch, the conversion between twips and pixels is done as follows:


The number of pixels per inch can be obtained with GetDeviceCaps(LOGPIXELSX) and GetDeviceCaps(LOGPIXELSY) for horizontal and vertical coordinates, respectively.

I have created two classes, CEditFlexGrid class, derived from CMSFlexGrid; and CCellEdit class, derived from CEdit. CEditFlexGrid has a member variable of the class CCellEdit.

Microsoft provides an example that can be found in the Knowldedge Base site (see Reference 2). This example and mine use a similar technique; however, I have introduced some features that are different from the MS example:

  • The MS example uses the default system font for the edit box. CCellEdit classs uses the current font in the flexgrid.
  • The MS example uses the default B/W edit box. CCellEdit takes the cell color every time.
  • In the MS example, the cells are always editable. In CEditFlexGrid the cells are only editable if the user presses the Return key or double pushes the left mouse button. The cells stops editing when the user clicks in another place, pushes Enter again, or changes the focus.

To change the color in the edit box, I have used the "Message reflection" technique, as shown in Reference 3. The procedure is to take the cell's colors and dynamically change the background and text colors in the edit box. The colors retrieved from the flexgrid are translated to valid RGB colors by using the function OleTranslateColor().

A similar procedure to the "Message reflection" is used for reflecting some MSFlexGrid events.

To handle special keys (Return) in the edit box and the flexgrid, I have used the technique described in Reference 4, overriding OnGetDlgCode() for both CEditFlexGrid and CCellEdit.

A strange problem happened every time a cell was enabled for editing. This problem was that the arrow keys did not work as expected. Normally, when you push an arrow key in an edit box with the focus, the caret moves. This did not happen with the CCellEdit object; the arrow keys produced a change in the grid's cell. The problem is that the arrow keys were treated as accelerator keys. Reference 5 explains this problem and how to solve it with the aid of the PreTranslateMessage() function.

How to Use CEditFlexGrid and CCellEdit in Your MFC Project

Add the four files EditFlexGrid.cpp, EditFlexGrid.h, CellEdit.cpp, and CellEdit.h to your project. Take care that you have written the appropiate #include directives for your project. Put the #include "EditFlexGrid.h" line in your .h file. Add a member variable to your MSFlexGrid object of the type CEditFlexGrid. Use any of the common functions of MSFlexGrid for sizing and filling the grids. When you want to edit a cell, just push Enter or double-click it. The cell edited will be the currently selected cell (take care of your selection mode flag). When you want to finish the editing, you can push Enter again, or click in another place or control. You can use any of the MSFlexGrid functions to retrieve the text in the cells.

Note: You cannot edit fixed cells. The class has been designed to work as said above; there is no guarantee to work if it is used in a different way. Changes of the grid font at run time are not supported.


  1. VB3 How to Edit Grid Cells in VB Using Overlapped Text Box: http://support.microsoft.com/ ... /ARTICLES/Q85/1/09.asp&NoWebContent=1

  2. EditGrid.exe: Edit Cells in MSFlexGrid ActiveX Control: http://support.microsoft.com/default.aspx?scid=kb;en-us;196833

  3. TN062: Message Reflection for windows controls: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfcnotes_tn062.asp

  4. How to Trap Arrow Keys in an Edit Control of a Dialog Box: http://support.microsoft.com/ ... /articles/Q104/6/37.asp&NoWebContent=1

  5. PRB: MFC ActiveX Control Ignores ARROW Keys on VB Container: http://support.microsoft.com/ ... /ARTICLES/Q180/4/02.asp&NoWebContent=1


Download source code - 23 Kb


  • Simple Editing in Flexgrid

    Posted by jayphilip on 11/03/2008 05:30am

    "How to Make a MS FlexGrid Control Editable" - An Excellent article. As author says, the idea is very simple. Just have a look on the opensource of Editable Flexgrid - wbcsoftwarelab.

    • abdusaid

      Posted by abdusaid on 09/23/2009 08:12am


  • Editing of FlexGrid control cells can be done in a more efficient way

    Posted by lavanya_thanneru on 02/22/2006 03:33am

    In VC++ it can be done using OnKeyPress method of CMSFlexGrid. The sample code is as follows... CString str = ""; void CFlxgridDlg::OnKeyPressMsflexgrid1(short FAR* KeyAscii) { // TODO: Add your control notification handler code here str+=(char)*KeyAscii; m_FGrid.SetClip(str); } The string can be reinitialised using OnSelChange method of flexgrid.Thie method is called whenever the focus on cell changes. void CFlxgridDlg::OnSelChangeMsflexgrid1() { // TODO: Add your control notification handler code here str = ""; }

    • pls help me !!!

      Posted by prabhakar_er on 12/12/2006 12:56am

      dear madam.. i have one doubt madam...i am also working on visual c++.. i have problem to flexgrid funtion..how can i add the funtunality to in...suppose in 1,1 grid i want to show the messahe alert...1,2 on i want to show the date...1,3 i want to set the timer..and last one i want to set the check box button..that means my first row and first colon r fix..and down side one ok button and one cancel buton..i all ready refer the www.codeguru.com and www.codeproject.com.. but i am not succes if u know any site or u have any idea pls guide me..thanks in advance.. i will send u the snap shot of my utility..this will clear ur idea..i got ur mail id from codeguru..u posted one of reply for grid control..i got ur id from below url.. http://www.codeguru.com/cpp/controls/controls/gridcontrol/comments.php/c5291/?thread=9411

  • Ping!!!

    Posted by Kaitsu on 12/03/2004 08:59am

    How do I get rid of "ping" sound when leaving edited cell with enter?

  • Ping!

    Posted by Kaitsu on 12/03/2004 08:27am

    How do I get rid of "ping" sound when leaving edited cell with enter?

  • How to perform Multiple Selection Cut & Paste

    Posted by cyoung35 on 07/26/2004 09:39am

    I am trying to figure out how to cut and paste a group (or block) of cells within the MSFlexGrid. Any Ideas?

    • msflexgrid

      Posted by ramshankar1985 on 08/20/2007 07:04am

      how to store data in database from msflexgrid using VB. Please give the sample coding

    • Cut&Paste

      Posted by Doctor Luz on 08/04/2004 03:36am

      You can show a cut/paste menu reacting to the OnMouseDown event. The message handler has a parameter "short Button" =1 for Left button =2 for right button. You can get the range of selected cells with GetCol() GetRow() GetCellSel() GetRowSel() The two first gives you a corner of selected cells and the two last gives you the opposite corner. You only need to call GetTextMatrix in order to obtain the cell's text and store them in some structure or array or the clipboard. To paste you only have to follow the inverse procedure. I hope this can help you

  • Problems with using MSFLEXGRID

    Posted by malets on 06/16/2004 02:15pm

    Problems with using MSFLEXGRID where Visual C++ is not installed. How to resolve problems?(MSFLEXGRID i have register.) After i have install data access component from Visual Studio - all is ok. Can i use MSFLEXGRID without any update?

    • Design time license

      Posted by CrystalHunter on 05/05/2005 03:30pm

      If you're trying to use MSFlexGrid in a Visual C++ project (or any MS dev tool), you will need the design time license, which you got with the data access components. However, if you're using it on a non-development machine you only need to register it as Doc says.

    • register msflxgrd.ocx

      Posted by Doctor Luz on 07/05/2004 10:44am

      There is no need for installing Visual Studio, you can use MSFLEXGRID in any computer only registering it. Put/copy the msflxgrd.ocx file in your SYSTEM directory and register it. For example the following command line should do it: regsrv32 msflxgrd.ocx

  • Copy /paste doesn't work in editable ms flex grid control

    Posted by bhaskarg on 04/12/2004 04:11pm

    I have an application with 3 property pages. In one of the property sheet I have grid control. I am able to do everything else. But Copy/Paste doesn't work thru Ctrl+C and Ctrl+V. It works through the Right mouse click "Copy and Paste".

    • Are you editing the cell?

      Posted by Doctor Luz on 05/11/2004 07:08am

      Ctrl + C and Ctrl + V works fine if you are editing the cell. (this is, when the edit control has the focus) If you want to do it without editing the cell you can easily modify OnKeyDownFlex()function in order to check if CTRL+V or CTRL+C have been pressed.

  • Thanks...

    Posted by Legacy on 01/29/2004 12:00am

    Originally posted by: ENavarre

    Your implementation of this control is remarkable; it is easy to integrate to a Visual C++ project and its look is really impressive. We save a lot of time and understood everything about TWIPS, which made us crazy.

    Thanks a lot.
    4 Students from ENSIACET (Toulouse, France).

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • This paper introduces IBM Java on the IBM PowerLinux 7R2 server and describes IBM's implementation of the Java platform, which includes IBM's Java Virtual Machine and development toolkit.

  • Targeted attacks and advanced threats are customized to infiltrate your unique IT infrastructure, evade conventional defenses, and remain hidden while stealing your corporate data. To detect these criminal intrusions, analysts and security experts agree that organizations should deploy advanced threat protection as part of an expanded security monitoring strategy. For this comparative analysis of breach detection systems, product analysis reports and comparative analysis reports are used to create the security …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds