Cообщество разработчиков на платформе Microsoft > Форумы > Visual C# Language > customized checkbox in child node and no checkbox in parent node in a treeview
Задайте вопросЗадайте вопрос
 

Отвеченоcustomized checkbox in child node and no checkbox in parent node in a treeview

  • 4 августа 2008 г. 15:03CsharpQA Медали пользователяМедали пользователяМедали пользователяМедали пользователяМедали пользователя
     
     hi all,
    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.


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Xml;
    using System.Drawing.Drawing2D;

    namespace TreeViewProj
    {
        public partial class Form2 : Form
        {
              public Form2()
            {
                InitializeComponent();
                this.treeView1.CheckBoxes = false;
                this.treeView1.ShowLines = true;
                this.treeView1.ShowPlusMinus = true;
                this.treeView1.ShowNodeToolTips = true;
                this.treeView1.ShowRootLines = true;
                this.treeView1.DrawMode = System.Windows.Forms.TreeViewDrawMode.OwnerDrawAll;

                this.treeView1.DrawNode += new System.Windows.Forms.DrawTreeNodeEventHandler(this.treeView_DrawNode);


               
                for (int i = 0; i < 3; ++i)
                {

                    this.treeView1.Nodes.Add(string.Format("First level {0}", i));



                    for (int j = 0; j < 2; j++)
                    {

                        this.treeView1.Nodes[i].Nodes.Add(string.Format("Second level {0}", j));



                        for (int k = 0; k < 1; k++)
                        {

                            this.treeView1.Nodes[i].Nodes[j].Nodes.Add(string.Format("Third level {0}", k));

                        }

                    }
                }

            }
            private void treeView_DrawNode(object sender, DrawTreeNodeEventArgs e)
            {
               
               
                if (HasChild(e.Node))
                {


                    Color backColor, foreColor;


                    if ((e.State & TreeNodeStates.Selected) == TreeNodeStates.Selected)
                    {

                        backColor = SystemColors.Highlight;

                        foreColor = SystemColors.HighlightText;

                    }

                    else if ((e.State & TreeNodeStates.Hot) == TreeNodeStates.Hot)
                    {

                        backColor = SystemColors.HotTrack;

                        foreColor = SystemColors.HighlightText;

                    }

                    else
                    {

                        backColor = e.Node.BackColor;

                        foreColor = e.Node.ForeColor;

                    }
                   
                    using (SolidBrush brush = new SolidBrush(backColor))
                    {

                        e.Graphics.FillRectangle(brush, e.Node.Bounds);
                        //e.Graphics.FillPath(brush, GraphicsPath p);

                    }

                      TextRenderer.DrawText(e.Graphics, e.Node.Text, this.treeView1.Font, e.Node.Bounds, foreColor, backColor);
                     if ((e.State & TreeNodeStates.Focused) == TreeNodeStates.Focused)
                    {

                        ControlPaint.DrawFocusRectangle(e.Graphics, e.Node.Bounds, foreColor, backColor);

                    }

                      e.DrawDefault = true;
                       }

                else
                {
                   
                   // e.DrawDefault = true;
                    CheckBox checkbox1 = new CheckBox();
                    checkbox1.Appearance = Appearance.Normal;
                    checkbox1.AutoCheck = false;
                   
                    this.treeView1.Controls.Add(checkbox1);
                    //checkbox1.Size.Height = 1;
                    //checkbox1.Size.Width = 1;

                    checkbox1.Show();
                   
                    //checkbox1.Anchor = AnchorStyles.Left;
                    //this.treeView1.CheckBoxes = true;
                        }

               
            }
          
           
            private static bool HasChild(TreeNode node)
            {
                int nodeCount = 0;
                foreach (TreeNode x in node.Nodes)
                    nodeCount++;
                if (nodeCount != 0)
                    return true;
                else
                    return false;

            }


    }   
        }





    Thanks in advance

    sanjeev

Ответы

  • 4 августа 2008 г. 16:16Jayanta Dey Медали пользователяМедали пользователяМедали пользователяМедали пользователяМедали пользователя
     ОтвеченоС кодом
    Hi,

    This is what you want:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Xml;
    using System.Drawing.Drawing2D;
    using System.Runtime.InteropServices;

    namespace WindowsApplication2
    {
        public partial class Form2 : Form
        {

            public const int TVIF_STATE = 0x8;
            public const int TVIS_STATEIMAGEMASK = 0xF000;
            public const int TV_FIRST = 0x1100;
            public const int TVM_SETITEM = TV_FIRST + 63;

            [StructLayout(LayoutKind.Sequential)]
            public struct TVITEM
            {
                public int mask;
                public IntPtr hItem;
                public int state;
                public int stateMask;
                [MarshalAs(UnmanagedType.LPTStr)]
                public string lpszText;
                public int cchTextMax;
                public int iImage;
                public int iSelectedImage;
                public int cChildren;
                public IntPtr lParam;
            }

            [DllImport("User32.dll")]
            public static extern int SendMessage (IntPtr hwnd, int msg, IntPtr wParam, ref TVITEM lParam);

            private void TreeNode_SetStateImageIndex(TreeNode node, int index)
            {
                TVITEM tvi = new TVITEM();
                tvi.hItem = node.Handle;
                tvi.mask = TVIF_STATE;
                tvi.stateMask = TVIS_STATEIMAGEMASK;
                tvi.state = index << 12;
                SendMessage(node.TreeView.Handle, TVM_SETITEM, IntPtr.Zero, ref tvi);
            }
            public Form2()
            {
                InitializeComponent();
                this.treeView1.CheckBoxes = true;
                this.treeView1.ShowLines = true;
                this.treeView1.ShowPlusMinus = true;
                this.treeView1.ShowNodeToolTips = true;
                this.treeView1.ShowRootLines = true;
                treeView1.ItemHeight = 25;
               

                this.treeView1.DrawMode = System.Windows.Forms.TreeViewDrawMode.OwnerDrawAll;

                this.treeView1.DrawNode += new System.Windows.Forms.DrawTreeNodeEventHandler(this.treeView_DrawNode);

                for (int i = 0; i < 3; i++)
                {
                    this.treeView1.Nodes.Add(string.Format("First level {0}", i));
                    for (int j = 0; j < 2; j++)
                    {
                        this.treeView1.Nodes[i].Nodes.Add(string.Format("Second level {0}", j));
                        for (int k = 0; k < 2; k++)
                        {
                            this.treeView1.Nodes[i].Nodes[j].Nodes.Add(string.Format("Third level {0}", k));
                        }
                    }
                }          
            }

            private void treeView_DrawNode(object sender, DrawTreeNodeEventArgs e)
            {

                Color backColor, foreColor;

                if ((e.State & TreeNodeStates.Selected) == TreeNodeStates.Selected)
                {
                    backColor = SystemColors.Highlight;
                    foreColor = SystemColors.HighlightText;

                }

                else if ((e.State & TreeNodeStates.Hot) == TreeNodeStates.Hot)
                {

                    backColor = SystemColors.HotTrack;
                    foreColor = SystemColors.HighlightText;

                }

                else
                {
                    backColor = e.Node.BackColor;
                    foreColor = e.Node.ForeColor;
                }

                using (SolidBrush brush = new SolidBrush(backColor))
                {

                    e.Graphics.FillRectangle(brush, e.Node.Bounds);
                    //e.Graphics.FillPath(brush, GraphicsPath p);

                }

                TextRenderer.DrawText(e.Graphics, e.Node.Text, this.treeView1.Font, e.Node.Bounds, foreColor, backColor);

                if ((e.State & TreeNodeStates.Focused) == TreeNodeStates.Focused)
                {

                    ControlPaint.DrawFocusRectangle(e.Graphics, e.Node.Bounds, foreColor, backColor);

                }
                if (HasParent(e.Node) == false)
                {
                    TreeNode_SetStateImageIndex(e.Node, 0);
                }

                e.DrawDefault = true;
            }


            private static bool HasChild(TreeNode node)
            {
                int nodeCount = 0;
                foreach (TreeNode x in node.Nodes)
                    nodeCount++;
                if (nodeCount != 0)
                    return true;
                else
                    return false;

            }

            private static bool HasParent(TreeNode node)
            {
                if (node.Parent == null)
                    return false;
                else
                    return true;
            }


        }
    }

    ref: http://groups.google.com/group/microsoft.public.dotnet.framework.windowsforms/msg/0a0b6b7c0571018c?hl=en

    Regards,

Все ответы

  • 4 августа 2008 г. 16:16Jayanta Dey Медали пользователяМедали пользователяМедали пользователяМедали пользователяМедали пользователя
     ОтвеченоС кодом
    Hi,

    This is what you want:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Xml;
    using System.Drawing.Drawing2D;
    using System.Runtime.InteropServices;

    namespace WindowsApplication2
    {
        public partial class Form2 : Form
        {

            public const int TVIF_STATE = 0x8;
            public const int TVIS_STATEIMAGEMASK = 0xF000;
            public const int TV_FIRST = 0x1100;
            public const int TVM_SETITEM = TV_FIRST + 63;

            [StructLayout(LayoutKind.Sequential)]
            public struct TVITEM
            {
                public int mask;
                public IntPtr hItem;
                public int state;
                public int stateMask;
                [MarshalAs(UnmanagedType.LPTStr)]
                public string lpszText;
                public int cchTextMax;
                public int iImage;
                public int iSelectedImage;
                public int cChildren;
                public IntPtr lParam;
            }

            [DllImport("User32.dll")]
            public static extern int SendMessage (IntPtr hwnd, int msg, IntPtr wParam, ref TVITEM lParam);

            private void TreeNode_SetStateImageIndex(TreeNode node, int index)
            {
                TVITEM tvi = new TVITEM();
                tvi.hItem = node.Handle;
                tvi.mask = TVIF_STATE;
                tvi.stateMask = TVIS_STATEIMAGEMASK;
                tvi.state = index << 12;
                SendMessage(node.TreeView.Handle, TVM_SETITEM, IntPtr.Zero, ref tvi);
            }
            public Form2()
            {
                InitializeComponent();
                this.treeView1.CheckBoxes = true;
                this.treeView1.ShowLines = true;
                this.treeView1.ShowPlusMinus = true;
                this.treeView1.ShowNodeToolTips = true;
                this.treeView1.ShowRootLines = true;
                treeView1.ItemHeight = 25;
               

                this.treeView1.DrawMode = System.Windows.Forms.TreeViewDrawMode.OwnerDrawAll;

                this.treeView1.DrawNode += new System.Windows.Forms.DrawTreeNodeEventHandler(this.treeView_DrawNode);

                for (int i = 0; i < 3; i++)
                {
                    this.treeView1.Nodes.Add(string.Format("First level {0}", i));
                    for (int j = 0; j < 2; j++)
                    {
                        this.treeView1.Nodes[i].Nodes.Add(string.Format("Second level {0}", j));
                        for (int k = 0; k < 2; k++)
                        {
                            this.treeView1.Nodes[i].Nodes[j].Nodes.Add(string.Format("Third level {0}", k));
                        }
                    }
                }          
            }

            private void treeView_DrawNode(object sender, DrawTreeNodeEventArgs e)
            {

                Color backColor, foreColor;

                if ((e.State & TreeNodeStates.Selected) == TreeNodeStates.Selected)
                {
                    backColor = SystemColors.Highlight;
                    foreColor = SystemColors.HighlightText;

                }

                else if ((e.State & TreeNodeStates.Hot) == TreeNodeStates.Hot)
                {

                    backColor = SystemColors.HotTrack;
                    foreColor = SystemColors.HighlightText;

                }

                else
                {
                    backColor = e.Node.BackColor;
                    foreColor = e.Node.ForeColor;
                }

                using (SolidBrush brush = new SolidBrush(backColor))
                {

                    e.Graphics.FillRectangle(brush, e.Node.Bounds);
                    //e.Graphics.FillPath(brush, GraphicsPath p);

                }

                TextRenderer.DrawText(e.Graphics, e.Node.Text, this.treeView1.Font, e.Node.Bounds, foreColor, backColor);

                if ((e.State & TreeNodeStates.Focused) == TreeNodeStates.Focused)
                {

                    ControlPaint.DrawFocusRectangle(e.Graphics, e.Node.Bounds, foreColor, backColor);

                }
                if (HasParent(e.Node) == false)
                {
                    TreeNode_SetStateImageIndex(e.Node, 0);
                }

                e.DrawDefault = true;
            }


            private static bool HasChild(TreeNode node)
            {
                int nodeCount = 0;
                foreach (TreeNode x in node.Nodes)
                    nodeCount++;
                if (nodeCount != 0)
                    return true;
                else
                    return false;

            }

            private static bool HasParent(TreeNode node)
            {
                if (node.Parent == null)
                    return false;
                else
                    return true;
            }


        }
    }

    ref: http://groups.google.com/group/microsoft.public.dotnet.framework.windowsforms/msg/0a0b6b7c0571018c?hl=en

    Regards,