Editable Flex Grid (without an Edit control) | CodeGuru

Editable Flex Grid (without an Edit control)

Editable Flex Grid: How to edit Flex grid? Indeed there is a demo project in MSDN for VB. But there is no example for VC. There is no one to one conversion of the source from VB to VC. Even if you convert the VB project to VC, you are still left with some of […]

Written By
CodeGuru Staff
CodeGuru Staff
Feb 9, 2000
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Editable Flex Grid: How to edit Flex grid?


Indeed there is a demo project in MSDN for VB. But there is no


example for VC. There is no one to one conversion of the source from


VB to VC. Even if you convert the VB project to VC, you are still left


with some of the magic numbers to map correctly edit box to the grid


cell. For my different views I have to always twick those magic numbers


to get edit box in sync with the flexgrid cells. At the end of the article


I shown the edit box approach also. If some body knows a way to eliminate


those magic numbers, please let me know.

I used a different approach than using an Edit control. With this approach, I am
trapping the keys and manipulating them which gives user a feeling as
he is editing the grid cell directly.

This approach is very easy to use as I put all the functionality in a single
class called CGrid.

Using the CGrid Class


  1. You simply need to include this class’ header file in your project and
    declare a CGrid object.

  2. CGrid m_grid;
    

  3. Define an HWND hGrid variable in your view class.
  4. From the Flex Grid click and key press events just call the
    m_grid equivalent functions as follows:
  5. void CFlexGridView::OnClickMsflexgrid1()
    {
     m_grid.OnClickGrid ();
    }
    
    void CFlexGridView::OnKeyPressMsflexgrid1(short FAR* KeyAscii)
    {
     m_grid.OnKeyPressGrid ( KeyAscii);
    }
    
    BOOL CFlexGridView::PreTranslateMessage(MSG* pMsg)
    {
     if (pMsg->message == WM_KEYDOWN
     && (pMsg->hwnd == hGrid  ) ) {
    
      switch (pMsg->wParam ) {
    
       case VK_UP :
       m_grid.GoUp ();
       return TRUE;
    
       case VK_DOWN:
       m_grid.GoDown();
       return TRUE;
    
       case VK_LEFT :
       m_grid.GoLeft();
       return TRUE;
    
       case VK_RIGHT:
       m_grid.GoRight();
       return TRUE;
      }
     }
    
     return CFormView::PreTranslateMessage(pMsg);
    }
    
    void CFlexGridView::OnInitialUpdate()
    {
     CFormView::OnInitialUpdate();
     CWnd * hwnd = GetDlgItem (IDC_MSFLEXGRID1);
     hGrid = hwnd->GetSafeHwnd ();
    }
    

Thats all for making flex grid editiable . Enjoy.

Advertisement

Part 2

Following is the code which is a translation of the MSDN VB code example to Visual C++.

void CGridEdit::MoveEditBox ()
{
 HDC hDC;
 hDC = ::GetDC (NULL);

 SetMapMode (hDC, MM_TWIPS);

 RECT rec;
 m_grid.GetWindowRect (&rec);

 int x,y,h,w;
 x =  XTwipsToPixels (rec.left)
   + XTwipsToPixels ( m_grid.GetCellLeft () );

 y =  YTwipsToPixels (rec.top )
   +  YTwipsToPixels( m_grid.GetCellTop ()  );

 h = (int) XTwipsToPixels (m_grid.GetCellHeight() );
 w = (int) YTwipsToPixels(m_grid.GetCellWidth () );

 //MAGIC NUMBERS ????
 y = y + 148;
 x = x + 27;

 m_edit.MoveWindow  (x ,y,w,h ,TRUE); //has to be in pixels
 m_edit.BringWindowToTop ();

 m_edit.SetFocus ();

 TransferValue (FALSE); //get the value in Editbox
}

Utility Functions

BOOL CGridEdit::TransferValue(BOOL ToGrid)
{
 CString sText;

 if (ToGrid) { //Transfer value to Grid from Edit Box
  GetDlgItemText (IDC_EDITBOX,sText);
  m_grid.SetText( sText );

 } else {
  sText = m_grid.GetText ();
  SetDlgItemText (IDC_EDITBOX,sText);
 }

 return TRUE;
}

short lpx = 0;
short lpy = 0;

// called once to get constants
void GetDeviceConstants(void)
{
 HDC hdc = GetDC(NULL);

 //pixel in one logical inch
 lpx = GetDeviceCaps(hdc, LOGPIXELSX);
 lpy = GetDeviceCaps(hdc, LOGPIXELSY);

 ReleaseDC(NULL, hdc);
}

long XTwipsToPixels(long twips)
{
 if (!lpx)
  GetDeviceConstants();

 return MulDiv(lpx, (int)twips, 1440);
 // Total pixel in Inch = Total pixel in twips/1440
}

long YTwipsToPixels(long twips)
{
 if (!lpy)
  GetDeviceConstants();

 return MulDiv(lpy, (int)twips, 1440);
}

long XPixelsToTwips(long pixels)
{
 if (!lpx)
  GetDeviceConstants();

 return MulDiv(pixels, 1440, lpx);
}

long YPixelsToTwips(long pixels)
{
 if (!lpx) GetDeviceConstants();
  return MulDiv(pixels, 1440, lpy);
}
Advertisement

Downloads

Download demo application – 16 Kb
Download demo source – 31 Kb

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.