I am wondering how the TreeNode property NextVisibleNode is defined semantically. I noticed that it does not correspond with a TreeNode‘s IsVisible property.
Consider a Windows.Forms.Form with a TreeView holding more TreeNodes than it can display in it’s client area (i.e. the scroll bar appears).
When trying to iterate only over those TreeNodes that are visible in the client area with the following code…
int numVisible = 0;
for (TreeNode node = treeView.TopNode;
node != null;
node = node.NextVisibleNode)
{
// Do something...
++numVisible;
}
…actually all TreeNodes below the first visible TreeNode are counted. Although TopNode works as expected, one has to extend the for‘s condition to be node != null && node.IsVisible in order to iterate over the correct subset of TreeNodes.
Is this behaviour of NextVisibleNode intended or possibly due to a bug?
Looks like it is either “as designed” or a bug. But sounds like your approach of checking
IsVisibleis the way to go:What does TreeNode.NextVisibleNode have to return
Perhaps the
NextVisibleNoderefers to expansion state (i.e. child nodes that aren’t expanded don’t count as visible), but not the actualIsVisibleproperty. That’s the behavior I’m seeing with my limited testing. And thats the only thing that seems to differentiate it fromNextNode