本文介绍了如何在c#中的childnodes树视图中添加复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi
我正在使用c#在.NET 3.5中使用winforms。现在我想在父节点和子节点中的复选框中添加treeview ...



i通过以下代码完成父节点创建..

Hi I am working with winforms in .NET 3.5 using c#. now i want to add treeview with parent nodes and checkboxes in child nodes...

i have completed parent node creation by following code..

dt = conn.dataTable("select desgn from designation");
            tvRoles.Nodes.Clear();
            foreach (DataRow dr in dt.Rows)
            {
                TreeNode parent = new TreeNode();
                parent.Text = dr[0].ToString();
                string value = dr[0].ToString();
                parent.Expand();
                tvRoles.Nodes.Add(parent);
                subLevel(parent, value);
            }





现在我想在子节点中添加复选框..



你可以帮助我吗???



now i want to add checkboxes in child nodes..

can u help me anyone???

推荐答案


private void treeView_DrawNode(object sender, DrawTreeNodeEventArgs e)
{
    if (e.Node.Parent == null)
    {                
        int d = (int)(0.2*e.Bounds.Height);
        Rectangle rect = new Rectangle(d + treeView.Margin.Left, d + e.Bounds.Top, e.Bounds.Height - d*2, e.Bounds.Height - d*2);
        e.Graphics.FillRectangle(new SolidBrush(Color.FromKnownColor(KnownColor.Control)), rect);
        e.Graphics.DrawRectangle(Pens.Silver, rect);
        StringFormat sf = new StringFormat() { LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Center };
        e.Graphics.DrawString(e.Node.IsExpanded ? "-" : "+", treeView.Font, new SolidBrush(Color.Blue), rect, sf); 
        //Draw the dotted line connecting the expanding/collapsing button and the node Text
        using (Pen dotted = new Pen(Color.Black) {DashStyle = System.Drawing.Drawing2D.DashStyle.Dot})
        {
          e.Graphics.DrawLine(dotted, new Point(rect.Right + 1, rect.Top + rect.Height / 2), new Point(rect.Right + 4, rect.Top + rect.Height / 2));
        }               
        //Draw text
        sf.Alignment = StringAlignment.Near;
        Rectangle textRect = new Rectangle(e.Bounds.Left + rect.Right + 4, e.Bounds.Top, e.Bounds.Width - rect.Right - 4, e.Bounds.Height);
        if (e.Node.IsSelected)
        {
            SizeF textSize = e.Graphics.MeasureString(e.Node.Text, treeView.Font);
            e.Graphics.FillRectangle(new SolidBrush(SystemColors.Highlight), new RectangleF(textRect.Left, textRect.Top, textSize.Width, textRect.Height));
        }
        e.Graphics.DrawString(e.Node.Text, treeView.Font, new SolidBrush(treeView.ForeColor), textRect,sf);
    }
    else e.DrawDefault = true;
} 


这篇关于如何在c#中的childnodes树视图中添加复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 13:55