TreeView控件在以下情况下会引发非期待中的AfterSelect事件:
- 原始状态为未选中任何结点
- TreeView内容较多,出现了竖向滚动条
- 最小化TreeView所在窗体
- 然后最大化此窗体
步骤4完成后,TreeView会自动选中屏幕上显示的第一个节点,并引发AfterSelect事件。
如果你不想处理此事件,可以检查TreeViewEventArgs.Action, 这种情况下Action的值应该是Unknown。所以微软自己在MSDN Library里对TreeViewEventArgs的示例代码就是这样的:
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
protected override void OnAfterSelect(TreeViewEventArgs e)
{
// Confirm that the user initiated the selection.
// This prevents the first node from expanding when it is
// automatically selected during the initialization of
// the TreeView control.
if (e.Action != TreeViewAction.Unknown)
{
if (e.Node.IsExpanded)
{
e.Node.Collapse();
}
else
{
e.Node.Expand();
}
}
// Remove the selection. This allows the same node to be
// clicked twice in succession to toggle the expansion state.
SelectedNode = null;
}
只是只口未提刚才我们谈到的这种奇特形为。。。