这是我要实现的功能:
像往常一样通过单击鼠标检查树节点,用户可以多选。
当用户按住 Ctrl+ 鼠标时,我希望选中鼠标单击的树节点,而未选中所有其他节点。
我有一个版本,但它并不优雅:
有什么建议么?
如果要测试代码,请将 TreeViewAdv 和 TreeNodeAdv 替换为普通的 TreeView 和 TreeNode。
using Syncfusion.Windows.Forms.Tools;
namespace treeviewtest
{
public partial class Form1 : Form
{
TreeNodeAdv CurrentNode = null;
public Form1()
{
InitializeComponent();
}
private void treeViewAdv1_AfterCheck(object sender, Syncfusion.Windows.Forms.Tools.TreeNodeAdvEventArgs e)
{
TreeViewAdv tree = sender as TreeViewAdv;
if (Control.ModifierKeys == Keys.Control
&& e.Action!= TreeViewAdvAction.Unknown)
{
foreach (TreeNodeAdv node in tree.Nodes)
{
if (node.CheckState == CheckState.Checked && node != CurrentNode)
node.Checked = false;
foreach (TreeNodeAdv n in node.WalkTree())
{
if (n.CheckState == CheckState.Checked && n != CurrentNode)
n.Checked = false;
}
}
}
CurrentNode = null;
}
private void treeViewAdv1_BeforeCheck(object sender, TreeNodeAdvBeforeCheckEventArgs e)
{
if (Control.ModifierKeys == Keys.Control && CurrentNode == null)
{
CurrentNode = e.Node;
if (e.NewCheckState == CheckState.Unchecked)
{
TreeViewAdv tree = sender as TreeViewAdv;
foreach (TreeNodeAdv node in tree.Nodes)
{
if (node.CheckState == CheckState.Checked && node != CurrentNode)
node.Checked = false;
foreach (TreeNodeAdv n in node.WalkTree())
{
if (n.CheckState == CheckState.Checked && n != CurrentNode)
n.Checked = false;
}
}
e.Cancel = true;
}
}
}
}
public static class externtree
{
public static IEnumerable<TreeNodeAdv> WalkTree(this TreeNodeAdv node)
{
if (node == null)
yield break;
// return itself first
yield return node;
// return children
if (node.HasChildren)
{
foreach (TreeNodeAdv cNode in node.Nodes)
foreach (TreeNodeAdv iNode in cNode.WalkTree())
yield return iNode;
}
}
}
}