c# - Winforms: Determine if tree view background clicked -
i making winforms
app user can add nodes tree view right clicking on node or background. if user clicks node, new node should become child of node, otherwise added root of tree view.
my problem there no function check if background clicked. below have far. unfortunately, if node clicked right child added both root , parent node.
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 node, add root, otherwise add parent if (parent == null) treeview.nodes.add(node); else parent.nodes.add(node) node.parent.expand(); }
move code mouseclick
mouseup
(when user releases mouse button). check mouse coordinates there node on location. take @ code:
private void treeview1_mouseup(object sender, mouseeventargs e) { var clickednode = treeview1.getnodeat(e.x, e.y); if (clickednode == null) { //clicked on background addchild(null); } else { //clicked on node addchild(clickednode); } }
Comments
Post a Comment