Reputation: 967
When I double-click an unchecked node in a WinForms TreeView
, it visually goes to checked followed by unchecked.
However only one AfterCheck
event is fired as detected by the following event handler:
private void treeView1_AfterCheck( object sender, TreeViewEventArgs e )
{
System.Diagnostics.Debug.WriteLine( "{0} {1}: {2}", e.Node.Checked, e.Node, e.Action );
}
e.Node.Checked
is true
even though the visual representation in the GUI is unchecked.
Clicking the checkbox again raises an AfterCheck
event with Node.Checked
equal to false
. The checkbox remains unchecked on the GUI.
Vice-versa for double-clicking a checked node.
I'm compiling for .NET 4.0 with Visual Studio 2010 and running on .NET 4.5.1.
Any way to work around this?
Upvotes: 2
Views: 673
Reputation: 125197
When you double click on check boxes in treeview, not only it makes problem in after check event, also it makes problem in your next mouse down and if you click anywhere else that the treeview itself, your next mouse click will be lost.
Also mentioned by Hans Passant in an old post : The Vista version of the native Windows control induced a bug in the TreeView wrapper. It automatically checks an item without generating a notification message that the wrapper can detect to raise the BeforeCheck and AfterCheck event.
I can confirm that the problem doesn't exists in Windows XP, but exists in 7 and 8.1.
To solve the problem, you can hanlde WM_LBUTTONDBLCLK
message and check if double click is on check box, neglect it:
public class ExTreeView : TreeView
{
private const int WM_LBUTTONDBLCLK = 0x0203;
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_LBUTTONDBLCLK)
{
var info = this.HitTest(PointToClient(Cursor.Position));
if (info.Location == TreeViewHitTestLocations.StateImage)
{
m.Result = IntPtr.Zero;
return;
}
}
base.WndProc(ref m);
}
}
Upvotes: 2