CString and sorting question
Greetings to all,
I'm working on a small project, basically a "Phone book", as part of my
academic obligations (I was bitching a year ago about that, some may
remember :)).
In the meanwhile, I obtained some MFC knowledge, and here are my humble
questions:
a) that "phone book" is designed as a list of single strings that have
categories divided by tab character
http://slike.hr/slike/mfcwork01_476fb.png.html
void CPhonesListBox::NewEntry()
{
CEditDialog dlg;
if (dlg.DoModal () == IDOK) {
CString strItem = dlg.m_strName + _T ("\t")
+ dlg.m_strSurName + _T ("\t")
+ dlg.m_strCity + _T ("\t")
+ dlg.m_strAddress + _T ("\t")
+ dlg.m_strPhone;
AddString (strItem);
}
SetFocus ();
}
That is, by design, I suppose, totally stupid. What would be the better
and comprehensible to beginner version?
In respect to my design, I'm having problem with editing. This is a
dialog:
http://slike.hr/slike/mfcwork02_863bc.png.html
Dialog is the same, both for adding new entries and editing the current
ones.
Here's my first problem - please take look at code:
void CPhonesListBox::OnEditItem()
{
CEditDialog dlg;
CString strItem, tmp;
int nIndex = GetCurSel ();
GetText (nIndex, strItem);
int nPos = strItem.Find (_T ('\t'));
dlg.m_strName = strItem.Left (nPos);
tmp = strItem.Mid (dlg.m_strName.GetLength());
nPos = tmp.ReverseFind (_T ('\t'));
dlg.m_strPhone = tmp.Mid (nPos);
dlg.m_strPhone.TrimLeft (_T("\t"));
tmp.TrimLeft (dlg.m_strName);
tmp.TrimRight (dlg.m_strPhone);
nPos = tmp.Find (_T ('\t'));
dlg.m_strSurName = tmp.Left (nPos);
nPos = tmp.ReverseFind (_T ('\t'));
dlg.m_strAddress = tmp.Mid (nPos);
if (dlg.DoModal () == IDOK) {
strItem = dlg.m_strName + _T ("\t")
+ dlg.m_strSurName + _T ("\t")
+ dlg.m_strCity+ _T ("\t")
+ dlg.m_strAddress + _T ("\t")
+ dlg.m_strPhone;
DeleteString (nIndex);
AddString (strItem);
}
SetFocus ();
}
The code isn't finished (since it doesn't work as I'd like it to do), but
my idea was to find tab character and then get the part of the string.
However, in this part of code:
nPos = tmp.Find (_T ('\t'));
dlg.m_strSurName = tmp.Left (nPos);
nPos = tmp.ReverseFind (_T ('\t'));
dlg.m_strAddress = tmp.Mid (nPos);
I simply get 0 as return value of Find(), as there's no '\t' any longer.
What would be the best way to do what I want? Is there some sort of
'explode' function that would split a string by string?
b) Is there a way to sort data easily?
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
if (!m_wndView.Create(WS_CHILD | WS_VISIBLE | LBS_USETABSTOPS |
LBS_SORT | LBS_NOTIFY | LBS_NOINTEGRALHEIGHT, CRect(0, 0, 0, 0),
this, AFX_IDW_PANE_FIRST))
return -1;
return 0;
}
LBS_SORT automatically sorts by name, but could it be possible to sort by
other criteria? Would that require significant changes to program?
Thanks in advance!
--
"If you lie to the compiler,
it will get its revenge."
Henry Spencer
2.718281828459045235360287471352662497757247093699959574966967627.com