EditGrid.exe: Edit Cells in MSFlexGrid ActiveX Control
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
This article was previously published under Q196833
On This Page
SUMMARY
The Microsoft Flex Grid Control (MSFlexGrid), which ships
with Visual C++ and Visual Basic, does not support editing of individual cells.
The Visual Basic Programmer's Guide has an example that shows how to
programmatically add this functionality by placing a TextBox control over the
cell to be edited, and then updating the cell programmatically.
EditGrid.exe is a sample that shows the steps needed to implement this feature
in Microsoft Visual C++ using MFC.
For
additional information about how to download Microsoft Support files, click the
following article number to view the article in the Microsoft Knowledge Base:
119591 (http://support.microsoft.com/kb/119591/EN-US/) How to Obtain Microsoft Support Files from Online Services
Microsoft scanned this file for viruses. Microsoft used the most
current virus-detection software that was available on the date that the file
was posted. The file is stored on security-enhanced servers that help to
prevent any unauthorized changes to the file.
NOTE: Use the -d option when running EditGrid.exe to
decompress the file and recreate the proper directory structure.
Use the Component Gallery to add the MSFlexGrid to your
project. This creates MFC based wrapper classes for the control.
2.
Use ClassWizard to create a new generic CWnd-derived class
named CEditGrid. After you create this class, change it so it is derived from
the MSFlexGrid class. See the REFERENCES section of this article for more
information.
3.
Use ClassWizard to create a new CEdit-derived class named
CEditWnd. Handle the WM_CHAR and WM_KEYDOWN messages:
void CEditWnd::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if (nChar != 13) // Ignore ENTER key.
CEdit::OnChar(nChar, nRepCnt, nFlags);
}
void CEditWnd::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if (nChar == 27) // Esc means "Cancel".
{
SetWindowText("");
ShowWindow(SW_HIDE);
GetParent()->SetFocus();
}
else if (nChar == 13) // Enter means "OK".
GetParent()->SetFocus();
CEdit::OnKeyDown(nChar, nRepCnt, nFlags);
}
4.
Add the following member variables to CEditGrid:
public:
CEditWnd m_edit;
long m_lBorderWidth;
long m_lBorderHeight;
int m_nLogX;
int m_nLogY;
5.
Override the PreSubclassWindow() virtual function for
CEditGrid:
void CEditGrid::PreSubclassWindow()
{
// Calculate border size.
SetRow(0);
SetCol(0);
m_lBorderWidth = GetCellLeft();
m_lBorderHeigth = GetCellTop();
// To convert grid rect from twips to DC units you need
// pixels per inch.
CDC* pDC = GetDC();
m_nLogX = pDC->GetDeviceCaps(LOGPIXELSX);
m_nLogY = pDC->GetDeviceCaps(LOGPIXELSY);
ReleaseDC(pDC);
// Create invisible edit control.
m_edit.Create(WS_CHILD|ES_MULTILINE|ES_WANTRETURN,
CRect(0,0,0,0), this, GetDlgCtrlID());
}
6.
Handle the KeyPress, DBLClick, and LeaveCell reflected
events in CEditGrid. To do this, you need to add an event map to the class:
Create an instance of the CEditGrid control in either of
the following ways:
•
Add the MSFlexGrid control to a dialog box or CFormView
using the Resource Editor. You can then subclass the control by holding down
the CTRL key and double-clicking on the control in the Resource Editor. You
must edit the .h file, and change the class from CMSFlexGrid to
CEditGrid.
•
Create the control dynamically. To do this, you need to
override the CEditGrid::Create functions to call CMSFlexGrid::Create. You also
need to call CEditGrid::SubClassWindow() to subclass it. For more information,
see the REFERENCES section of this article.
If you add the control to a dialog box or CFormView, you can
use the Resource Editor. Otherwise, you need to do this programmatically after
you create and subclass the control.
Need More Help? Contact a Support professional by E-mail, Online or Phone.
Customer Service For non-technical assistance with product purchases, subscriptions, online services, events, training courses, corporate sales, piracy issues, and more.
Newsgroups Pose a question to other users. Discussion groups and Forums about specific Microsoft products, technologies, and services.