customized checkbox in child node and no checkbox in parent node in a treeview  hi all,<br>I want to add a customized checkbox in child node and no checkbox in parent node in a treeviewstructure in c#. But i don't want to use DrawDefault property.Can anyone help me in doing this. I have tried the following code but cannot position the checkbox and it is not working.<br><br><br> <p>using System;<br>using System.Collections.Generic;<br>using System.ComponentModel;<br>using System.Data;<br>using System.Drawing;<br>using System.Text;<br>using System.Windows.Forms;<br>using System.Xml;<br>using System.Drawing.Drawing2D;<br><br>namespace TreeViewProj<br>{<br>    public partial class Form2 : Form<br>    {<br>          public Form2()<br>        {<br>            InitializeComponent();<br>            this.treeView1.CheckBoxes = false;<br>            this.treeView1.ShowLines = true;<br>            this.treeView1.ShowPlusMinus = true;<br>            this.treeView1.ShowNodeToolTips = true;<br>            this.treeView1.ShowRootLines = true;<br>            this.treeView1.DrawMode = System.Windows.Forms.TreeViewDrawMode.OwnerDrawAll;</p> <p>            this.treeView1.DrawNode += new System.Windows.Forms.DrawTreeNodeEventHandler(this.treeView_DrawNode);</p> <p><br>            <br>            for (int i = 0; i &lt; 3; ++i)<br>            {</p> <p>                this.treeView1.Nodes.Add(string.Format(&quot;First level {0}&quot;, i));</p><br><br> <p>                for (int j = 0; j &lt; 2; j++)<br>                {</p> <p>                    this.treeView1.Nodes[i].Nodes.Add(string.Format(&quot;Second level {0}&quot;, j));</p><br><br> <p>                    for (int k = 0; k &lt; 1; k++)<br>                    {</p> <p>                        this.treeView1.Nodes[i].Nodes[j].Nodes.Add(string.Format(&quot;Third level {0}&quot;, k));</p> <p>                    }</p> <p>                }<br>            }</p> <p>        }<br>        private void treeView_DrawNode(object sender, DrawTreeNodeEventArgs e)<br>        {<br>            <br>            <br>            if (HasChild(e.Node))<br>            {</p> <p><br>                Color backColor, foreColor;</p> <p><br>                if ((e.State &amp; TreeNodeStates.Selected) == TreeNodeStates.Selected)<br>                {</p> <p>                    backColor = SystemColors.Highlight;</p> <p>                    foreColor = SystemColors.HighlightText;</p> <p>                }</p> <p>                else if ((e.State &amp; TreeNodeStates.Hot) == TreeNodeStates.Hot)<br>                {</p> <p>                    backColor = SystemColors.HotTrack;</p> <p>                    foreColor = SystemColors.HighlightText;</p> <p>                }</p> <p>                else<br>                {</p> <p>                    backColor = e.Node.BackColor;</p> <p>                    foreColor = e.Node.ForeColor;</p> <p>                }<br>                <br>                using (SolidBrush brush = new SolidBrush(backColor))<br>                {</p> <p>                    e.Graphics.FillRectangle(brush, e.Node.Bounds);<br>                    //e.Graphics.FillPath(brush, GraphicsPath p);</p> <p>                }</p> <p>                  TextRenderer.DrawText(e.Graphics, e.Node.Text, this.treeView1.Font, e.Node.Bounds, foreColor, backColor);<br>                 if ((e.State &amp; TreeNodeStates.Focused) == TreeNodeStates.Focused)<br>                {</p> <p>                    ControlPaint.DrawFocusRectangle(e.Graphics, e.Node.Bounds, foreColor, backColor);</p> <p>                }</p> <p>                  e.DrawDefault = true;<br>                   }</p> <p>            else<br>            {<br>                <br>               // e.DrawDefault = true;<br>                CheckBox checkbox1 = new CheckBox();<br>                checkbox1.Appearance = Appearance.Normal;<br>                checkbox1.AutoCheck = false;<br>                <br>                this.treeView1.Controls.Add(checkbox1);<br>                //checkbox1.Size.Height = 1;<br>                //checkbox1.Size.Width = 1;</p> <p>                checkbox1.Show();<br>                <br>                //checkbox1.Anchor = AnchorStyles.Left;<br>                //this.treeView1.CheckBoxes = true;<br>                    }</p> <p>            <br>        }<br>       <br>        <br>        private static bool HasChild(TreeNode node)<br>        {<br>            int nodeCount = 0;<br>            foreach (TreeNode x in node.Nodes)<br>                nodeCount++;<br>            if (nodeCount != 0)<br>                return true;<br>            else<br>                return false;</p> <p>        }</p> <p><br>}    <br>    }</p><br><br><br><br>Thanks in advance<br><hr size="1" align="left" width="25%">sanjeev© 2009 Microsoft Corporation. All rights reserved.Tue, 24 Feb 2009 19:55:29 Z4992c9ef-59cc-41ec-bae0-43b7e335560dhttp://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/4992c9ef-59cc-41ec-bae0-43b7e335560d#4992c9ef-59cc-41ec-bae0-43b7e335560dhttp://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/4992c9ef-59cc-41ec-bae0-43b7e335560d#4992c9ef-59cc-41ec-bae0-43b7e335560dCsharpQAhttp://social.msdn.microsoft.com/Profile/en-US/?user=CsharpQAcustomized checkbox in child node and no checkbox in parent node in a treeview  hi all,<br>I want to add a customized checkbox in child node and no checkbox in parent node in a treeviewstructure in c#. But i don't want to use DrawDefault property.Can anyone help me in doing this. I have tried the following code but cannot position the checkbox and it is not working.<br><br><br> <p>using System;<br>using System.Collections.Generic;<br>using System.ComponentModel;<br>using System.Data;<br>using System.Drawing;<br>using System.Text;<br>using System.Windows.Forms;<br>using System.Xml;<br>using System.Drawing.Drawing2D;<br><br>namespace TreeViewProj<br>{<br>    public partial class Form2 : Form<br>    {<br>          public Form2()<br>        {<br>            InitializeComponent();<br>            this.treeView1.CheckBoxes = false;<br>            this.treeView1.ShowLines = true;<br>            this.treeView1.ShowPlusMinus = true;<br>            this.treeView1.ShowNodeToolTips = true;<br>            this.treeView1.ShowRootLines = true;<br>            this.treeView1.DrawMode = System.Windows.Forms.TreeViewDrawMode.OwnerDrawAll;</p> <p>            this.treeView1.DrawNode += new System.Windows.Forms.DrawTreeNodeEventHandler(this.treeView_DrawNode);</p> <p><br>            <br>            for (int i = 0; i &lt; 3; ++i)<br>            {</p> <p>                this.treeView1.Nodes.Add(string.Format(&quot;First level {0}&quot;, i));</p><br><br> <p>                for (int j = 0; j &lt; 2; j++)<br>                {</p> <p>                    this.treeView1.Nodes[i].Nodes.Add(string.Format(&quot;Second level {0}&quot;, j));</p><br><br> <p>                    for (int k = 0; k &lt; 1; k++)<br>                    {</p> <p>                        this.treeView1.Nodes[i].Nodes[j].Nodes.Add(string.Format(&quot;Third level {0}&quot;, k));</p> <p>                    }</p> <p>                }<br>            }</p> <p>        }<br>        private void treeView_DrawNode(object sender, DrawTreeNodeEventArgs e)<br>        {<br>            <br>            <br>            if (HasChild(e.Node))<br>            {</p> <p><br>                Color backColor, foreColor;</p> <p><br>                if ((e.State &amp; TreeNodeStates.Selected) == TreeNodeStates.Selected)<br>                {</p> <p>                    backColor = SystemColors.Highlight;</p> <p>                    foreColor = SystemColors.HighlightText;</p> <p>                }</p> <p>                else if ((e.State &amp; TreeNodeStates.Hot) == TreeNodeStates.Hot)<br>                {</p> <p>                    backColor = SystemColors.HotTrack;</p> <p>                    foreColor = SystemColors.HighlightText;</p> <p>                }</p> <p>                else<br>                {</p> <p>                    backColor = e.Node.BackColor;</p> <p>                    foreColor = e.Node.ForeColor;</p> <p>                }<br>                <br>                using (SolidBrush brush = new SolidBrush(backColor))<br>                {</p> <p>                    e.Graphics.FillRectangle(brush, e.Node.Bounds);<br>                    //e.Graphics.FillPath(brush, GraphicsPath p);</p> <p>                }</p> <p>                  TextRenderer.DrawText(e.Graphics, e.Node.Text, this.treeView1.Font, e.Node.Bounds, foreColor, backColor);<br>                 if ((e.State &amp; TreeNodeStates.Focused) == TreeNodeStates.Focused)<br>                {</p> <p>                    ControlPaint.DrawFocusRectangle(e.Graphics, e.Node.Bounds, foreColor, backColor);</p> <p>                }</p> <p>                  e.DrawDefault = true;<br>                   }</p> <p>            else<br>            {<br>                <br>               // e.DrawDefault = true;<br>                CheckBox checkbox1 = new CheckBox();<br>                checkbox1.Appearance = Appearance.Normal;<br>                checkbox1.AutoCheck = false;<br>                <br>                this.treeView1.Controls.Add(checkbox1);<br>                //checkbox1.Size.Height = 1;<br>                //checkbox1.Size.Width = 1;</p> <p>                checkbox1.Show();<br>                <br>                //checkbox1.Anchor = AnchorStyles.Left;<br>                //this.treeView1.CheckBoxes = true;<br>                    }</p> <p>            <br>        }<br>       <br>        <br>        private static bool HasChild(TreeNode node)<br>        {<br>            int nodeCount = 0;<br>            foreach (TreeNode x in node.Nodes)<br>                nodeCount++;<br>            if (nodeCount != 0)<br>                return true;<br>            else<br>                return false;</p> <p>        }</p> <p><br>}    <br>    }</p><br><br><br><br>Thanks in advance<br><hr size="1" align="left" width="25%">sanjeevMon, 04 Aug 2008 15:03:25 Z2008-08-04T15:03:25Zhttp://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/4992c9ef-59cc-41ec-bae0-43b7e335560d#6ffa128c-cdd1-429e-a33a-a89946b04bd1http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/4992c9ef-59cc-41ec-bae0-43b7e335560d#6ffa128c-cdd1-429e-a33a-a89946b04bd1Jayanta Deyhttp://social.msdn.microsoft.com/Profile/en-US/?user=Jayanta%20Deycustomized checkbox in child node and no checkbox in parent node in a treeview Hi,<br><br>This is what you want:<br><br>using System;<br>using System.Collections.Generic;<br>using System.ComponentModel;<br>using System.Data;<br>using System.Drawing;<br>using System.Text;<br>using System.Windows.Forms;<br>using System.Xml;<br>using System.Drawing.Drawing2D;<br>using System.Runtime.InteropServices;<br><br>namespace WindowsApplication2<br>{<br>    public partial class Form2 : Form<br>    {<br><br>        public const int TVIF_STATE = 0x8;<br>        public const int TVIS_STATEIMAGEMASK = 0xF000;<br>        public const int TV_FIRST = 0x1100;<br>        public const int TVM_SETITEM = TV_FIRST + 63;<br><br>        [StructLayout(LayoutKind.Sequential)]<br>        public struct TVITEM<br>        {<br>            public int mask;<br>            public IntPtr hItem;<br>            public int state;<br>            public int stateMask;<br>            [MarshalAs(UnmanagedType.LPTStr)]<br>            public string lpszText;<br>            public int cchTextMax;<br>            public int iImage;<br>            public int iSelectedImage;<br>            public int cChildren;<br>            public IntPtr lParam;<br>        }<br><br>        [DllImport(&quot;User32.dll&quot;)]<br>        public static extern int SendMessage (IntPtr hwnd, int msg, IntPtr wParam, ref TVITEM lParam);<br><br>        private void TreeNode_SetStateImageIndex(TreeNode node, int index)<br>        {<br>            TVITEM tvi = new TVITEM();<br>            tvi.hItem = node.Handle;<br>            tvi.mask = TVIF_STATE;<br>            tvi.stateMask = TVIS_STATEIMAGEMASK;<br>            tvi.state = index &lt;&lt; 12;<br>            SendMessage(node.TreeView.Handle, TVM_SETITEM, IntPtr.Zero, ref tvi);<br>        }<br>        public Form2()<br>        {<br>            InitializeComponent();<br>            this.treeView1.CheckBoxes = true;<br>            this.treeView1.ShowLines = true;<br>            this.treeView1.ShowPlusMinus = true;<br>            this.treeView1.ShowNodeToolTips = true;<br>            this.treeView1.ShowRootLines = true;<br>            treeView1.ItemHeight = 25;<br>            <br><br>            this.treeView1.DrawMode = System.Windows.Forms.TreeViewDrawMode.OwnerDrawAll;<br><br>            this.treeView1.DrawNode += new System.Windows.Forms.DrawTreeNodeEventHandler(this.treeView_DrawNode);<br><br>            for (int i = 0; i &lt; 3; i++)<br>            {<br>                this.treeView1.Nodes.Add(string.Format(&quot;First level {0}&quot;, i));<br>                for (int j = 0; j &lt; 2; j++)<br>                {<br>                    this.treeView1.Nodes[i].Nodes.Add(string.Format(&quot;Second level {0}&quot;, j));<br>                    for (int k = 0; k &lt; 2; k++)<br>                    {<br>                        this.treeView1.Nodes[i].Nodes[j].Nodes.Add(string.Format(&quot;Third level {0}&quot;, k));<br>                    }<br>                }<br>            }           <br>        }<br><br>        private void treeView_DrawNode(object sender, DrawTreeNodeEventArgs e)<br>        {<br><br>            Color backColor, foreColor;<br><br>            if ((e.State &amp; TreeNodeStates.Selected) == TreeNodeStates.Selected)<br>            {<br>                backColor = SystemColors.Highlight;<br>                foreColor = SystemColors.HighlightText;<br><br>            }<br><br>            else if ((e.State &amp; TreeNodeStates.Hot) == TreeNodeStates.Hot)<br>            {<br><br>                backColor = SystemColors.HotTrack;<br>                foreColor = SystemColors.HighlightText;<br><br>            }<br><br>            else<br>            {<br>                backColor = e.Node.BackColor;<br>                foreColor = e.Node.ForeColor;<br>            }<br><br>            using (SolidBrush brush = new SolidBrush(backColor))<br>            {<br><br>                e.Graphics.FillRectangle(brush, e.Node.Bounds);<br>                //e.Graphics.FillPath(brush, GraphicsPath p);<br><br>            }<br><br>            TextRenderer.DrawText(e.Graphics, e.Node.Text, this.treeView1.Font, e.Node.Bounds, foreColor, backColor);<br><br>            if ((e.State &amp; TreeNodeStates.Focused) == TreeNodeStates.Focused)<br>            {<br><br>                ControlPaint.DrawFocusRectangle(e.Graphics, e.Node.Bounds, foreColor, backColor);<br><br>            }<br>            if (HasParent(e.Node) == false)<br>            {<br>                TreeNode_SetStateImageIndex(e.Node, 0);<br>            }<br><br>            e.DrawDefault = true;<br>        }<br><br><br>        private static bool HasChild(TreeNode node)<br>        {<br>            int nodeCount = 0;<br>            foreach (TreeNode x in node.Nodes)<br>                nodeCount++;<br>            if (nodeCount != 0)<br>                return true;<br>            else<br>                return false;<br><br>        }<br><br>        private static bool HasParent(TreeNode node)<br>        {<br>            if (node.Parent == null)<br>                return false;<br>            else<br>                return true;<br>        }<br><br><br>    }<br>}<br><br>ref: http://groups.google.com/group/microsoft.public.dotnet.framework.windowsforms/msg/0a0b6b7c0571018c?hl=en<br><br>Regards,<br><br> Mon, 04 Aug 2008 16:16:33 Z2008-08-04T16:16:33Z