Windows Develop Bookmark and Share   
 index > Windows Forms General > Possible bug in .NET TreeView? TreeNode checked state inconsistent
 

Possible bug in .NET TreeView? TreeNode checked state inconsistent

(is crosspost at CodeProject.com, no replies)

I'm working with a .NET System.Windows.Forms.TreeView (both .NET 2.0 and 3.5) and have a problem with the check boxes not correctly affecting/reflecting the TreeNode's Checked state.

I've enabled checkboxes and have event handlers for BeforeCheck and AfterCheck to act whenever a TreeNode's checkbox is checked or unchecked. My problem is that if you place the mouse pointer directly on a node's checkbox and double-click, the checkbox's visible checked state toggles, but the Checked state of the corresponding TreeNode does not actually change. Further the TreeView's BeforeCheck and AfterCheck handlers are never invoked [this actually seems consistent and follows from the observation the these events are raised exactly and only when anything actually changes the TreeNode.Checked property, not the state of the visible check box]. A single(left)-click on the check box works properly: the Before/AfterCheck handlers are invoked and the TreeNode's Checked state changes correctly.

Double-clicking elsewhere on the displayed node toggles the expanded/collapsed state (as I assume it's suposed to do).

I've tried to find a TreeView event handler that will allow me to catch a double-click directly on the checkbox and either ignore it or modify the node's checked state myself. But I've been unable to find an event handle to catch this.

Is this a known bug? Could there be something I'm doing wrong? It gives my application a rather annoying incorrect behavior and I really don't want to abandon the use of TreeView and have to reimplement a correct "TreeView"!

Can anyone steer me toward any information about this behavior of TreeView?

Thanks in advance.

P.S. using C#, Studio 2008, running on Vista Ultimate 32bit. Answered below.

  • Edited byjohnk310 Monday, June 01, 2009 4:12 PM
  •  
johnk310  Sunday, May 31, 2009 6:51 PM
Yes, this is a bug in Vista. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the toolbox onto your form.

using System;
using System.Windows.Forms;

public class MyTreeView : TreeView {
protected override void WndProc(ref Message m) {
// Suppress WM_LBUTTONDBLCLK
if (m.Msg == 0x203) { m.Result = IntPtr.Zero; }
else base.WndProc(ref m);
}
}

Hans Passant.
  • Marked As Answer byjohnk310 Monday, June 01, 2009 4:04 PM
  •  
nobugz  Sunday, May 31, 2009 8:34 PM
Yes, this is a bug in Vista. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the toolbox onto your form.

using System;
using System.Windows.Forms;

public class MyTreeView : TreeView {
protected override void WndProc(ref Message m) {
// Suppress WM_LBUTTONDBLCLK
if (m.Msg == 0x203) { m.Result = IntPtr.Zero; }
else base.WndProc(ref m);
}
}

Hans Passant.
  • Marked As Answer byjohnk310 Monday, June 01, 2009 4:04 PM
  •  
nobugz  Sunday, May 31, 2009 8:34 PM
Wow! Thank you so much. I'll try it ASAP (and I'll do it on XP & 7, too). I did forget to mention I'm building, debugging, testing on Vista. But app will run on XP, 7, etc.eventually.

P.S. Fix verified on Vista Ultimate 32, XP Pro 32, 7 Beta 1 32. Thank you!
johnk310  Monday, June 01, 2009 4:10 PM
I have had the same experience asjohnk310

Thanksnobugzfor the work around, works for me on: Vista Business 32, .Net 2.0

Is there are bug report for this at Microsoft Connect?
I cannot seem to find one for it and was thinking of opening one.
bb_amadeus  Friday, September 18, 2009 2:07 AM

You can use google to search for other answers

Custom Search

More Threads

• Focus, form clicks & dispose...
• The Mighty BitBlt
• Simple Toobar question ? perhaps not....
• Improve TableLayoutPanel with large amount of controls
• Datagridview ComboboxColumn problem
• HTML-inputelement
• Runtime Exceptions in Windows Form application
• Newbie Q - Save File Dialog
• Tooltip in subItem of ListView Control (.Net)
• MDI and DirectX