c# - treeview node is highlighted even i did not right click on the node -


i working on winform , on ui there treeview, found treenode highlighted did not click on node right mouse (eg, node1 highlighted when click on following position), not behavior because want show different context menu when did not click on treenode

+rootnode

  |_ node1                [ right click here, node1 highlighted]   |   |_ node2                [ right click here, node2 highlighted] 

going off of comment kevin wienhold's answer, want allow user click in empty space of treeview , deselect selected node.

you can handling mousedown event of treeview control, , setting selectednode property null if mouse clicked on location not contain node. example, use following code:

private void mytreeview_mousedown(object sender, system.windows.forms.mouseeventargs e) {     if (mytreeview.hittest(e.location).node == null)     {         mytreeview.selectednode = null;     } } 

this takes advantage of hittest method determine node located @ particular point, specifying location of mouse event point test. don't need other code select nodes usual when user does click on them; handled automatically treeview.


edit: comment question indicates, i'm still exceptionally unclear trying accomplish here. if you're interested in preventing node being temporarily highlighted while hold down right mouse button in empty space side of node, things little more complex.

i've looked problem before, , tricky part window messages not received while mouse button being held down, @ least not until mouse moved (in case, node no longer selected anyway). behavior apparently dictated operating system , not overridden using standard .net-provided events. can try cancel click of right button in mousedown event day long, node being selected windows before event ever raised in control (remember .net-provided controls treeview , listview wrappers around same controls provided windows api, apparently implements "select-node-while-right-button-held-down" behavior itself).

what does work, however, overriding wndproc in derived treeview control, , handling wm_rbuttondown message. notice setting selectednode property null doesn't work here, because that's not processed until after windows automatically selects node response right mouse button being clicked—no matter do, have prevent base treeview control receiving wm_rbuttondown message. so, have couple of choices in how handle this:

  1. you can cancel right-click message bailing out return statement. of course, means you're not going able handle event in mousedown handler, because it's never passed control! if want show pop-up context menu, won't work you.

    public class newtreeview : system.windows.forms.treeview {     protected override void wndproc(ref system.windows.forms.message m)     {         const int wm_rbuttondown = 0x204;         if (m.msg == wm_rbuttondown)         {             return;         }         base.wndproc(ref m);     }    } 
  2. you can show context menu in overridden wndproc method response wm_rbuttondown message, , return method without allowing base class handle message. exact same thing first solution (prevents right-click event causing node appear selected), allow show context menu (or else want) whenever right-click occurs. of course, mean of relevant code have contained within subclass of treeview control, not handled in form's ui code, may or may not convenient you.

    public class newtreeview : system.windows.forms.treeview {     protected override void wndproc(ref system.windows.forms.message m)     {         const int wm_rbuttondown = 0x204;         if (m.msg == wm_rbuttondown)         {             //create , show context menu             var mycontextmenu = new contextmenustrip();             mycontextmenu.items.add("first item");             mycontextmenu.items.add("second item");             return;         }         base.wndproc(ref m);     }    } 
  3. you raise own rightmouseclick event custom treeview class response wm_rbuttondown message, handle wish form's ui code. not passing wm_rbuttondown message base treeview control class, accomplishes same goal previous 2 suggestions, allows handle right-button click event in form's ui code instead of having put of logic subclassed control's wndproc.

    public class newtreeview : system.windows.forms.treeview {     protected override void wndproc(ref system.windows.forms.message m)     {         const int wm_rbuttondown = 0x204;         if (m.msg == wm_rbuttondown)         {             //raise custom event             onrightmouseclick(new eventargs());             return;         }         base.wndproc(ref m);     }    } 

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -