【发布时间】:2017-08-18 08:44:32
【问题描述】:
我正在制作一个winforms 应用程序,用户可以通过右键单击节点或背景将节点添加到树视图中。如果用户点击一个节点,新节点应该成为该节点的子节点,否则将被添加到树视图的根目录。
我的问题是没有检查背景是否被点击的功能。以下是我到目前为止所拥有的。不幸的是,如果现在单击一个节点,那么子节点将被添加到根节点和父节点。
private void treeView_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right) addChild(null);
}
private void treeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (e.Button == MouseButtons.Right) addChild(e.Node);
}
private void addChild(TreeNode parent)
{
TreeNode node = new TreeNode("new node");
// If didn't click on a node, add to root, otherwise add to parent
if (parent == null) treeView.Nodes.Add(node);
else parent.Nodes.Add(node)
node.Parent.Expand();
}
【问题讨论】: