Question

I am a newbie to visual Studio MFC stuffs. Im in a urgent need of creating a small application. I need a help im stuck with this and issue i have a text file which have the following data. A-A1,A2 where A is root node and A1 A2 are the child node. My requirememnt is i need to create a SDI MFC Application. I need like when i go to File->Open->"xxx.txt"(which contains the above data) it must be displayed in the format like A |_ A1 |_ A2 (TREE VIEW). I went through many tutorials like it needs to be tokenized etc. Really im confused ike how to proceed etc.I have done only with dailog based and im new to SDI. Any help is appreciated. Thanks in Advance.

Was it helpful?

Solution

From what I can gather from the comments to the question and the same question linked in Codeproject, I'll try to give a little help, but given how general the question is, the answer will probably not be very specific either.

First, if you want to have a tree view display, you need your view class to be a CTreeView. A CTreeView is a CView with an embedded CTreeCtrl. A CEditView is a CView with an embedded CEdit, so it's usefull to display text (like a text editor or something like that). A plain CView has no support for any special kind of content, so you have to "draw" it yourself.

Now, to show something in the view, you have to tell it to display it. Just reading the file won't do it. You have to actively show it. Usually, you'll read your data from the file into some structure, then display it from there. Or you may store your data directly in the tree, it depends. Anyway, you need to learn to use CTreeView/CTreeCtrl. Basically, use CTreeCtrl::InsertItem to add elements.

As to the tokenization, I'm not sure if I understand your format, but I think I would use different separators for root node and child node. So if you have ROOT-Child1,Child2, I would do something like:

int pos = 0;
CString strRoot = strLine.Tokenize(_T("-"), pos);

// do something with strRoot, like store it or display it in the tree

while (pos != -1)
{
    CString strChild;

    strChild = strLine.Tokenize(_T(","), pos);
    // do something with strChild, like store it or display it in the tree
}

Finally, when working with the Doc/View architecture, the way to go is to separate the data from its display. So you'd typically keep your data in your document, and do all your open/save operations there, and then access the data in the document from the view with GetDocument to display it. Sometimes, it may make sense to have live data in the view, but that's not the usual way of doing it. In such cases it may even make sense to make it a dialog based app instead.

OTHER TIPS

Basically i have worked in the CTreeCtrl in the dailog based applications but this is the first time me working in SDI. I would really thank you for providing me such a solution. I have developed a small piece of code have a look at it

void CFileView::OnFileOpen()
 { 
  // TODO: Add your command handler code here
 CString pathname,strLine; 
 CStdioFile File; 

 if(File.Open(pathname, CFile::modeRead)) // Open to read
 { 
  while(File.ReadString(strLine)) // Read file 
  { 
     int Position = 0; 
     CString Token; 
     Token = strLine.Tokenize(_T("-:,"), Position);
     HTREEITEM hRoot = m_wndFileView.InsertItem(Token, 0, 0);

     while(Token!="") 
     { 
         Token = strLine.Tokenize(_T("-:,"), Position); 
         HTREEITEM hSrc = m_wndFileView.InsertItem(Token, 0, 0, hRoot);
     } 
   } 
 }
}

Like my point is am i placing my code in the correct function? i.e where we open the file. Beacuse in the client window i am not getting anything rather a blank window. SDI is based CTreeView class. I am now clear with the tokenizer and the tree control part but now i am confused whynot i not at all getting anything in the client window :(

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top