开发者

How do you make the TreeView.SelectedNode property nulled without nulling the current node in the SelectedNode?

开发者 https://www.devze.com 2023-01-30 20:32 出处:网络
If TreeView.SelectedNode = null开发者_StackOverflow is used, would it also null the node object on the SelectedNode?You appear to be confusing references and objects. You can have a null-reference (wh

If TreeView.SelectedNode = null开发者_StackOverflow is used, would it also null the node object on the SelectedNode?


You appear to be confusing references and objects. You can have a null-reference (which refers to no object at all), but there is no such thing as a null-object in C#.

If on the other hand, you are concerned that setting the SelectedNode property to null will evict the currently selected node from the TreeView, this is not the case - that node will simply be unselected.

EDIT: Perhaps you are concerned that the values of other variables that hold references to the currently-selected node will be set to null should the SelectedNode property be set to null. This is not the case either:

TreeView treeView = ...

TreeNode node = new TreeNode();    
treeView.Nodes.Add(node);

treeView.SelectedNode = node;

treeView.SelectedNode = null; 
bool isNodeNull = (node == null); // false


No. The SelectedNode property refers only to the node that is currently selected, so you can set it to null without "nulling the node object" itself. It will simply de-select whatever node is currently selected in the TreeView control.

According to the documentation:

If no TreeNode is currently selected, the SelectedNode property is Nothing.

0

精彩评论

暂无评论...
验证码 换一张
取 消