Ajouter un commentaire

leneutrino
[C#]pb avec icone de mon treeview

salut à tous !
j'ai un treevieew sur lequel je spécifie une icone pour les noeuds parent et une autre pour les enfants. MAis le pb est que sur clic d'un enfant l'icone change et devient celle du parent. je ne comprends pas . qu'elqu'un pourrait il m'aider ?

voici mon code :

private void AddDirectoryAndNode(TreeNode ParentNode, string DirectoryPath)
 {
     // Create the ContextMenuStrip.
     ContextMenuStrip docMenu;

     docMenu = new ContextMenuStrip();

     //Create some menu items.
     ToolStripMenuItem deleteLabel = new ToolStripMenuItem();
     deleteLabel.Text = "Supprimer";
     ToolStripMenuItem renameLabel = new ToolStripMenuItem();
     renameLabel.Text = "Renommer";

     //Add the menu items to the menu.
     docMenu.Items.AddRange(new ToolStripMenuItem[]{deleteLabel, renameLabel});
     string[] directories = Directory.GetDirectories(DirectoryPath);
     foreach (string childDirectoryPath in directories)
     {
         TreeNode childNode = new TreeNode(childDirectoryPath.Substring(18));
//         childNode.ImageIndex=0;
         ParentNode.Nodes.Add(childNode);
         childNode.ImageIndex = 0;
         childNode.ContextMenuStrip = docMenu;
         AddFilesToNode(childNode, childDirectoryPath);
     }
 }

private void AddFilesToNode(TreeNode ParentNode, string FilePath)
        {
            // Create the ContextMenuStrip.
            ContextMenuStrip docMenu;

            docMenu = new ContextMenuStrip();

            //Create some menu items.
            ToolStripMenuItem openLabel = new ToolStripMenuItem();
            openLabel.Text = "Charger";
            ToolStripMenuItem deleteLabel = new ToolStripMenuItem();
            deleteLabel.Text = "Supprimer";
            ToolStripMenuItem renameLabel = new ToolStripMenuItem();
            renameLabel.Text = "Renommer";

            //Add the menu items to the menu.
            docMenu.Items.AddRange(new ToolStripMenuItem[]{openLabel, 
        deleteLabel, renameLabel});
            string[] files = Directory.GetFiles(FilePath);
            foreach (string childfile in files)
            {
                TreeNode childNode = new TreeNode(childfile.Substring(30));
        
                childNode.ContextMenuStrip = docMenu;
                                ParentNode.Nodes.Add(childNode);
                                childNode.ImageIndex = 1;
               
            }
        }

         /******************************************/
        public void addImage()
        {
            // Be sure that you use an appropriate escape sequence (such as the 
            // @) when specifying the location of the file.
            System.Drawing.Image myImageFolder =
              Image.FromFile("D:\\Les images\\folder.ico");
            imageList1.Images.Add(myImageFolder);

            System.Drawing.Image myImageFile =
              Image.FromFile("D:\\Les images\\text.ico");
            imageList1.Images.Add(myImageFile);

        }

l'appelle du code se passe comme ceci :

private void Form1_Load(object sender, EventArgs e)
        {            
            // Set the ContextMenuStrip property to the ContextMenuStrip.
            
          
            TreeNode initialNode;
            string initialDirectory = @"D:\RepertoireScan";

            initialNode = new TreeNode(initialDirectory);
            treeView1.Nodes.Add(initialNode);

            AddDirectoryAndNode(initialNode, initialDirectory);
                        addImage();
                        treeView1.ImageList = imageList1;             
 }

Filtered HTML

Plain text

CAPTCHA
Cette question permet de vérifier que vous n'êtes pas un robot spammeur :-)
 M   M  III  Y   Y  W     W  RRRR  
MM MM I Y Y W W R R
M M M I Y W W W RRRR
M M I Y W W W R R
M M III Y W W R RR