AlwaysLearningNewStuff
AlwaysLearningNewStuff

Reputation: 3031

Safest method to determine if all nodes in treeview are deselected?

I need to deselect all selected nodes in a treeview control.

Searching through the MSDN documentation I found TreeView_SelectItem macro.

The documentation states:

If the hitem parameter is NULL, the control is set to have no selected item.

This solves the first part of my problem -> deselecting all nodes.

My question is about the following part I have read in the Return value and Remarks sections for TVM_GETNEXTITEM message:

Return value

Returns the handle to the item if successful. For most cases, the message returns a NULL value to indicate an error. See the Remarks section for details.

Remarks

This message will return NULL if the item being retrieved is the root node of the tree. For example, if you use this message with the TVGN_PARENT flag on a first-level child of the tree view's root node, the message will return NULL.

After deselecting all nodes using TreeView_SelectItem, which macro/message can I use to check if any item is selected?

I must be 100% sure that returned result is not a result of an error.

Upvotes: 1

Views: 256

Answers (2)

Remy Lebeau
Remy Lebeau

Reputation: 597016

TreeView_SelectItem() has its own return value:

Return value

Returns TRUE if successful, or FALSE otherwise.

If TreeView_SelectItem(hwndTV, NULL) returns TRUE, you are guaranteed to not have any items selected at that moment in time. If you were to then check the TreeView for see if any items were selected and happened to find one, that would mean that someone was allowed to select an item after you had unselected them. So just make sure your code does not allow new selections until you are ready for them.

Upvotes: 1

xMRi
xMRi

Reputation: 15375

Use

HTREEITEM hItem = (HTREEITEM)::SendMessage(hWnd, TVM_GETNEXTITEM, TVGN_CARET, 0);

An "error" cannot occur in this combination of parameters.

About the documentation: From my point of view it isn't an error to ask a root node if there is a parent. It simply has none so it returns NULL. An error means here "The function return NULL because you are asking for something that can't exist..." But even than you get a result you can deal with.

I can think of no error for this function that might prevent to return an invalid result here, if the window exists and the system isn't already compromised by invalid pointer writes or similar.

Upvotes: 1

Related Questions