Change Treeview Link line style

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gyanendar
    New Member
    • Jun 2007
    • 90

    Change Treeview Link line style

    Hi All,
    Can it be possible to change the Treeview link line(line connecting parent & Child node) style.Default its coming as dotted line .I want to make it solid line.
    I am using WIN-FORM.
    Please suggest.

    -Gyanendar
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    I think you would have to change the draw mode to owner draw and draw the lines yourself?

    Comment

    • gyanendar
      New Member
      • Jun 2007
      • 90

      #3
      If i draw line by myself ,how i'll find the exact path along which line would be drawn? We can hide the treeview link and need to get the exact co-ordinate of the point of Parent & Child

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Well getting one is easy with the DrawNode event.
        Hmm I think I have an idea on how to draw the lines, i'll get back to you

        Comment

        • gyanendar
          New Member
          • Jun 2007
          • 90

          #5
          Originally posted by Plater
          Well getting one is easy with the DrawNode event.
          Hmm I think I have an idea on how to draw the lines, i'll get back to you
          Hi Plater,
          Thanks for reply.
          Here If we have the co-ordinate of points ,line can be drawn by e.Graphics.Draw line().

          Gyanendar

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Yeah I did the whole thing(sort of). I don't generally do the project, but this one caught my interest.

            I do a few silly things to highlight what is going on, but it could certainly be adapted as needed.

            [code=C#]

            private void treeView1_DrawN ode(object sender, DrawTreeNodeEve ntArgs e)
            {
            bool isOSDrawn = e.DrawDefault;
            Graphics g = e.Graphics;
            TreeNode tn = e.Node;
            Font DrawFont = treeView1.Font;
            Brush ForeBrush = new SolidBrush(tree View1.ForeColor );
            Brush BackGroundBrush = new SolidBrush(tree View1.BackColor );
            Brush TextBackGroundB rush = BackGroundBrush ;
            float baseX = e.Bounds.Left;
            float baseY = e.Bounds.Top;
            string stringToDraw = tn.Text;
            bool hasParent = (tn.Parent != null);
            bool hasNext = (tn.NextNode != null);
            bool hasPrev = (tn.PrevNode != null);

            float strX = baseX;
            float strY = baseY;
            float adjX = ((tn.Level + 1) * 24);
            strX += adjX;

            Rectangle UpLine = new Rectangle((int) (strX - 17), (int)(baseY), 2, 8);
            Rectangle DownLine = new Rectangle((int) (strX - 17), (int)(baseY + 8), 2, 8);
            Rectangle ChildBox = new Rectangle((int) (baseX - 16 + adjX + 2), (int)(baseY + 2), 12, 12);

            float tailStartX = baseX - 16 + adjX + 2 + 12;
            float tailStartY = baseY + 8;
            if (e.Bounds.Width != 0)
            {
            if ((e.State & TreeNodeStates. Hot) == TreeNodeStates. Hot)
            {//Hot tracking
            DrawFont = new Font(DrawFont, FontStyle.Under line);
            }
            if ((e.State & TreeNodeStates. Selected) == TreeNodeStates. Selected)
            {//change foregroudn/background
            TextBackGroundB rush = new SolidBrush(Syst emColors.Inacti veBorder);
            if (this.ActiveCon trol == sender)
            {
            ForeBrush = new SolidBrush(Syst emColors.Highli ghtText);
            TextBackGroundB rush = new SolidBrush(Syst emColors.Highli ght);
            }
            }
            //// Erase previous
            g.FillRectangle (BackGroundBrus h, e.Bounds);
            SizeF stringSize = g.MeasureString (stringToDraw, DrawFont, (int)(e.Bounds. Width - strX), new StringFormat(St ringFormatFlags .NoWrap));
            //// Draw the "selected" background
            g.FillRectangle (TextBackGround Brush, strX, strY, (float)Math.Flo or(stringSize.W idth), 16F);
            //// Draw the text
            g.DrawString(st ringToDraw, DrawFont, ForeBrush, strX, strY);

            //// Every node gets a tail sticking out
            g.FillRectangle (Brushes.Black, strX - 17, tailStartY, 16, 2);
            ///////////////////////////////////////////////////////////////////
            if (hasPrev || hasParent) g.FillRectangle (Brushes.Black, UpLine);// Draw up line
            if (hasNext) g.FillRectangle (Brushes.Black, DownLine);// Draw down line
            //// Draw the "+" box
            if (tn.Nodes.Count > 0)
            {
            g.FillRectangle (BackGroundBrus h, ChildBox);
            if (!tn.IsExpanded ) g.FillRectangle (new SolidBrush(Colo r.Red), ChildBox);
            g.DrawRectangle (Pens.Black, ChildBox);
            }
            }//end of valid draw region
            }

            [/code]

            Comment

            Working...