Editable Flex Grid (without an Edit control)
Posted
by Rafat Sarosh
on February 9th, 2000
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 (); }
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 KbDownload demo source - 31 Kb
Comments
Editable Flexgrid
Posted by jaiguru on 11/05/2008 04:46amNice article sir. We like to share our download - Flexgrid component with source, with you to all programmers of this community. with regards, wbcsoftwarelab
ReplyMagic Numbers
Posted by dhn on 10/22/2005 06:44pmPleaseHelpme..emergency!!
Posted by Legacy on 10/17/2003 12:00amOriginally posted by: shailesh Kanzariya
Edit Box Control
Posted by Legacy on 04/04/2003 12:00amOriginally posted by: GayRek
I want the edit box control to accept only numbers, special characters like [, ., ], *. I want to suppress all other characters. How can I program that in VC++ environment.
Thanks
ReplyHow to display a picture in a cell of FlexGrid?
Posted by Legacy on 11/21/2002 12:00amOriginally posted by: Eric
Help!How to display a bitmap, or together with text, on a cell of FlexGrid?
ReplyThanks!
How to add any charater in FixedColumn
Posted by Legacy on 10/24/2002 12:00amOriginally posted by: racuca
I added 1 FixedRow and 1 FixedColumn in MSFlexgrid Control
And each title in FixedRow using setFormatString function.
But, I don't know how to add each title in FixedCol.
Please, let me know that.
thanks.
Where did you get the msflexgrid.h & msflexgrid.cpp?
Posted by Legacy on 08/23/2002 12:00amOriginally posted by: win
Did those files in your project are your own creation or they are existing somewhere?
Reply
Help in getting values from each cell !!!
Posted by Legacy on 02/18/2002 12:00amOriginally posted by: Nitin Bhatnagar
I need to extract values contained in each cell at run time from this editable flex grid control. Can you tell me how can I retrieve value of each cell in MSFlexGrid control?
Thanx,
ReplyNitin
How to insert a blank row in the grid
Posted by Legacy on 08/28/2001 12:00amOriginally posted by: CandyAppleDan
Hi,
Is it possible to insert a blank row between rows. I have been attempting to do this but have not achieved a desired result.
Many Thanks
ReplyDan Escott.
MultiSelection with a flexgrid?
Posted by Legacy on 07/30/2001 12:00amOriginally posted by: Carl Austin
Is there anyway that you can return an array of all of the row numbers that are selected at the same time?
ReplyLoading, Please Wait ...