Friday, March 02, 2001 ..:: Home ::.. Register  Login
   TechTidBits (Blog)  

Have you ever wanted to mix a TreeView with checkboxes?

Feb 9

Written by:
Monday, February 09, 2009 9:46 PM  RssIcon

Someone at my work today asked me a whiz-bang question (thanks SB! :>) about how to selectively put a checkbox on a TreeView's node?  HHHHMMMM  Please read on if you were as interested as I was!

SB asked me a cool question today about judiciously having a checkbox hanging off a TreeView node element.  Easy I thought at first, just set a property....hhhhmmmm this guy's smart, so he would have already thought of that!  There must be a catch eh?  There is!  Read on!

Warning, background material here!

If you look at MSDN TreeView class docs, you'll notice there is a property for showing up Checkboxes.  The only problem with that property is it sets the checkboxes for the entire tree!

TreeView with checkboxes on all nodes

Notice, all the nodes have checkboxes.   HHHMMMM I thought maybe I need to go looking specifically at the TreeNode class.  HEY!  There's a Checked property which looks promising!  But alas, it's what gets/sets the checkmark itself.  No dice there.

Solution Details

After scratching my head a bit, I found this article which basically explains this CAN be done, BUT you need to use Windows message pumps (I believe that's what it's called?  I"m sure my buddy SG will correct me if I'm wrong :>).  That's the technical term for using C#s interop functionality to call unmanaged code.  If you're familiar with C++, I'm sure you'll be just at home with the code but if you're from the java/VB world, just take a gander at the code, cut'n'paste it and poke around a bit.  I did that, got it to work and had to move onto another deliverable.  Sorry, I can't give many more details than that (that's not entirely true, there are some comments in the code! :>).

The meat of all this is two parts (ok, so I lied, I'll give some details here :>).

Key pieces for making this work

You'll notice three sections above, first you need to declare the call to the SendMessage method in user32.dll.  Next you need to declare some constants you need to know (these you just take for granted are right, if you're sceptic, cool, you go look them up yourself, DOH! :>), and lastly you need to setup the TVITEM structure to hold the information going out to Windows via the SendMessage method.

That's the plumbing, next you'll need to actually call the SendMessage, here's how you do that.

Example of SendMessage method

After you run the code in the solution link below, you should see the following (and I say SHOULD loosely cause I haven't had my four cups of coffee today, but the code compiles! :>).

TreeView with selective checkboxes on some nodes

Download of solution/project which will create the screen shot above.  The soution file is zipped up, it's a VS08, .NET 2.0 project and not doing anything out of the scope of this topic.

What's that?  You want to debate?  Sure ok!

  • Yup, you could extend TreeNode to encapsulate this "checkbox" functionality, but I'm assuming you  know how to do that anyways.
  • By creating your own TreeNodeWithoutCheckbox you'll probably need to do this code anyways.
  • There is another way to do this which SG suggested (we were brainstorming a solution), you could overload the image drawing method to draw your own checkbox.  This isn't as far fetched as it first sounds since this is how other's have solved this before.  However, this comes with two issues, one, you'll have to cover off the Checked and Unchecked events on your own, and also, since you're doing the drawing, you'll also have to take into account when/if there is an image (like above) supplied to be displayed.

Good luck and I hope you find this helpful. 


Selecgtively removing checkboxes in a .NET 1.1 / 2.0 TreeView

Location: Blogs Parent Separator TechTidBits

2 comment(s) so far...


Re: Have you ever wanted to mix a TreeView with checkboxes?

I shouldn't have to download your gay ass C# project to copy a few lines of code.Moron.

By Bob on   Friday, March 23, 2012 3:54 PM

Re: Have you ever wanted to mix a TreeView with checkboxes?

Hi Bob, I would like to say thanks for leaving a comment, but I sense some hostility? DOH! haha

Actually, you don't HAVE to download the project if you don't want to. You could either try it out yourself, or look at the screen caps/pics of the code to gleen what you want out of it. At the end of the day, if you want to mimic the code, you have to write it. So either way you get it, it's still in Visual Studio. If you don't want to get it, that's your choice.

Actually, this is the first time somebody's commented on my code, thank you for that. Have a good weekend.

By Peter Henry on   Friday, March 23, 2012 4:04 PM

Your name:
Gravatar Preview
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Add Comment   Cancel 
Copyright 1999-2012 by   Terms Of Use  Privacy Statement