C# TreeView 連續點選 不觸發AfterCheck事件
https://blog.csdn.net/hanjun0612/article/details/65632720
建立一個類 TreeView2
namespace System.Windows.Forms
{
public class TreeView2 : TreeView
{
protected override void WndProc(ref Message m)
{
if (m.Msg != 0x203)
{
base.WndProc(ref m);
}
}
}
}
然後前臺放置一個TreeView控制元件。名叫:treeView1
後臺程式碼:
private void Form3_Load(object sender, EventArgs e)
{
//測試資料
TreeNode t = new TreeNode();
t.Text = "所有許可權";
TreeNode t1 = new TreeNode();
t1.Text = "會員消費";
t.Nodes.Add(t1);
for (int i = 0; i < 100; i++)
{
TreeNode t2 = new TreeNode();
t2.Text = "Test" + i;
t1.Nodes.Add(t2);
}
this.treeView1.CheckBoxes = true;
this.treeView1.Nodes.Add(t);
}
private void CheckAllChildNodes(TreeNode treeNode, bool nodeChecked)
{
foreach (TreeNode node in treeNode.Nodes)
{
node.Checked = nodeChecked;
if (node.Nodes.Count > 0)
{
this.CheckAllChildNodes(node, nodeChecked);
}
}
}
private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
if (e.Action != TreeViewAction.Unknown)
{
if (e.Node.Nodes.Count > 0)
{
this.CheckAllChildNodes(e.Node, e.Node.Checked);
}
}
}
最後,將Form3.Designer.cs 檔案中的 TreeView 全部替換成我們的 TreeView2
---------------------
作者:正怒月神
來源:CSDN
原文:https://blog.csdn.net/hanjun0612/article/details/65632720
版權宣告:本文為博主原創文章,轉載請附上博文連結!