As a modern software, it is often connected to a database, and there is also an interface between connection and convenient user operations. MFC is a good framework for developing user interfaces on Windows. However, its built-in controls do not support good controls for database tables, the DataGrid and other controls mentioned on the Internet appear on win8.1 and VS2013 platforms.
As a modern software, it is often connected to a database, and there is also an interface between connection and convenient user operations. MFC is a good framework for developing user interfaces on Windows. However, its built-in controls do not support good controls for database tables, the DataGrid and other controls mentioned on the Internet appear on win8.1 and VS2013 platforms.
As a modern software, it is often connected to a database, and there is also an interface between connection and convenient user operations. MFC is a good framework for developing user interfaces on Windows. However, its built-in controls do not support good controls for database tables, however, when using the DataGrid and other controls mentioned on the Internet on win8.1 + VS2013 platform, the problem that the control or header file cannot be found is annoying. Finally, I thought that as long as the List Control is slightly modified and the cell editing and cell drop-down List are added, it can actually be well connected to the database, one hundred degrees, and someone has already done this, it's so touching! The following describes how to implement the two functions mentioned above. Because they are for reference by others, if you want to see the original article: click here. As for the biggest difference with the original article, I think it is the process operation I can write in detail, and to maximize the simplification of the Code, finally, add some methods that I think can bring convenience to database operations. OK. Let's see how these two functions are implemented. In fact, the implementation principle is very simple, that is, when you click on the corresponding cells in the list, generate an edit box or drop-down list box with the same size as the cell at the corresponding position and paste it on the cell. The implementation is as follows: add the following variables and functions in the dialog box class declaration:
Int e_Item; // The Newly Edited row int e_SubItem; // The Newly Edited column CEdit m_Edit; // generate the bool haveeditcreate object in the unit editing box; // The void createEdit (NM_LISTVIEW * pEditCtrl, CEdit * createdit, int & Item, int & SubItem, bool & havecreat) has been created in the flag editing box ); // create the cell editing box function void distroyEdit (CListCtrl * list, CEdit * distroyedit, int & Item, int & SubItem); // destroy the cell editing box object CComboBox m_comBox; // Production Cell drop-down list object bool haveccomboboxcreate; // The Mark drop-down list box has been created void createCcombobox (NM_LISTVIEW * pEditCtrl, CComboBox * createccomboboxobj, int & Item, int & SubItem, bool & havecreat); // create a cell drop-down list box function void distroyCcombobox (CListCtrl * list, CComboBox * distroyccomboboxobj, int & Item, int & SubItem); // destroy the drop-down list box of cells
In the initialization function OnInitDialog () in the dialog box, add the initialization Code as follows:
Haveeditcreate = false; // indicates that no edit box haveccomboboxcreate = false has been created; // indicates that no drop-down list box RECT m_rect has been created; m_list.GetClientRect (& m_rect); // obtain the list's customer region to adjust the width of each column m_list.SetExtendedStyle (LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT); // set the list style, LVS_EX_GRIDLINES is the gridline (only applicable to listctrl in report style) // LVS_EX_FULLROWSELECT is the entire row highlighted (only applicable to listctrl in report style) m_list.InsertColumn (0, _ T ("student ID"), LVCFMT_LEFT, m_rect.right/4); m_list.InsertColumn (1, _ T ("name"), LVCFMT_LEFT, m_rect.right/4); m_list.InsertColumn (2, _ T ("gender"), LVCFMT_LEFT, m_rect.right/4); m_list.InsertColumn (3, _ T ("class"), LVCFMT_LEFT, m_rect.right/4); m_list.InsertItem (0, _ T ("09090901"); // Add the first student data m_list.SetItemText (0, 1, _ T ("Xiao Li"); m_list.SetItemText (0, 2, _ T ("male"); m_list.SetItemText (0, 3, _ T ("Jike 0901"); m_list.InsertItem (1, _ T ("09090902 ")); // Add the second student data m_list.SetItemText (1, 1, _ T (""); m_list.SetItemText (1, 2, _ T ("male ")); m_list.SetItemText (1, 3, _ T ("Family Planning 0902 "));
Add a click response function to the list box:
Void PIM phovisiondatabasedlg: OnNMClickListStudentdata (NMHDR * pNMHDR, LRESULT * pResult) {LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast
(PNMHDR); NM_LISTVIEW * pEditCtrl = (NM_LISTVIEW *) pNMHDR; printf ("row: % d, column: % d \ n", pEditCtrl-> iItem, pEditCtrl-> iSubItem); if (pEditCtrl-> iItem =-1) // click to non-workspace {if (haveeditcreate = true) // If an edit box is created, destroy {distroyEdit (& m_list, & m_Edit, e_Item, e_SubItem); // destroy the cell edit box object haveeditcreate = false ;} if (haveccomboboxcreate = true) // if a drop-down list box is created before, destroy {distroyCcombobox (& m_list, & m_comBox, e_Item, e_SubItem); haveccombobox Create = false ;}} else if (pEditCtrl-> iSubItem! = 2) // if it is not a gender option {if (haveccomboboxcreate = true) // if an edit box is created earlier, the {distroyCcombobox (& m_list, & m_comBox, e_Item, e_SubItem); haveccomboboxcreate = false;} if (haveeditcreate = true) {if (! (E_Item = pEditCtrl-> iItem & e_SubItem = pEditCtrl-> iSubItem) // if the cell in the vertex is not created previously {distroyEdit (& m_list, & m_Edit, e_Item, e_SubItem); haveeditcreate = false; createEdit (pEditCtrl, & m_Edit, e_Item, e_SubItem, haveeditcreate ); // create edit box} the cell in else // point is the previously created {m_Edit.SetFocus (); // set to focus} else {e_Item = pEditCtrl-> iItem; // assign the row of the cell in the vertex to the "edited row" for later processing e_SubItem = pEditCtrl-> iSubItem; // assign the row of the cell in the vertex to the "edited row" for later processing of crea TeEdit (pEditCtrl, & m_Edit, e_Item, e_SubItem, haveeditcreate); // create edit box} else // if gender is selected, generate a drop-down list item {if (haveeditcreate = true) in the cell. // if an edit box is created, destroy {distroyEdit (& m_list, & m_Edit, e_Item, e_SubItem ); haveeditcreate = false;} if (haveccomboboxcreate = true) {if (! (E_Item = pEditCtrl-> iItem & e_SubItem = pEditCtrl-> iSubItem) // if the cell in the vertex is not created {distroyCcombobox (& m_list, & m_comBox, e_Item, e_SubItem); haveccomboboxcreate = false; createCcombobox (pEditCtrl, & m_comBox, e_Item, e_SubItem, haveccomboboxcreate); // create the edit box m_comBox.AddString (L "male "); m_comBox.AddString (L ""); m_comBox.ShowDropDown (); // automatic drop-down} the cells in the else // vertex are created {m_comBox.SetFocus (); // set to focus} else {e_Item = pEditCtrl-> iItem; // assign the row of the cell in the vertex to the "edited row" for later processing e_SubItem = pEditCtrl-> iSubItem; // assign the row of the cell in the vertex to "the row you just edited" for later processing. createCcombobox (pEditCtrl, & m_comBox, e_Item, e_SubItem, haveccomboboxcreate ); // create the edit box m_comBox.AddString (L "male"); m_comBox.AddString (L "female"); m_comBox.ShowDropDown (); // automatic drop-down} * pResult = 0 ;}
Implementation of the function for creating and destroying cell edit boxes and drop-down list boxes:
Void PIM phovisiondatabasedlg: createEdit (NM_LISTVIEW * pEditCtrl, CEdit * createdit, int & Item, int & SubItem, bool & havecreat) // create the cell editing box function // pEditCtrl is the list object pointer, createdit is the edit box pointer object, // Item is the row of the created cell in the list, and SubItem is the column, havecreat creates a standard {Item = pEditCtrl-> iItem for the object; // assigns the row of the cell in the vertex to "the row you just edited" for later processing SubItem = pEditCtrl-> iSubItem; // assign the row of the cell in the vertex to the "edited row" for later processing. createdit-> Create (ES_AUTOHSCROLL | WS_CHILD | ES_LEFT | ES_WANTRETURN, CRect (0, 0, 0, 0), this, IDC_EDIT_CREATEID); // create an edit box object. IDC_EDIT_CREATEID is the Control ID 3000, and havecreat = true is defined at the beginning of the article; createdit-> SetFont (this-> GetFont (), FALSE); // set the font, if this parameter is not set, the above words will suddenly feel createdit-> SetParent (& m_list); // you can set list control as the parent window to locate the generated Edit correctly, this is also important to CRect EditRect; m_list.GetSubItemRect (e_Item, e_SubItem, LVIR_LABEL, EditRect); // obtain the spatial location information of cells EditRect. setRect (EditRect. left + 1, EditRect. top + 1, EditRect. left + m_list.GetColumnWidth (e_SubItem)-1, EditRect. bottom-1); // + 1 and-1 can prevent the edit box from blocking the gridline CString strItem = m_list.GetItemText (e_Item, e_SubItem) in the list box ); // obtain the corresponding cell character createdit-> SetWindowText (strItem); // display the cell character in the editing box createdit-> MoveWindow (& EditRect ); // place the editing box on the corresponding cell. createdit-> ShowWindow (SW_SHOW); // display the editing box on the cell. createdit-> SetFocus (); // set the focus to createdit-> SetSel (-1); // set the cursor at the end of the text box} void cgphovisiondatabasedlg: distroyEdit (CListCtrl * list, CEdit * distroyedit, int & Item, int & SubItem) {CString meditdata; distroyedit-> GetWindowTextW (meditdata); list-> SetItemText (Item, SubItem, meditdata ); // obtain the corresponding cell character distroyedit-> DestroyWindow (); // destroy the object. If an object is created, the object will be destroyed. Otherwise, an error will be reported.} void PIM phovisiondatabasedlg: createCcombobox, CComboBox * createccomboboxobj, int & Item, int & SubItem, bool & havecreat) // create a cell drop-down list box function // pEditCtrl is the list object pointer, and createccombobox is the drop-down list box pointer object, // Item is the row of the created cell in the list, SubItem is the column, and havecreat is the object creation standard {Item = pEditCtrl-> iItem; // assign the row of the cell in the vertex to the "edited row" for later processing. SubItem = pEditCtrl-> iSubItem; // assign the row of the cell in the vertex to the "edited row" for later processing havecreat = true; createccomboboxobj-> Create (WS_CHILD | WS_VISIBLE | CBS_DROPDOWN | CBS_OEMCONVERT, CRect (0, 0, 0, 0), this, IDC_COMBOX_CREATEID); createccomboboxobj-> SetFont (this-> GetFont (), FALSE); // set the font, if this parameter is not set, the above words will suddenly feel like createccomboboxobj-> SetParent (& m_list); // you can set list control as the parent window to locate the generated Ccombobox correctly, this is also important to CRect EditRect; m_list.GetSubItemRect (e_Item, e_SubItem, LVIR_LABEL, EditRect); // obtain the spatial location information of cells EditRect. setRect (EditRect. left + 1, EditRect. top + 1, EditRect. left + m_list.GetColumnWidth (e_SubItem)-1, EditRect. bottom-1); // + 1 and-1 can prevent the edit box from blocking the gridline CString strItem = m_list.GetItemText (e_Item, e_SubItem) in the list box ); // obtain the corresponding cell character createccomboboxobj-> SetWindowText (strItem); // display the cell character in the edit box createccomboboxobj-> MoveWindow (& EditRect ); // place the edit box on the corresponding cell. createccomboboxobj-> ShowWindow (SW_SHOW); // display the edit box on the cell.} void x0phovisiondatabasedlg: distroyCcombobox (CListCtrl * list, CComboBox * distroyccomboboxobj, int & Item, int & SubItem) {CString meditdata; distroyccomboboxobj-> GetWindowTextW (meditdata); list-> SetItemText (Item, SubItem, meditdata ); // update the corresponding cell character distroyccomboboxobj-> DestroyWindow (); // destroy the object. If an object is created, it must be destroyed. Otherwise, an error is returned}
OK. Now we have enabled the function of adding a cell edit box and a cell drop-down list box. Click the list cell except the Gender column to see the following results:
Click the cell in the Gender column to see the drop-down list, as shown below:
When writing something in the cell editing box, you often like to press Enter. If you do not add it for processing, you will find a very crashing phenomenon, and the program exits directly. This can be avoided by reloading the OnOK () function of the dialog box class. In the dialog box class declaration, add
afx_msg void OnOK();
Then, why not define it:
void CNephoVisionDatabaseDlg::OnOK(){}
In this way, there will be no direct exit of the program by pressing enter.
In addition, if you want to update the relevant information in the data list when the focus is lost in the edit box or drop-down list box, you can respond to the lost focus message of the two dynamically created controls. The method is as follows: in the dialog box class declaration, add
Afx_msg void OnKillfocusEdit (); // The focus response function afx_msg void OnKillfocusCcomboBox () is lost in the dynamically generated edit box; // The focus response function is lost in the dynamically generated drop-down list box.
In the message ing description of the dialog box class implementation, add
ON_EN_KILLFOCUS (IDC_EDIT_CREATEID, & Region: OnKillfocusEdit) // Add the loss focus response function ON_CBN_KILLFOCUS (IDC_COMBOX_CREATEID, & nnphovisiondatabasedlg: Release) in the dynamically generated edit box)
In this way, when the two controls lose focus, they will respond to the functions defined separately. We only need to add the processing code to the corresponding functions:
Void cnphovisiondatabasedlg: OnKillfocusEdit () {if (haveeditcreate = true) // if an edit box is created, destroy {distroyEdit (& m_list, & m_Edit, e_Item, e_SubItem ); // destroy the cell editing box object haveeditcreate = false;} if (haveccomboboxcreate = true) // if a drop-down list box is created, destroy {distroyCcombobox (& m_list, & m_comBox, e_Item, e_SubItem); haveccomboboxcreate = false ;}} void nnphovisiondatabasedlg: OnKillfocusCcomboBox () {if (haveeditcreate = true) // If an edit box is created, destroy {distroyEdit (& m_list, & m_Edit, e_Item, e_SubItem); // destroy the cell edit box object haveeditcreate = false ;} if (haveccomboboxcreate = true) // if a drop-down list box is created, {distroyCcombobox (& m_list, & m_comBox, e_Item, e_SubItem) is destroyed; haveccomboboxcreate = false ;}}