Read online ordering many ways , did not understand , tragedy beginners , then broke on their own , please correct me .
ClistCtrl line with a control structure , but that structure is difficult to understand , see dazzling . Many also do not understand , on his own wrote a structure , the whole row of data is written in the structure ,
Data exchange two structure is much simpler .
typedef struct
{
wchar_t id [6]; // Number
wchar_t name [10]; // Name
wchar_t sex [2]; // Gender
int age; // Age
wchar_t fenlei [6]; // Classification
wchar_t tel [12]; // telephone
} MyTongXiLu;
[Cpp]
Write two functions , the structure is written to the specified line , a data structure is written in the specified row from
// write
void CMFCApp_CFile_testDlg::SetItemInofToxiluJG(int selIndex,myTongXiLu* toxilu)
{
CString str;
m_list.SetItemText(selIndex, 0, LPCTSTR(toxilu->id));
m_list.SetItemText(selIndex, 1, toxilu->name);
m_list.SetItemText(selIndex, 2, toxilu->sex);
str.Format(_T("%d"), toxilu->age);
m_list.SetItemText(selIndex, 3, str);
m_list.SetItemText(selIndex, 4, toxilu->fenlei);
m_list.SetItemText(selIndex, 5, toxilu->tel);
}
// read data
myTongXiLu CMFCApp_CFile_testDlg::GetToxiluJGInofItem(int selIndex)
{
CString str;
myTongXiLu toxilu;
_tcscpy_s(toxilu.id, m_list.GetItemText(selIndex, 0));
_tcscpy_s(toxilu.name, m_list.GetItemText(selIndex, 1));
_tcscpy_s(toxilu.sex, m_list.GetItemText(selIndex, 2));
str = m_list.GetItemText(selIndex, 3);
if (!str.IsEmpty() ||str.SpanIncluding(_T("0123456789"))==str)
toxilu.age = _ttoi(str);
else
{
toxilu.age = 0;
}
_tcscpy_s(toxilu.fenlei, m_list.GetItemText(selIndex, 4));
_tcscpy_s(toxilu.tel, m_list.GetItemText(selIndex, 5));
return toxilu;
}
The next step is to exchange data , and some even converted digital , I would not bother converted , all based on the string to compare ,
rowInt: the number of rows to be compared , the original did not make this argument , every time all the comparison , so then get hold limit the number of rows , compared the line will no longer compare , in descending order .
ColInt: as compared to the index of the column
bol: it is true for the ascending order , for the flase descending ,
void CMFCApp_CFile_testDlg::CListCtrlShort(int rowInt,int ColInt, bool bol /*= true*/)
{
CListCtrl* pList = (CListCtrl*)GetDlgItem(IDC_LIST1);
myTongXiLu toxi1, toxi2;
CString str1, str2;
int n = 0;
for (int i = 0; i<rowInt-1; i++)
{
toxi1 = GetToxiluJGInofItem(i);
toxi2 GetToxiluJGInofItem = (i + 1);
str1 = pList->GetItemText(i, ColInt);
str2 = pList->GetItemText(i + 1, ColInt);
n = StrCmpW(str1, str2);
if (bol = true) // determination is ascending or descending , as a true ascending
{
if (n>0)
{
/*toxiT = toxi1;
toxi1 = toxi2;
toxi2 = toxiT;*/
SetItemInofToxiluJG(i, &toxi2);
SetItemInofToxiluJG(i + 1, &toxi1);
}
}
else
{
if (n < 0)
{
SetItemInofToxiluJG(i, &toxi2);
SetItemInofToxiluJG(i + 1, &toxi1);
}
}
}
}
// click on the column header events
void CMFCApp_CFile_testDlg::OnHdnItemclickList1(NMHDR *pNMHDR, LRESULT *pResult)
{
Director * = 0;
int colInt;
NMLISTVIEW* pListView = (NMLISTVIEW*)pNMHDR;
if (-1 != pListView->iItem)
{
colInt = pListView-> iItem; // get the index column of mouse clicks
/*CString str;
str.Format(_T("item: %d, subitem: %d"), pListView->iItem, pListView->iSubItem);
AfxMessageBox(str);*/
}
// loop comparison , the largest put last , in descending order
for (int j=m_list.GetItemCount();j>=1;j--)
{
CListCtrlShort(j,colInt,true);
}
}
You're done , go after learning its own structure .