-
September 25th, 2012, 02:38 AM
#16
Re: display data of text file in list view
[ continued ]
Step 2: Read from text file and fill CDemoDocument::m_arrLines.
One common place to load the document is the overridden CDocument::Serialize.
However, this is not the best place in our case. We can get a CFile*pointer from CArchive argument or the file name. First is no use, because we need CStdioFile* (derived from CFile*). Also, we cannot use the file name because further we canget a share violation.
So better let's override CDocument::OnOpenDocument and simply call _ReadTextFile, similar to the method from that FAQ mentioned earlier.
Code:
class CDemoDocument : public CDocument
{
// ...
// Attributes
private:
CStringArray m_arrLines;
// Operations
public:
CStringArray& GetDocumentData() {return m_arrLines;}
// ...
// ...
// Overrides
virtual BOOL OnOpenDocument(LPCTSTR lpszPathName);
// Implementation
private:
void _ReadTextFile(LPCTSTR pszFileName, CStringArray& arrLines);
};
Code:
BOOL CDemoDocument::OnOpenDocument(LPCTSTR lpszPathName)
{
if (!CDocument::OnOpenDocument(lpszPathName))
return FALSE;
_ReadTextFile(lpszPathName, m_arrLines);
return TRUE;
}
void CDemoDocument::_ReadTextFile(LPCTSTR pszFileName, CStringArray& arrLines)
{
arrLines.RemoveAll();
CString strLine;
TRY
{
CStdioFile file(pszFileName, CFile::modeRead);
while(file.ReadString(strLine))
arrLines.Add(strLine);
}
CATCH_ALL(e)
{
e->ReportError(); // shows what's going wrong
}
END_CATCH_ALL
}
[ to be continued ]
Last edited by ovidiucucu; September 25th, 2012 at 02:45 AM.
Reason: typos
-
September 25th, 2012, 03:08 AM
#17
Re: display data of text file in list view
[ continued ]
Step 3: Filling the list.
When a file has been open, the MFC document-view (SDI/MDI) framework automatically calls the virtual method CView::OnUpdate.
Bingo! Here is the right place to fill the list.
What we have to do is to override OnUpdate, get data from the document which is an array of strings, tokenize each string and finally fill thelist. Like a walking in the park.
Here is the code copleting my example:
Code:
class CDemoListView : public CListView
{
//...
// Generated message map functions
protected:
DECLARE_MESSAGE_MAP()
virtual void OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint);
// Implementation
private:
int _GetListColumnCount() const;
void _ClearList();
void _FillList();
};
Code:
void CDemoListView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
{
_ASSERT((GetStyle() & LVS_TYPEMASK) == LVS_REPORT); // set report style, dude!
_ClearList();
_FillList();
}
Code:
void CDemoListView::_ClearList()
{
CListCtrl& listCtrl = GetListCtrl();
// delete all list items
listCtrl.DeleteAllItems();
// delete all list columns
CHeaderCtrl* pHeader = listCtrl.GetHeaderCtrl();
const int nColCount = _GetListColumnCount();
if(nColCount > 0)
{
for(int nCol = 0; nCol < nColCount; nCol++)
{
pHeader->DeleteItem(0);
}
}
}
Code:
void CDemoListView::_FillList()
{
CListCtrl& listCtrl = GetListCtrl();
// get the document data
CDemoDocument* pDoc = GetDocument();
CStringArray& arrLines = pDoc->GetDocumentData();
// fill the list
const int nLineCount = (int)arrLines.GetCount();
for(int nLine = 0; nLine < nLineCount; nLine++)
{
// add an emtpty item
listCtrl.InsertItem(nLine, NULL);
// now, let's tokenize the line and fill sub-items
CString strLine = arrLines.GetAt(nLine);
CString strToken, strCaption;
int nTokenCount = 0; // count tokens
int nCurPos = 0; // used in tokenize
// first token
strToken = strLine.Tokenize(_T(" "), nCurPos);
while(!strToken.IsEmpty())
{
++nTokenCount;
const int nCol = nTokenCount - 1;
const int nColCount = _GetListColumnCount();
if(nTokenCount > nColCount)
{
// more tokens than colums, so add a new column
strCaption.Format(_T("Column %d"), nCol);
listCtrl.InsertColumn(nCol, strCaption, LVCFMT_LEFT, 100, nCol);
}
// set subitem text
listCtrl.SetItemText(nLine, nCol, strToken);
// next token
strToken = strLine.Tokenize(_T(" "), nCurPos);
}
}
}
Code:
int CDemoListView::_GetListColumnCount() const
{
CListCtrl& listCtrl = GetListCtrl();
int nColCount = 0;
// count the header items in order to get the number of colums
CHeaderCtrl* pHeader = listCtrl.GetHeaderCtrl();
if(NULL != pHeader)
{
nColCount = pHeader->GetItemCount();
}
return nColCount;
}
Final notes:
- This is just a brief example. Although it works, you have to test, complete and solve bugs if any.
- If something is going wrong or is not enough clear, please do not hesitate to ask!
- You owe me a couple of beers (virtual, of course).
Last edited by ovidiucucu; September 25th, 2012 at 05:52 AM.
-
September 26th, 2012, 02:50 PM
#18
Re: display data of text file in list view
thankyou for your help, small and may be last question e.g there is object named sol of type solutions im calling updateAllViews and passing this object, doing type cast that object, after which it automatically goes to onupdate of left view and ryt view, but im stuck with ryt view i dont know how to give that data to onselchnge function that data has to be passed to onupdate ryt? then how should i send it to onselchnge , actually i dont know how to use onupdate of list view with onselchange of left view, kindly try to reply soon its urgent
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|