0

I'm new to this domain and I'm trying to develop an application and after searching, trying any posibillities i still couldn't resolve my problem...:(

I have a windows forms. In that i have toolStripMenu, treeView and pictureBox. On click of the menu item there is FolderBrowserDialog to take input for directory path. Then, after selecting the Directory path and click 'Load Directory' button, directories and files will display in treeView. Here's my code to display directories and files (images) in treeView control:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string strSelectedPath;
        FolderBrowserDialog FBD = new FolderBrowserDialog();

        private void wczytajŚcieżkęToolStripMenuItem_Click(object sender, EventArgs e)
        {
            *// Code for the FolderBrowserDialog button*
            DialogResult drResult = FBD.ShowDialog();
            if (drResult == System.Windows.Forms.DialogResult.OK)
                strSelectedPath = FBD.SelectedPath;
        }

        *// Code for the 'Load Directory'*
        private void DirectoryLoad_btn_Click(object sender, EventArgs e)
        {
            progressBar1.Value = 0;
            treeView1.Nodes.Clear();
            toolTip1.ShowAlways = true;
            if (strSelectedPath != "" && Directory.Exists(FBD.SelectedPath))
                LoadDirectory(FBD.SelectedPath);
            else
                MessageBox.Show("Wczytaj folder!", "Brak ścieżki do załadowania");
        }

        public void LoadDirectory(string Dir)
        {
            DirectoryInfo di = new DirectoryInfo(Dir);
            DirectoryInfo[] directories = di.GetDirectories();
            progressBar1.Maximum = Directory.GetFiles(Dir, "*.*", SearchOption.AllDirectories).Length + Directory.GetDirectories(Dir, "**", SearchOption.AllDirectories).Length;
            TreeNode tds = treeView1.Nodes.Add(di.Name);
            tds.Tag = di.FullName;
            tds.StateImageIndex = 0;
            LoadFiles(Dir, tds);
            LoadSubDirectories(Dir, tds);
        }

        private void LoadSubDirectories(string dir, TreeNode td)
        {
            string[] subdirectoryEntries = Directory.GetDirectories(dir);
            foreach (string subdirectory in subdirectoryEntries)
            {
                DirectoryInfo di = new DirectoryInfo(subdirectory);
                TreeNode tds = td.Nodes.Add(di.Name);
                tds.StateImageIndex = 0;
                tds.Tag = di.FullName;
                LoadFiles(subdirectory, tds);
                LoadSubDirectories(subdirectory, tds);
                UpdateProgress();
            }
        }

        private void LoadFiles(string dir, TreeNode td)
        {
            string[] Files = Directory.GetFiles(dir, "*.*");
            foreach (string file in Files)
            {
                FileInfo fi = new FileInfo(file);
                TreeNode tds = td.Nodes.Add(fi.Name);
                tds.Tag = fi.FullName;
                tds.StateImageIndex = 1;
                UpdateProgress();
            }
        }

        private void UpdateProgress()
        {
            if (progressBar1.Value < progressBar1.Maximum)
            {
                progressBar1.Value++;
                int percent = (int)
                (((double)progressBar1.Value / (double)progressBar1.Maximum) * 100);
                progressBar1.CreateGraphics().DrawString(percent.ToString() + "%", new Font("Adobe Caslon Pro", (float)8.25, FontStyle.Regular), Brushes.Black, new PointF(progressBar1.Width / 2 - 10, progressBar1.Height / 2 - 7));
            }
            Application.DoEvents();
        }

        private void treeView1_MouseMove(object sender, MouseEventArgs e)
        {
            TreeNode theNode = this.treeView1.GetNodeAt(e.X, e.Y);
            if (theNode != null && theNode.Tag != null)
            {
                if (theNode.Tag.ToString() != this.toolTip1.GetToolTip(this.treeView1))
                    this.toolTip1.SetToolTip(this.treeView1, theNode.Tag.ToString());
            }
            else
            {
                this.toolTip1.SetToolTip(this.treeView1, "");
            }
        }

This code works fine but Now I can't loading images into a pictureBox from treeView. I tried many ways to resolve this and I'm stuck with it ...

Any suggestion with sample code will be great! Thank You!

icebat
  • 4,696
  • 4
  • 22
  • 36
Kalolalo
  • 9
  • 4
  • Well what is the problem. All looks fine; but: did you code a TreeView event, maybe AfterSelect? – TaW Dec 13 '18 at 15:00
  • Yes. I tried AfterSelect, NodeMouseClick, NodeMouseDoubleClick etc. and nothing worked. I suppose that I did something wrong in code. – Kalolalo Dec 13 '18 at 15:28
  • The treeview is a form control so see following : https://stackoverflow.com/questions/1881317/c-sharp-windows-form-control-to-image – jdweng Dec 13 '18 at 15:29
  • _I tried AfterSelect, NodeMouseClick, NodeMouseDoubleClick etc. and nothing worked._ Sigh. Not seeing that code, how can we help?? Try `string fn = e.Node.Tag.ToString(); if (File.Exists(fn)) pictureBox1.Image = Image.FromFile(fn);` – TaW Dec 13 '18 at 15:43
  • I wrote something like that and this is one of the many method's I used to try: TreeNode selection = this.treeView1.GetNodeAt(e.X, e.Y); if (selection != null) { pictureBox1.Image = Image.FromFile(treeView1.SelectedNode.Text); } Sorry, I didn't write it sooner, but I'm new around here. Actually, I'm just sort of learning ;). And by the way, what you have written TaW... it's working! Thank You! :) – Kalolalo Dec 14 '18 at 13:24

0 Answers0