Jump to content

Treeview click a dir, then listbox contents c# .net vs 2019

WillLTT
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Skotoma_Files_2._0
{

    public partial class Form1 : Form
    {
        public Form1()
 

        {
            InitializeComponent();
            treeView1.ImageList = imageList1;
        }

        

        private void ToolStripMenuItem1_Click(object sender, EventArgs e)
        {

        }

        private void ToolStripMenuItem5_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
           
            string[] drives = Environment.GetLogicalDrives();

            foreach (string drive in drives)
            {
                DriveInfo di = new DriveInfo(drive);
                int driveImage;

                switch (di.DriveType)    
                {
                    case DriveType.CDRom:
                        driveImage = 3;
                        break;
                    case DriveType.Network:
                        driveImage = 6;
                        break;
                    case DriveType.NoRootDirectory:
                        driveImage = 8;
                        break;
                    case DriveType.Unknown:
                        driveImage = 8;
                        break;
                    default:
                        driveImage = 2;
                        break;
                }

                TreeNode node = new TreeNode(drive.Substring(0, 1), driveImage, driveImage);
                node.Tag = drive;

                if (di.IsReady == true)
                    node.Nodes.Add("...");

                treeView1.Nodes.Add(node);
            }
    }

        private void TreeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            if (e.Node.Nodes.Count > 0)
            {
                if (e.Node.Nodes[0].Text == "..." && e.Node.Nodes[0].Tag == null)
                {
                    e.Node.Nodes.Clear();

                    
                    string[] dirs = Directory.GetDirectories(e.Node.Tag.ToString());

                    foreach (string dir in dirs)
                    {
                        DirectoryInfo di = new DirectoryInfo(dir);
                        TreeNode node = new TreeNode(di.Name, 0, 1);

                        try
                        {
                            
                            node.Tag = dir;

                            
                            if (di.GetDirectories().Count() > 0)
                                node.Nodes.Add(null, "...", 0, 0);
                        }
                        catch (UnauthorizedAccessException)
                        {
                            
                            node.ImageIndex = 12;
                            node.SelectedImageIndex = 12;
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "DirectoryLister",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        finally
                        {
                            e.Node.Nodes.Add(node);
                        }
                    }
                }
            }
        }

        private void OpenSettingsInNewTabToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form f2 = new Form2();
            f2.ShowDialog();
        }

        private void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                lblDir.Text = treeView1.SelectedNode.FullPath;
                string path = Path.Combine(treeView1.SelectedNode.FullPath, listBox1.SelectedItem.ToString());

                FileInfo fi = new FileInfo(path);

                lblFile.Text = listBox1.SelectedItem.ToString();
                lblSize.Text = fi.Length.ToString();
                lblCreated.Text = fi.CreationTime.ToString();
                lblAccess.Text = fi.LastAccessTime.ToString();
                lblModified.Text = fi.LastWriteTime.ToString();
                

                StreamReader rstr = fi.OpenText();
                string[] sline = new string[65535];

                // max lines a text box can hold

                int count = 0;

                if (rstr != null)
                {
                    count = 0;
                    sline[count++] = rstr.ReadLine();
                    while (sline[count - 1] != null)
                    {
                        sline[count++] = rstr.ReadLine();
                        if (count >= 65536) break;
                    }
                    rstr.Close();

                    string[] finalSize = new string[count];

                    // find exact size of line array otherwise blank

                    // blank lines will show up in editor

                    for (int i = 0; i < count; i++)

                        finalSize[i] = sline[i];

                    

                }
            }
            catch { }
        }

        private void TreeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {

        }




        private void FillDirectory(string drv, TreeNode parent, int level)
        {
            try
            {
               
                level++;
                if (level > 4)
                    return;
                DirectoryInfo dir = new DirectoryInfo(drv);
                if (!dir.Exists)
                    throw new DirectoryNotFoundException
                        ("directory does not exist:" + drv);

                foreach (DirectoryInfo di in dir.GetDirectories())
                {
                    TreeNode child = new TreeNode();
                    child.Text = di.Name;
                    parent.Nodes.Add(child);

                    FillDirectory(child.FullPath, child, level);
                }
            }
            catch (Exception ex)
            {
                ex.ToString();
            }

        }

        private void Button1_Click(object sender, EventArgs e)
        {
           
                


         }
        
}
}

I Have a treeview showing contents of drives. I cannot find a way to make the nodes clickable and show files in the folder clicked in a listview.

Please help your my only hope i cannot find this anywhere else and stackoverflow idk how to use <code tags> so i cant post anything there

in vs 2019 c# win forms .net

Link to comment
Share on other sites

Link to post
Share on other sites

 

the Treeview control triggers a 'AfterSelect' event that is what you want to use.

- Click you TreeView

- go to the property tab

- click the little lightning in the top of that window to display the events

- double ciick the empty box beside "AfterSelect"

 

This will create the event for you then simply do something like this :

private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
    // e.Node contain the currently clicked/selected node
    var selectedNode = e.Node;
  
    // here call your list box filling method
    FillListBox(selectedNode.Tag.ToString());
}

private void FillListBox(string drivePath)
{
    // clear items
    ListBox1.Items.Clear();
  
    // get list of files
    var files = new List<string>(); // fill files
  
    // add each file to the listbox
    foreach(var file in files)
    {
        ListBox1.Items.Add(file);
    }
}

as for this 

34 minutes ago, ughiwanthackintosh said:

i cannot find this anywhere else and stackoverflow idk how to use <code tags> so i cant post anything there

Stack you copy your code and simply Select all of it and click the button format code. You can also manually format it that is done by ensuring you have a minimum of 4 spaces before any code line. For indentation use 4 space too (if you format by hand not by the button)

Link to comment
Share on other sites

Link to post
Share on other sites

On 7/5/2019 at 6:18 PM, Franck said:

 

the Treeview control triggers a 'AfterSelect' event that is what you want to use.

- Click you TreeView

- go to the property tab

- click the little lightning in the top of that window to display the events

- double ciick the empty box beside "AfterSelect"

 

This will create the event for you then simply do something like this :


private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
    // e.Node contain the currently clicked/selected node
    var selectedNode = e.Node;
  
    // here call your list box filling method
    FillListBox(selectedNode.Tag.ToString());
}

private void FillListBox(string drivePath)
{
    // clear items
    ListBox1.Items.Clear();
  
    // get list of files
    var files = new List<string>(); // fill files
  
    // add each file to the listbox
    foreach(var file in files)
    {
        ListBox1.Items.Add(file);
    }
}

as for this 

Stack you copy your code and simply Select all of it and click the button format code. You can also manually format it that is done by ensuring you have a minimum of 4 spaces before any code line. For indentation use 4 space too (if you format by hand not by the button) 

thanks ? will test the code now thanks :) for the explain of stack :)

Link to comment
Share on other sites

Link to post
Share on other sites

  • 1 month later...

What Now? it does not do anything no errors, but nothing happends.

also i changed project..

On 7/5/2019 at 9:18 AM, Franck said:

 

the Treeview control triggers a 'AfterSelect' event that is what you want to use.

- Click you TreeView

- go to the property tab

- click the little lightning in the top of that window to display the events

- double ciick the empty box beside "AfterSelect"

 

This will create the event for you then simply do something like this :


private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
    // e.Node contain the currently clicked/selected node
    var selectedNode = e.Node;
  
    // here call your list box filling method
    FillListBox(selectedNode.Tag.ToString());
}

private void FillListBox(string drivePath)
{
    // clear items
    ListBox1.Items.Clear();
  
    // get list of files
    var files = new List<string>(); // fill files
  
    // add each file to the listbox
    foreach(var file in files)
    {
        ListBox1.Items.Add(file);
    }
}

as for this 

Stack you copy your code and simply Select all of it and click the button format code. You can also manually format it that is done by ensuring you have a minimum of 4 spaces before any code line. For indentation use 4 space too (if you format by hand not by the button)

nope no difference the box is just blank. no folders / file names are showing up

Link to comment
Share on other sites

Link to post
Share on other sites

On 8/7/2019 at 3:49 PM, ughiwanthackintosh said:

nope no difference the box is just blank. no folders / file names are showing up

You have a problem in your code somewhere else. This code works #1 i just tried it in an empty form.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×