Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. I want to create a treeview with my structure/class variable as nodes and update the same with custom values

I want to create a treeview with my structure/class variable as nodes and update the same with custom values

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestionannouncement
11 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Shibu Krishnan

    I have some structures in my application (which is pretty big). To test certain error conditions, I have to input wrong values to these variables and send across another system (using udp sockets). How can I use the treeview to populate all the structures and upon user selection of one of the node (the struct variable), i wants to input error values and update the same structure variable in my app. Here is the sample

    Quote:

    hRoot = m_TreeView.InsertItem(_T("Messages"), 0, 1); hMainMsg = m_TreeView.InsertItem("XXXXX", 0, 1, hRoot); hSubMsg = m_TreeView.InsertItem("MsgHdr", 2, 3, hMainMsg); m_TreeView.InsertItem(_T("msgId"), 4, 5, hSubMsg); m_TreeView.InsertItem(_T("InfId"), 4, 5, hSubMsg); m_TreeView.InsertItem(_T("Seq No"), 4, 5, hSubMsg); m_TreeView.InsertItem(_T("msgLen"), 4, 5, hSubMsg); m_TreeView.InsertItem(_T("timeStamp"), 4, 5, hSubMsg);

    in double click event,

    HTREEITEM nodSelected = m_TreeView.GetSelectedItem();
    CString strSelected = m_TreeView.GetItemText(nodSelected);

    HTREEITEM hParent = nodSelected;
    
    while (hParent = m\_TreeView.GetParentItem(hParent))
    {
    	
    	CString strParent    = m\_TreeView.GetItemText(hParent);
    	
    	if (strcmp(strParent,"Messages")==0)
    		break;
    
    	m\_TreeView.SetItemState(hParent, TVIS\_BOLD, TVIS\_BOLD);		
    
    	strSelected = strParent + "." + strSelected;
    }
    
    m\_txtMsg = strSelected + "=";
    

    for ex. m_txtMsg will be now "XXXXX.msgHdr.msgId=" Is there any way to use this text data to assign as my variable Please help.

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #2

    You can only set the content of a structure by referring to it by address (aka pointer/reference). So rather than adding the item as text to each tree node, you should create a structure item, populate it with the data, and save its address in the node.

    S 1 Reply Last reply
    0
    • L Lost User

      You can only set the content of a structure by referring to it by address (aka pointer/reference). So rather than adding the item as text to each tree node, you should create a structure item, populate it with the data, and save its address in the node.

      S Offline
      S Offline
      Shibu Krishnan
      wrote on last edited by
      #3

      Thank you for the answer. I checked the methods to insertitem to treeview. (

      Quote:

      HTREEITEM InsertItem( LPTVINSERTSTRUCT lpInsertStruct ); HTREEITEM InsertItem(UINT nMask, LPCTSTR lpszItem, int nImage, int nSelectedImage, UINT nState, UINT nStateMask, LPARAM lParam, HTREEITEM hParent, HTREEITEM hInsertAfter ) etc..

      So how can I save address of a variable in the node? Can u please share the code segment for the same or any link to the same.

      L 1 Reply Last reply
      0
      • S Shibu Krishnan

        Thank you for the answer. I checked the methods to insertitem to treeview. (

        Quote:

        HTREEITEM InsertItem( LPTVINSERTSTRUCT lpInsertStruct ); HTREEITEM InsertItem(UINT nMask, LPCTSTR lpszItem, int nImage, int nSelectedImage, UINT nState, UINT nStateMask, LPARAM lParam, HTREEITEM hParent, HTREEITEM hInsertAfter ) etc..

        So how can I save address of a variable in the node? Can u please share the code segment for the same or any link to the same.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #4

        Use the lParam field.

        S 1 Reply Last reply
        0
        • L Lost User

          Use the lParam field.

          S Offline
          S Offline
          Shibu Krishnan
          wrote on last edited by
          #5

          Yes, I used the SetItemData() and GetItemData() methods with lparam. I am passing the (DWORD). But the variables are of different data types. the value has to be changed for the selected variable. I have lots of fields in the struct and 8 structures are there in the tree. Compare and set values for each fields will be cumbersome. Can you give proper guidance. thank you

          L 1 Reply Last reply
          0
          • S Shibu Krishnan

            Yes, I used the SetItemData() and GetItemData() methods with lparam. I am passing the (DWORD). But the variables are of different data types. the value has to be changed for the selected variable. I have lots of fields in the struct and 8 structures are there in the tree. Compare and set values for each fields will be cumbersome. Can you give proper guidance. thank you

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #6

            Sorry, I don't understand what you are asking, or what you mean by "I am passing the (DWORD)". You should create your structures, populate them with the relevant data, and then take their address as a pointer, which you send in the lParam field. You then have direct access to each structure in the relevant tree node.

            S 1 Reply Last reply
            0
            • L Lost User

              Sorry, I don't understand what you are asking, or what you mean by "I am passing the (DWORD)". You should create your structures, populate them with the relevant data, and then take their address as a pointer, which you send in the lParam field. You then have direct access to each structure in the relevant tree node.

              S Offline
              S Offline
              Shibu Krishnan
              wrote on last edited by
              #7

              Sorry to post you incomplete question. I mean the address of the variable I am passing in the lParam. But again in my GetDataItem() method, i have to typecast to the corresponding variable. is it? for eg.

              typedef struct address
              {
              CString houseName;
              CString placeName;
              int cnrtyCode;
              char pinCode[6];
              };

              typedef struct node
              {
              CString Name;
              int Age;
              float Height;
              address adrs;
              };

              this is my structure. and

              HTREEITEM hItems, hRoot;
              TVINSERTSTRUCT tvInsert;

              pNode->Name = "Shibu";
              pNode->adrs.cnrtyCode = 91;
              pNode->adrs.houseName = "House No 23";
              pNode->adrs.placeName = "Bnglr";
              pNode->Height = 5.5;	
              pNode->Age = 28;
              
              hRoot	 = m\_tree.InsertItem(\_T("Personal Data"), 0, 1);
              tvInsert.hParent = hRoot;
              tvInsert.hInsertAfter = NULL;
              tvInsert.item.mask = TVIF\_TEXT | TVIF\_PARAM;	
              tvInsert.item.pszText = "Name";
              hItems = m\_tree.InsertItem(&tvInsert);	
              m\_tree.SetItemData(hItems, DWORD(&pNode->Name)); 
              

              .......
              .....
              tvInsert.item.pszText = "Age";
              hItems = m_tree.InsertItem(&tvInsert);
              m_tree.SetItemData(hItems, DWORD(&pNode->Age));

              Now my requirement is when i select the treeitem, associated value has to be displayed in a label. Also i have to change the value (through text box entry ; one text box only) of the selected item. Each value ( name,age etc.) are of different data types. first i tried by passing address of pNode. then how to check my selection (whether user selected name/age/..). Please help me with a code portion.

              L 1 Reply Last reply
              0
              • S Shibu Krishnan

                Sorry to post you incomplete question. I mean the address of the variable I am passing in the lParam. But again in my GetDataItem() method, i have to typecast to the corresponding variable. is it? for eg.

                typedef struct address
                {
                CString houseName;
                CString placeName;
                int cnrtyCode;
                char pinCode[6];
                };

                typedef struct node
                {
                CString Name;
                int Age;
                float Height;
                address adrs;
                };

                this is my structure. and

                HTREEITEM hItems, hRoot;
                TVINSERTSTRUCT tvInsert;

                pNode->Name = "Shibu";
                pNode->adrs.cnrtyCode = 91;
                pNode->adrs.houseName = "House No 23";
                pNode->adrs.placeName = "Bnglr";
                pNode->Height = 5.5;	
                pNode->Age = 28;
                
                hRoot	 = m\_tree.InsertItem(\_T("Personal Data"), 0, 1);
                tvInsert.hParent = hRoot;
                tvInsert.hInsertAfter = NULL;
                tvInsert.item.mask = TVIF\_TEXT | TVIF\_PARAM;	
                tvInsert.item.pszText = "Name";
                hItems = m\_tree.InsertItem(&tvInsert);	
                m\_tree.SetItemData(hItems, DWORD(&pNode->Name)); 
                

                .......
                .....
                tvInsert.item.pszText = "Age";
                hItems = m_tree.InsertItem(&tvInsert);
                m_tree.SetItemData(hItems, DWORD(&pNode->Age));

                Now my requirement is when i select the treeitem, associated value has to be displayed in a label. Also i have to change the value (through text box entry ; one text box only) of the selected item. Each value ( name,age etc.) are of different data types. first i tried by passing address of pNode. then how to check my selection (whether user selected name/age/..). Please help me with a code portion.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #8

                I am not sure that I understand what you mean or what you are trying to do. Presumably you have some nodes that are addresses, so you should add some key as the nodename, and then a pointer to the address structure as the item data. And for the other nodes you have some other type. But I am having trouble visualising exactly why you are using a TreeView in this instance.

                S 1 Reply Last reply
                0
                • L Lost User

                  I am not sure that I understand what you mean or what you are trying to do. Presumably you have some nodes that are addresses, so you should add some key as the nodename, and then a pointer to the address structure as the item data. And for the other nodes you have some other type. But I am having trouble visualising exactly why you are using a TreeView in this instance.

                  S Offline
                  S Offline
                  Shibu Krishnan
                  wrote on last edited by
                  #9

                  As my actual structure is pretty huge as I mentioned earlier, designing a form with label & textbox for each one is difficult. So I planned to use treeview to list the items and one text box to enter value. my form is simple with 1 treeview, label and textbox. in this case,my requirement is when i select the treeitem, associated value has to be displayed in a label and change the value through text box. Hope you are clear with my struct.

                  L 1 Reply Last reply
                  0
                  • S Shibu Krishnan

                    As my actual structure is pretty huge as I mentioned earlier, designing a form with label & textbox for each one is difficult. So I planned to use treeview to list the items and one text box to enter value. my form is simple with 1 treeview, label and textbox. in this case,my requirement is when i select the treeitem, associated value has to be displayed in a label and change the value through text box. Hope you are clear with my struct.

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #10

                    That sounds quite straightforward, so I do not really understand what the problem is. If the user selects the "Name" node, then you display the name, and if it gets changed, save the new information from the textbox. But perhpas you should be looking more at your overall design.

                    S 1 Reply Last reply
                    0
                    • L Lost User

                      That sounds quite straightforward, so I do not really understand what the problem is. If the user selects the "Name" node, then you display the name, and if it gets changed, save the new information from the textbox. But perhpas you should be looking more at your overall design.

                      S Offline
                      S Offline
                      Shibu Krishnan
                      wrote on last edited by
                      #11

                      Yes exactly. So this design will not work for this requirement. is it? any how thank you for the interest and help provided.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups