1

Ok this might seem a pretty simple question but I'm new to working with MFC.

My task is simple, I dragged dropped a Tree Control and now I want to populate some data in it.I've followed some online examples like in the link below

http://www.functionx.com/visualc/controls/treectrl.htm

there's no build errors in the code but when I run the code I get the error Debug Assertion Failed.

Can anyone help me solving this issue or Provide some basic tutorial or online help of populating data into a Tree Control.

Omar Iqbal
  • 81
  • 1
  • 2
  • 8
  • 1
    That Debug Assertion will point you to a filename, a line number, and usually has an expression shown in it. What are they? – Roger Lipscombe Sep 03 '15 at 16:08
  • 1
    Program: C:\Windows\SYSTEM32\mfc120ud.dll File: f:\dd\vctools\vc7libs\ship\atlmfc\include\afxcmn.inl Line:271 – Omar Iqbal Sep 03 '15 at 16:44

1 Answers1

2

In the example referenced above, TreeView is created manually using p_TreeView->CreateWindow(...)

However this is not needed when using drag and drop in resource editor. Dialog class only needs a reference to the tree control which is already created.

Declare in CMyDialog class:

class CMyDialog : public CDialogEx
{
    ...
    CTreeCtrl m_TreeView;
    void DoDataExchange(CDataExchange* pDX);
};

Add this to *.cpp file:

void CMyDialog::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_TREE1, m_TreeView);
}

Now you can use m_TreeView, for example:

m_TreeView.InsertItem("Test");
HTREEITEM level_1 = m_TreeView.InsertItem("level 1");
m_TreeView.InsertItem("level 2 a", level_1);
m_TreeView.InsertItem("level 2 b", level_1);
HTREEITEM level_2_c = m_TreeView.InsertItem("level 2 c", level_1);
m_TreeView.InsertItem("level 3 c", level_2_c);

m_TreeView.Expand(level_1, TVM_EXPAND);
Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77
  • Getting the following build error Error 1 error LNK2019: unresolved external symbol "public: __thiscall CMFCApplicationDlg::CMFCApplicationDlg(class CWnd *)" (??0CMFCApplicationDlg@@QAE@PAVCWnd@@@Z) referenced in function "public: virtual int __thiscall CMFCApplicationApp::InitInstance(void)" (?InitInstance@CMFCApplicationApp@@UAEHXZ) C:\Users\omar.iqbal\Desktop\Shared\Adobe CS6\Adobe Illustrator CS6 SDK\samplecode\MFCApplication\MFCApplication.obj MFCApplication – Omar Iqbal Sep 03 '15 at 17:06
  • OK I created a new project and followed your steps and it worked. Thanks :) Can you kindly tell me how to specify the parent node and child node data – Omar Iqbal Sep 03 '15 at 17:21
  • I added some example, see also https://msdn.microsoft.com/en-us/library/ettyybhw.aspx – Barmak Shemirani Sep 03 '15 at 17:54
  • 1
    Thanx a lot for the help Barmak I'd like to add just a little thing that in InsertItem before the string you also have to add 'L' e.g. : m_TreeView.InsertItem(L"Test"); or else it gives the incompatible type error. – Omar Iqbal Sep 03 '15 at 18:14
  • You are right. I was using ANSI strings for non-unicode version, but that's outdated. It should have the `L` prefix for any up to date project. – Barmak Shemirani Sep 04 '15 at 00:33