Workaround 1:
In the Aftercheck event, the minimum time to execute a function is controlled by System.Threading.Thread.Sleep () to ensure that the function execution time must be greater than a certain value
Workaround 2:
Writing column TreeView2
class treeview2 : treeview
{
protected override void WndProc (ref Message m)
{
if (m.ms g!=0x203)
{
BASE.W Ndproc (ref m);
}
}
}
To reproduce this, start a c#.net Windows Forms application, drop a TreeView control onto the form and set the checkboxes property to True. Then add a Aftercheck event and paste in this over the FORM1 code:
Using System;
Using System.Windows.Forms;
Namespace WindowsFormsApplication1
{
public partial class Form1:form
{
Public Form1 ()
{
InitializeComponent ();
}
private void Form1_Load (object sender, EventArgs e)
{
TreeNode parent = TREEVIEW1.NODES.ADD ("parent");
parent. Nodes.Add ("Child1");
parent. Nodes.Add ("Child2");
parent. ExpandAll ();
}
private void Treeview1aftercheck (object sender, TreeViewEventArgs e)
{
if (e.action = = Treeviewaction.unknown)
return;
If the parent is clicked then set the CheckState of the children to match
if (E.node.level = = 0)
foreach (TreeNode childnode in E.node.nodes)
childnode.checked = e.node.checked;
If a child was clicked then set the parent to being checked if any children was checked otherwise unchecked
else if (E.node.level = = 1)
{
TreeNode parent = e.node.parent;
bool nonechecked = true;
foreach (TreeNode siblingnode in parent. Nodes)
if (siblingnode.checked)
nonechecked = false;
Parent. Checked =!nonechecked;
}
}
}
}
When you run the application, you'll see a form with a tree view with a Parent and a children. When you check/uncheck the Parent the children would check/uncheck. Unchecking all the children would uncheck the parent and checking either child would check the parent if it isn ' t already ch Ecked.
This works fine if you click slowly or use the keyboard.
however, if you click on the Parent twice quickly (but not quickly enough to being a double-click) then the Tr EE View doesn ' t fire the Aftercheck event the second time and goes to some weird state where the form freezes up until Y OU click on it (that's click is then ignored).
for example, if I have everything checked and click the Parent twice quickly I'll see all the checkbox Es clear and then JUST the Parent would end up checked (even though this should is impossible) . at this point Hoverin G The Form would not show where the mouse have focus and the next click'll be ignored! for Example, you can CLI CK the Close button and the form would remain open but would start to work properly again.
I tried to debug this, it looks as if the NodeMouseClick event does fire both times but the Aftercheck event only fires th E first time.
Note:this only happens when using the mouse, using the keyboard are fine, you can tap away on the space bar as fast as you Like and it always works.
Answer:
the. NET TreeView class heavily customizes mouse handling for the native Windows control on order to SYN Thesize the Before/after events. Unfortunately, they didn ' t get it quite right. When you start clicking Fast, you'll generate double-click messages. The native control responds to a double-click by toggling the checked state for the Item, without telling the. NET Wrapper about it . You won ' t get a Before/aftercheck event.
it s a bug but they won ' t fix It. The workaround isn't difficult, you'll need to prevent the native control from seeing the double-click event. ADD a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox, replacing the existing one.
using System;using System.Windows.Forms;class MyTreeView : TreeView { protected override void WndProc(ref Message m) { // Filter WM_LBUTTONDBLCLK if (m.Msg != 0x203) base.WndProc(ref m); }}
TreeView Quick Click does not perform Aftercheck time