azolfaghari 0 Newbie Poster

Hi list,:)
I have writen a treeview, in which I have three nodes, in the front of each one there is a rectangle filled with a colour. Now I want to click the mouse (or double click mouse) on the node, then change the colour of rectangle using colorDialog. I have done something, but once I choose the colour in colorDialog and press ok, it does change the rectangle colour, but the colorDialog appears a few second after being disappear. Would you let me know where the problem is? I have attached the code, which is zipped.

thanks
Arvin

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

namespace treeview1
{
    public partial class Form1 : Form
    {
      //private TreeView myTreeView;
      // Create a Font object for the node tags.
      Font tagFont = new Font("Arial", 8, FontStyle.Bold);

        public Form1()
        {
            InitializeComponent();
            mytreeview.CheckBoxes = true;
            TreeNode node;
            node = mytreeview.Nodes.Add("firstParent");
            node.Nodes.Add("firstChild");
            node = mytreeview.Nodes.Add("secondParent");
            mytreeview.ExpandAll();
            //mytreeview.CollapseAll();
           
            //to update the option of treeview for text and rectangle 
            mytreeview.DrawMode = TreeViewDrawMode.OwnerDrawAll;

            // to write node 
            mytreeview.DrawNode += new DrawTreeNodeEventHandler(drawRectangleForMe);
            // Add a handler for the MouseDown event so that a node can be 
            // selected by clicking the tag text as well as the node text.
            mytreeview.MouseDown += new MouseEventHandler(myTreeView_MouseDown);

        }

    // Clean up any resources being used.        
    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            tagFont.Dispose();
        }
        base.Dispose(disposing);
    }

    // Draws a node.
    private void drawRectangleForMe(object sender, DrawTreeNodeEventArgs e)
    {
        e.DrawDefault = true;
        using (Pen focusPen = new Pen(Color.Black))
        {
            focusPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
            Rectangle focusBounds = NodeBounds(e.Node);
            focusBounds.Size = new Size(focusBounds.Width - 1,
            focusBounds.Height - 1); 
            e.Graphics.DrawRectangle(focusPen, focusBounds);
        }

         e.Graphics.FillRectangle(Brushes.Green  , NodeBounds(e.Node));
    }

    private void editnode(object sender, DrawTreeNodeEventArgs e)
    {
         if (colorDialog1.ShowDialog() == DialogResult.OK)
         {
            colorDialog1.ShowDialog();
            SolidBrush brush = new SolidBrush(colorDialog1.Color);
            e.Graphics.FillRectangle(brush, NodeBounds(e.Node));
         }
    }

    private void myTreeView_MouseDown(object sender, MouseEventArgs e)
    {
        mytreeview.DrawNode += new DrawTreeNodeEventHandler(editnode);
    }


    // Returns the bounds of the specified node
    private Rectangle NodeBounds(TreeNode node)
    {
                // Set the return value to the normal node bounds.
                Rectangle bounds = node.Bounds;
                bounds.X = 120;
                bounds.Width = 13;
                bounds.Height = 13;
                if (node.Tag != null)
                {
                    // Retrieve a Graphics object from the TreeView handle
                    // and use it to calculate the display width of the tag.
                    Graphics g = mytreeview.CreateGraphics(); 
                    int tagWidth = (int)g.MeasureString
                        (node.Tag.ToString(), tagFont).Width + 40;

                    // Adjust the node bounds using the calculated value.
                    bounds.Offset(5, 0);
                    bounds = Rectangle.Inflate( bounds, 0, 0);
                    g.Dispose();
                 }

                return bounds;

    }

    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.