Why isn't there a SelectedNodeChanged event for Windows.Forms.TreeView?

前端 未结 4 861
深忆病人
深忆病人 2020-12-10 10:04

The System.Web.UI.WebControls.TreeView class offers this event, but the Forms version of TreeView doesn\'t. What\'s the equivalent in the Forms world? I\'m using AfterSelect

4条回答
  •  爱一瞬间的悲伤
    2020-12-10 10:39

    There's none in WinForms TreeView. To quote MSDN for TreeView.AfterSelect:

    This event does not occur when the node is unselected. To detect this occurrence, handle the Control.MouseUp event and test the TreeNode.IsSelected property.

    You'd better use TreeView.NodeMouseClick event combined with AfterSelect. AfterSelect isn't called when you select the previously selected SelectedNode. So just call AfterSelect when necessary, e.Node helps you.

    private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Node == tv.SelectedNode)
                treeView1_AfterSelect(sender, null);
        }
    

提交回复
热议问题