How to modify a student's information by right clicking the submenu??? And dynamically modify the data in the file.
That is, double-click the list box to modify the data
resolvent
1. Double click the function nm using the message response of the list control_ Dblclk, message response of edit box control, double click function NM_KILLFOCUS
2. Add control variable names for list controls and edit controls to facilitate calling the functions of these controls when realizing function functions
3. Add nm using the class wizard_ Dblclk message response (i.e. double click), M_KILLFOCUS message response function, and add implementation functions in the function, that is, what effect will be produced after double click.
4. Function implementation of list control:
Get the number of the selected row and column to determine the position.
void CMyBrowseDlg::OnDblclkListView(NMHDR* pNMHDR, LRESULT* pResult)//Generated when the mouse double clicks a list view control { LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR); // TODO: add control notification handler code here CRect rc; CString strTemp; NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;//This structure is used to store information about the notification message of the list view. Most notification messages of the list view will be accompanied by a pointer to this structure m_Row = pNMListView->iItem;//Line number m_Col = pNMListView->iSubItem;//Column number if (pNMListView->iItem == -1) //Select the blank space, add a row, and set the focus to the last row and the second column { m_Row = m_allstudent.GetItemCount(); strTemp.Format(_T("%d"), m_Row + 1); m_allstudent.InsertItem(m_Row, strTemp); m_allstudent.SetItemState(m_Row, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED); m_allstudent.EnsureVisible(m_Row, FALSE); //m_Col = 1; } if (m_Col != 0) // Select children { m_allstudent.GetSubItemRect(m_Row, m_Col, LVIR_LABEL, rc); m_EditUpdate.SetParent(&m_allstudent); m_EditUpdate.MoveWindow(rc); m_EditUpdate.SetWindowTextA(m_allstudent.GetItemText(m_Row, m_Col)); m_EditUpdate.ShowWindow(SW_SHOW); m_EditUpdate.SetFocus();//Set Edit focus m_EditUpdate.ShowCaret();//Display cursor m_EditUpdate.SetSel(0, -1);//Select all } *pResult = 0; } 5,Function implementation of edit box control: void CMyBrowseDlg::OnEnKillfocusEdit1() { // TODO: add control notification handler code here CString str; m_EditUpdate.GetWindowTextA(str); m_allstudent.SetItemText(m_Row, m_Col, str); m_EditUpdate.ShowWindow(SW_HIDE); }
How to dynamically modify the data in the file???
First get the modified data on the list control, and the M set above_ Row and m_Col can locate the modified data.
Next, think about what function to use to get the data on the list??? Exactly, the newly written data has been saved in the function implementation of the edit control.
How to replace the data in the file with the new data???
Core principles:
1. First, take all the objects from the file and put them into a CStringArray array. Use the GetAt function to obtain each array object. Take out the selected line to modify the value. If other lines remain unchanged, they are still stored in a CStringArray array.
2. The selected row should be separated into multiple small attributes with spaces and stored in CString or a CStringArray array. When the modified column of data is encountered, the original data cannot be used, and the new data should be replaced with the old data. In this way, data modification is dynamically realized.
3. Finally, input the extracted array into the file, with special emphasis. When you encounter the selected line, input the modified line, and you're done.
void CMyBrowseDlg::OnEnKillfocusEdit1() { // TODO: add control notification handler code here CString str;//This string is the modified data, which can be used to replace the data in the file below m_EditUpdate.GetWindowTextA(str);//Get the data on the edit control and pass it to str m_allstudent.SetItemText(m_Row, m_Col, str);//Sets the data for the specified row and column on the list control m_EditUpdate.ShowWindow(SW_HIDE);// Hide the window and pass the active state to other windows. //Dynamically modify the data in the file //Get the modified data and replace it below //*****1. Fetch data CStringArray stusave; CString temp; int n = 0; //Read out the file data and input it. The key point is not to input the nItem data CStdioFile fin; CString FileName = _T("A:\\student.txt"); fin.Open(FileName, CFile::modeRead); //Read string line by line while (fin.ReadString(temp))//The contents read line by line are stored in the parameters {//There is a program bug in the Reading function here. You must type a space in the file and delete it. The read data is the whole line, or only the data in front of the first space stusave.Add(temp); temp = ' '; n++; } fin.Close(); //*****2. Enter the modified data CStdioFile fout;//Select modeCreate to clear the file data first, and then write without writing the deleted line of data to realize the dynamic deletion function fout.Open(FileName, CFile::modeCreate | CFile::modeWrite); stusave.GetAt(m_Row).GetAt(m_Col); CStringArray saveone; //saveone.Add(stusave.GetAt(m_Row));// Take out the modified row and split the small attributes /*****Take out the selected line, split it, and save each small attribute to the character array saveone******/ //Process the selected row and replace the old value int nPos = 0; int nPre_pos = 0; int count = 0; while (-1 != nPos) {//A loop can check a small attribute nPre_pos = nPos; nPos = stusave[m_Row].Find(' ', (nPos + 1)); //Start from (nPos + 1) to find a space, and return to the space position when it is found if (count == m_Col) { saveone.Add(" "+str);//A space must be added for the written data to be separated by a space count++; } else { saveone.Add(stusave[m_Row].Mid(nPre_pos, (nPos - nPre_pos))); count++; } } //Start entering new data for (int i = 0; i < n; i++) { if (i == m_Row) { fout.WriteString(saveone[0]+saveone[1] +saveone[2] + saveone[3] + saveone[4] + saveone[5] + saveone[6] + saveone[7] + " \n"); continue; } fout.WriteString(stusave.GetAt(i) + "\n");//GetAt(i) gets the data at the specified position i, which is equivalent to the array subscript [] } fout.Close(); }