简体   繁体   中英

MFC C++ Populate data in Tree Control

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM