A problem was found when Treeview was used last week. Assume that the Treeview contains multiple root nodes and child nodes. For example, you have registered Treeview In the instance constructor or before using it for the first time. afterselect event, this. treeview1.afterselect + = new system. windows. forms. treevieweventhandler (this. treeviewmediaafterselect); event processing is as follows: Program Used to calculate the number of event programs running. Private int I = 0; private void treeview1_afterselect (Object sender, system. windows. forms. treevieweventargs e) {I ++; this. label1.text = I. tostring ();} (1 ). when you click [Node 1] for the first time, I = 1; this is no problem. If you click another node for the first time, such as [Node 2] or [node 1.1], then I = 2, that is, two afterselect events are generated when a node is selected. Code Perform one-step debugging to find the E. node. the text value is "Node 1", that is, the first afterselect event is generated by [Node 1] (this node is not clicked here), and the second event is the expected event. (2 ). to solve this problem, I tried to click the expand/collapse icon (+/-) in front of the node for the first time, no matter whether the clicked node is the first root node or not, all events are executed, which is equivalent to selecting the first root node. This is also true when this. treeview1.select () is called for the first time. For (1), I tested it on. NET Framework 2.0 beta2 and no two events occurred. Is this a bug? For (2) I just want to remind myself that the afterselect event will also be triggered when nodes are not directly selected. The test results in. NET Framework 2.0 beta2 are the same.
If you want to write highly efficient programs, I think it is a matter of consideration to trigger repeated events. For the above phenomenon, assume that a Treeview is placed on the left of the form to indicate the directory, and a DataGrid on the right to list the products corresponding to the directory based on the selection on the left, is it annoying to trigger two consecutive events, especially when the data volume is large.