Click here to Skip to main content
13,248,295 members (44,163 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


14 bookmarked
Posted 8 Jun 2010

Placing ComboBox in MSFlexGrid Cell

, 8 Jun 2010
Rate this:
Please Sign up or sign in to vote.
How to edit the MSFlexGrid cell?

Sample Application



How to edit the MSFlexGrid cell?


The idea is simple. When you want to edit the text in a cell, you only need to retrieve the cell's rectangle (that is Top, Bottom, and Left) and place a Combobox control over it. The combobox control will manage all the keyboard input. When the editing is finished, the text in the Combobox control is written into the cell, replacing the previous text. Then, the Combobox is hidden until you want to edit another cell again.

Step 1


Select the Dialog Based option in the AppWizard and accept all the default setting.

Step 2

Create a new dialog resource with IDD_COMBO_DLG. Choose resource from Visual C++'s Insert Menu. The Insert Resource dialog appears. Click on Dialog, and then click New. Visual C++ creates a new dialog resource, as shown here.


Step 3

Choose Add To Project from Visual C++'s Project menu, choose Components And Controls from the submenu. Select Registered Activex Controls, and then choose Microsoft FlexGrid Control, Version 6.0 (SP6). Visual C++ will generate the wrapper class for the control. And add the Combo Box from the control palette.


Step 4

Use ClassWizard to add data member to the CflexGridComboclass. Click on the member variables tab, and then add the data members as shown:

Control IDS Type Member
IDC_COMBO CcomboBox m_cCombo


Step 5

Use Class Wizard to map the WM_INITDIALOG and event handler functions for Edit Control EN_KILLFOCUS and MSFlexGrid control Click.

Step 6

The OnInitDialog member function needs to set a column width and assign the value for MSFlexGrid. With the Help of SetColWidth(index, width) function, I used to set width of the each column in the MSFlexGrid and initialize, to set text for fixed Row in the MSFlexGrid using the function SetTextMatrix(Rowindex, Colindex, string). Combobox is hidden until you want to edit the cell.

BOOL CFlexGridCombo::OnInitDialog() 
          // TODO: Add extra initialization here
          m_FlexGrid.SetColWidth( 0, 1500);
          m_FlexGrid.SetColWidth( 1, 1500);
          m_FlexGrid.SetColWidth( 2, 1500);

          m_FlexGrid.SetTextMatrix( 0, 0, "Name");
          m_FlexGrid.SetTextMatrix( 0, 1, "Computer Science");
          m_FlexGrid.SetTextMatrix( 0, 2, "Mathematics");
          m_FlexGrid.SetTextMatrix( 0, 3, "Status");

          return TRUE; // return TRUE unless you set the focus to a control
                       // EXCEPTION: OCX Property Pages should return FALSE

Step 7

Edit the file FlexCombo.h. Add the following public member function:

void DisplayControl(CRect &rectData);

Step 8

In the DisplayControl function, with the help of the CMSFlexGrid Object, I used to get the WindowRect. Using GetCellLeft(), GetCellWidth(), GetCellTop() and GetCellHeight() function, the MSFlexGrid cell left, width and Height are easily found out and assigned to the Crect variable, rectData.bottom,, rectData.left and rectData.right.

void CFlexGridCombo :: DisplayControl(CRect &rectData)
           CMSFlexGrid *pGridData = 0;

           pGridData = &m_FlexGrid;
           CRect rectWnd;
           int nleft = rectWnd.left+(pGridData->GetCellLeft()/15); 
           int ntop =>GetCellTop()/15);  //Finding the Cell Height
							// & width 
           int nright = pGridData->GetCellWidth()/15;
           int nbottom = pGridData->GetCellHeight()/15;           
           rectData.bottom = nbottom;
  = ntop;
           rectData.left = nleft;
           rectData.right = nright;           

Step 9

When the particular column of the cell is clicked. First, retrieve the cell's rectangle (that is Left, Right, Top and Bottom) using the function DisplayControl. With help of the AddString function, all the strings are added in the ComboBox and ComboBox control displayed in its current state. Using the MoveWindow function, the ComboBox control is placed in the particular cell of the FlexGrid.

void CFlexGridCombo::OnClickMsflex() 
           // TODO: Add your control notification handler code here
           CRect rectCntrl;
           CString mPrevText;
           CMSFlexGrid *pGridData = 0;
           int iCol = m_FlexGrid.GetColSel();
           CString str;

           pGridData = &m_FlexGrid;

           //Moving the Edit Box to the Particular Cell
           if( iCol == 0)
                       m_cCombo.AddString("Ganesh Mangal");
                       m_cCombo.AddString("Java Senthil");
           if( ( iCol == 1 ) || ( iCol == 2) )
                       for( int i=0; i<=100; i++ )
           if( iCol == 3 )


Step 10

Whenever Combobox control loses focus, the KillFocus event is triggered and using SetTextMatrix function, value are placed in the corresponding cell and then, the ComboBox box is hidden until you want to edit another cell again.

void CFlexGridCombo::OnKillfocusCombo() 
          // TODO: Add your control notification handler code here
          CString strCombo;
          int iRow, iCol;
          iRow = m_FlexGrid.GetRowSel();
          iCol = m_FlexGrid.GetColSel();
          m_cCombo.GetWindowText( strCombo );
          m_FlexGrid.SetTextMatrix( iRow, iCol, strCombo );

Step 11

Build and test the application.


I hope my control helps you to edit MSFlexGrid. All friends of CodeProject can use my ActiveX if it is useful.

If you have any ideas, I hope to know and if you find any problems, you can tell me.

Thanks to CodeProject and to all.


  • 5th June, 2010: Initial version


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
India India
I am working in R&D department. In programming, I'm very much interested in OOPS concepts and I have written a book on "Programming in C++". My hobbies are playing cricket, painting and I have started a Educational trust "Illuminate Educational Trust".

You may also be interested in...

Comments and Discussions

Questioncombobox is not displaying properly Pin
ssanug4-Jun-14 21:26
memberssanug4-Jun-14 21:26 
GeneralSelecting many cells and editing is causing a bug. Pin
SharanyaMahi22-Dec-11 0:03
memberSharanyaMahi22-Dec-11 0:03 
GeneralRe: Selecting many cells and editing is causing a bug. Pin
shanmugarajaa22-Dec-11 0:29
membershanmugarajaa22-Dec-11 0:29 
GeneralRe: Selecting many cells and editing is causing a bug. Pin
SharanyaMahi22-Dec-11 18:33
memberSharanyaMahi22-Dec-11 18:33 
GeneralRe: Selecting many cells and editing is causing a bug. Pin
shanmugarajaa22-Dec-11 18:58
membershanmugarajaa22-Dec-11 18:58 
BugOn scrolling the combobox is not getting hidden Pin
SharanyaMahi17-Dec-11 6:54
memberSharanyaMahi17-Dec-11 6:54 
GeneralRe: On scrolling the combobox is not getting hidden Pin
shanmugarajaa17-Dec-11 21:41
membershanmugarajaa17-Dec-11 21:41 
GeneralRe: On scrolling the combobox is not getting hidden Pin
SharanyaMahi21-Dec-11 1:43
memberSharanyaMahi21-Dec-11 1:43 
GeneralRe: On scrolling the combobox is not getting hidden Pin
shanmugarajaa21-Dec-11 2:19
membershanmugarajaa21-Dec-11 2:19 
QuestionCFlexGridCombo class Pin
abbydee23-Feb-11 15:39
memberabbydee23-Feb-11 15:39 
QuestionMagical "15": what is it? Pin
Victor Nijegorodov15-Jun-10 1:01
memberVictor Nijegorodov15-Jun-10 1:01 
GeneralNested MSFlexGrid cells Pin
Member 304759314-Jun-10 23:06
memberMember 304759314-Jun-10 23:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171114.1 | Last Updated 8 Jun 2010
Article Copyright 2010 by shanmugarajaa
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid