Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Treeview AfterCheck Event 1

Status
Not open for further replies.

Fynn

Programmer
Apr 2, 2002
5
CA
I am using a treeview control in vb.net. I need to provide functionality that allows only one node to be checked at time. I am trying to avoid iterating through all the nodes (there could potentially be quite a few) by keeping a reference to the last node checked in a static variable. When the user checks another node I basically uncheck the referenced node and set the reference to the newly checked node. I also need the ability to programatically check all children of a checked node.
I have placed some code in the AfterCheck event which, of course, fires the AfterCheck event when the previously checked node is unchecked or when another node is checked programatically. Therein lies my problem. I have tried putting the code the Click event but that does not fire until the mouse button is released. This poses a problem if the user moves the mouse off the checked node before they release the mouse button so I lose the node that was checked (I'm using the GetNodeAt method of the tree to get a reference to the affected node).
If anyone can suggest another approach or a way around this problem it would be most appreciated.

Thanks
 
Fynn,

Out of curiosity, why don't you store the TreeView Key rather than the reference. Because key will be a unique ID and you can very easily use GetNodeByKey which is faster and you don't have to iterate through the treeview to find an item. And even with your current approach try "BeforeCheck" event of the treeview. Let me know which worked for you anyway.... even I am curious to know the end result.
 
Thanks for the reply Kris11. The only problem is that I am working with the vb.net treeview control which no longer has a key property. Treeviews in .net contain collections of nodes which in turn can contain collections of nodes. Using a static variable to hold a reference to the last selected node works fine for my needs my problem is that when I uncheck that node the AfterCheck event fires again. It's the AfterCheck event (or even the BeforeCheck event) firing when I'm changing the checked property of a node programatically that I'm trying to avoid. But thanks for taking the time to reply.
 
Fynn,

Yes I remember I came across the same situation with AfterCheck and BeforeCheck and What I did was, I declared a variable something like &quot;isUserClicked&quot; and then in the clicked event of the treeview I toggled the value to True. And in &quot;AfterCheck&quot; event I checked &quot;isUserClicked&quot; and returned back if it is not a user click. And the same way just before doing a <TreeView.TopNode.Checked = True>, I made the variable to FALSE, which will prevent the &quot;AfterCheck&quot; being fired when we do the Check/UnCheck programmatically.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top