{"id":262,"date":"2006-04-10T16:33:43","date_gmt":"2006-04-10T15:33:43","guid":{"rendered":"http:\/\/www.craigmurphy.com\/blog\/?p=262"},"modified":"2008-10-13T09:16:19","modified_gmt":"2008-10-13T08:16:19","slug":"selectively-removing-checkboxes-in-a-net-11-20-treeview","status":"publish","type":"post","link":"http:\/\/www.craigmurphy.com\/blog\/?p=262","title":{"rendered":"Selectively removing checkboxes in a .NET 1.1 \/ 2.0 TreeView"},"content":{"rendered":"<p>Earlier this month, I had the need to customise a TreeView control such that it had checkboxes against some, not all, of the nodes.<\/p>\n<p>Here&#8217;s a screenshot of what I wanted:<\/p>\n<p><img decoding=\"async\" id=\"image261\"  alt=\"Treeview\" src=\"http:\/\/www.craigmurphy.com\/blog\/wp-content\/uploads\/2006\/04\/treeview.gif\" \/><\/p>\n<p>It requires a little bit of code to achieve this effect, but is was worth the effort.  Here&#8217;s the code that performs the magic:<\/p>\n<p>[code lang=&#8221;C#&#8221;]<br \/>\nusing System.Runtime.InteropServices;<\/p>\n<p>namespace Treeview___CheckBoxes<br \/>\n{<br \/>\n    public partial class Form1 : Form<br \/>\n    {<br \/>\n        public Form1()<br \/>\n        {<br \/>\n            InitializeComponent();<br \/>\n        }<\/p>\n<p>        private void Form1_Load(object sender, EventArgs e)<br \/>\n        {<br \/>\n            \/\/ Iterate over the root nodes, removing their checkboxes<br \/>\n            for (int n = 0; n<treeView1.Nodes.Count; n++)\n            {\n                TreeNode node = treeView1.Nodes[n];\n\n                TVITEM tvItem = new TVITEM();\n                tvItem.hItem = node.Handle;  \n                tvItem.mask = TVIF_STATE;\n                tvItem.stateMask = TVIS_STATEIMAGEMASK;\n                tvItem.state = 0;\n                IntPtr lparam = Marshal.AllocHGlobal(Marshal.SizeOf(tvItem));\n                Marshal.StructureToPtr(tvItem, lparam, false);\n\n                SendMessage(this.treeView1.Handle, TVM_SETITEM, IntPtr.Zero, lparam);\n            }\n        }\n\n        public const int TVIF_STATE = 0x8;\n        public const int TVIS_STATEIMAGEMASK = 0xF000;\n        public const int TV_FIRST = 0x1100;\n        public const int TVM_SETITEM = TV_FIRST + 63;\n\n        public struct TVITEM\n        {\n            public int mask;\n            public IntPtr hItem;\n            public int state;\n            public int stateMask;\n            [MarshalAs(UnmanagedType.LPTStr)]\n            public String lpszText;\n            public int cchTextMax;\n            public int iImage;\n            public int iSelectedImage;\n            public int cChildren;\n            public IntPtr lParam;\n        }\n\n        [DllImport(\"user32.dll\")]\n        static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);\n\n    }\n}\n[\/code]\n\nTested in Visual Studio 2003 (.net 1.1) and Visual Studio 2005 (.net 2.0).<p>Technorati Tags: <a href=\"http:\/\/technorati.com\/tag\/treeview\" rel=\"tag\">treeview<\/a>, <a href=\"http:\/\/technorati.com\/tag\/treenode\" rel=\"tag\">treenode<\/a>, <a href=\"http:\/\/technorati.com\/tag\/removing\" rel=\"tag\">removing<\/a>, <a href=\"http:\/\/technorati.com\/tag\/checkbox\" rel=\"tag\">checkbox<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Earlier this month, I had the need to customise a TreeView control such that it had checkboxes against some, not all, of the nodes. Here&#8217;s a screenshot of what I wanted: It requires a little bit of code to achieve this effect, but is was worth the effort. Here&#8217;s the code that performs the magic: &hellip; <a href=\"http:\/\/www.craigmurphy.com\/blog\/?p=262\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Selectively removing checkboxes in a .NET 1.1 \/ 2.0 TreeView<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15,3],"tags":[85,87,86,88,89],"class_list":["post-262","post","type-post","status-publish","format-standard","hentry","category-net","category-development","tag-checkbox","tag-removing","tag-selective","tag-treenode","tag-treeview"],"_links":{"self":[{"href":"http:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/262","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=262"}],"version-history":[{"count":0,"href":"http:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/262\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=262"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=262"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=262"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}