[Implementation] Do an editable table control.
[Use control] A MSFLEXGRID table control, a text control, a drop-down list control.
[Principle] I like to call this method as visual illusion. Since the MSFlexGrid control itself does not support direct editing. So you need to combine text controls or drop-down list controls, so that the operation of the table control is as direct editing.
[Implementation step]
1. When the mouse click on a table of the table control, first determine the properties of the column, is the direct editing, or use the drop-down list.
2. Display hidden text controls or drop-down list controls, the displayed position and size are exactly the same size as the selected grouped position, which can overwrite
3. Fill in the contents of the selected Grid or in the drop-down list control
4. After the modification is completed, fill in the new content in the selected Grid and hide the text control or drop-down list control.
[Main procedures and descriptions]
Variable definitions:
CCOMBOBOX M_CHANGECOMBO; - Drop-drop list control, is not visible for initial
Cedit m_change; --------------- Text control, is not visible for initial
CSRING M_SCHANGE; ---------------- The string associated with the text control
CMSFLEXGRID M_FLEXGRID; ----- Table Control
Block:
Form Click on the event: After selecting a certain, you will display the appropriate text control or list control.
Void cchartinfoeditdlg :: OnClickMSFLEXGRID ()
{
// Click the invalid area, return
Long Lrow = m_flexgrid.getrowsel (); // Get Click on the line number
Long lcol = m_flexgrid.getcolsel (); // Get clicks
If (lrow> m_sattrinfo.attrnum) // If you have more than the maximum line number, click is invalid.
Return;
IF (lrow == 0) // If you click on the title line, it is invalid.
Return;
//
CRECT RECT;
m_flexgrid.getWindowRect (Rect); // Get window rectangle of the table control
ScreenToClient (Rect); // Convert to a client area rectangle
The length of the function of the // msflexGrid control is "Twips",
// Need to convert it into pixels, 1440 缇 = 1 inches
CDC * PDC = Getdc ();
/ / Calculate the proportion of the conversion of pixel points and
INT NTWIPSPERDOTX = 1440 / PDC-> getDeviceCaps (LogPixelsx);
INT NTWIPSPERDOTY = 1440 / PDC-> GetDeviceCaps (Logpixelsy);
/ / Calculate the coordinates of the upper left corner of the selected grid (pixel)
Long y = m_flexgrid.getrowpos (lrow) / NTWIPSPERDOTY
Long x = m_flexgrid.getcolpos (lcol) / ntwipsperdotX;
/ / Calculate the size of the selected grid (pixel). Plus 1 is actual debugging, found that the effect is better after 1
Long width = m_flexgrid.getColwidth (LCOL) / NTWIPSPERDOTX 1;
Long Height = m_flexgrid.getrowheight (lrow) / NTWIPSPERDOTY 1; // Form a rectangular area selected
CRECT RC (X, Y, X Width, Y Height);
/ / Convert to the coordinates of relative dialog
Rc.offsetRect (Rect.Left 1, Rect.top 1);
/ / Clear the content of the drop-down list
m_changecombo.ResetContent ();
// The following uses text controls, which list is used to determine the decision of the drop-down list control. If you use a drop-down list control, you will add data to the drop-down list control, otherwise it is empty.
..................................
..................................
// Get the selected text information
CString strval = m_flexgrid.getTextMatrix (LROW, LCOL);
INT Num = m_changecombo.getcount ();
// If there is data in the drop-down list control, it means using a drop-down list control to perform data selection.
IF (Num! = 0)
{
m_changecombo.showwindow (sw_show); // Display control
m_changecombo.movewindow (rc); // Move to the selected location, overwrite
m_changecombo.selectstring (-1, strvalue); // Content is selected. Convenient direct modification
m_changecombo.setfocus (); // Get focus
Updatedata (FALSE);
Return;
}
//
m_change.showwindow (sw_show); // Display control
m_change.setwindowText (Strvalue); // Displays text
m_change.setfocus (); // Get focus
m_change.setsel (0, -1); //
m_change.movewindow (rc); // Move to the location of the selected Grid, overwrite
}
After the text editing is completed, the carriage return will fill in the new text information in the selected
Void cchartinfoeditdlg :: ONKILLFOCUSEDITCHANGE ()
{
Updatedata (TRUE);
m_flexgrid.settext (m_schange); // Set text information
m_change.showwindow (sw_hide); // Hide text control
Updatedata (FALSE);
}
After the drop-down list is completed, fill a new new record in the selected
Void cchartinfoeditdlg :: ONKILLFOCUSCHANGECOMBO ()
{
Updatedata (TRUE);
CString Str;
m_changecombo.getWindowText (STR);
m_flexgrid.settext (STR);
m_changecombo.showwindow (sw_hide);
Updatedata (FALSE);
}
[Summary] This is a relatively simple program. If there are many columns in the form, the properties are different, and you may try to edit the information with more complex controls. You can also do multiple text controls and drop-down list controls at the same time, to correspond to different information requirements of different columns (such as possible different format requirements, so you can do a good job in various style controls and correspondence).