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
- You simply need to include this class’ header file in your project and
declare a CGrid object. - Define an HWND hGrid variable in your view class.
- From the Flex Grid click and key press events just call the
m_grid equivalent functions as follows:
CGrid m_grid;
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.
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); }
Downloads
Download demo application – 16 Kb
Download demo source – 31 Kb