Home > Software engineering >  Remove the problem in the List Ctrl
Remove the problem in the List Ctrl

Time:09-28



As shown in figure when click the add button to add some information, now want to delete, the code is as follows:
CListCtrl * pList=(CListCtrl *) GetDlgItem (IDC_LIST);
if (! PList - & gt; GetSelectedCount ())
{
AfxMessageBox (_T (" please select a line to delete "));
return;
}
The POSITION pos=pList - & gt; GetFirstSelectedItemPosition ();
While (pos)
{
Int nSel=pList - & gt; GetNextSelectedItem (pos);
Cstrings STR=pList - & gt; GetItemText (nSel, 0);
If (IDNO==AfxMessageBox (_T (" sure you want to delete ") + STR + _T ("?" ), MB_YESNO))
continue;
PList - & gt; Called DeleteItem (nSel);
}



Here working definition is type int



After several elected, choose delete, there is a problem: to delete the selected item, after analysis found that when deleting the first pos point to the next position, but at the moment the first selected has been deleted, cause a dislocation of the pos point position, the second delete is not supposed to delete, delete problem,

This should be how to solve? Thank you for your answer,

CodePudding user response:

After analysis, my thoughts began to delete the last, but don't know how to modify the code

CodePudding user response:

After thinking found in fact because of remove the front line in pos to appear problem, so just let the pos points to the current actual location
Modify the code below
CListCtrl * pList=(CListCtrl *) GetDlgItem (IDC_LIST);
if (! PList - & gt; GetSelectedCount ())
{
AfxMessageBox (_T (" please select a line to delete "));
return;
}
The POSITION pos=pList - & gt; GetFirstSelectedItemPosition ();
While (pos)
{
Int nSel=pList - & gt; GetNextSelectedItem (pos);
Cstrings STR=pList - & gt; GetItemText (nSel, 0);
If (IDNO==AfxMessageBox (_T (" sure you want to delete ") + STR + _T ("?" ), MB_YESNO))
continue;
PList - & gt; Called DeleteItem (nSel);
Pos=pList - & gt; GetFirstSelectedItemPosition ();

}

CodePudding user response:

CodePudding user response:

Pos=pList - & gt; GetFirstSelectedItemPosition (); good

CodePudding user response:

Code function at the end of the day is not others to help you see or interpretation or comment; But by myself calm down and take a long enough time and energy to do it yourself step or set a breakpoint or step to perform to a certain intermediate result shows or written to the log file analysis step by step,
Remind: cow x teacher cannot replace the student understand and use the toilet!
Single step debugging and set breakpoint debugging (VS IDE compilation connection through later, press F10 or F11 key step, press Shift + F11 exit the current function; In a press F9 set breakpoints after press F5 execution stops at the breakpoint,) is one of the programmers must master the skills,
  • Related