I want to create a treeview with my structure/class variable as nodes and update the same with custom values
-
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.
-
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.
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.
-
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.
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.
-
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.
-
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
-
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
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. -
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.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.
-
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.
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.
-
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.
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.
-
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.
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.
-
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.
Yes exactly. So this design will not work for this requirement. is it? any how thank you for the interest and help provided.