Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a WinForm Application which takes the structure of two TreeViews and implements them as Folders into the path which the users chose in the Drop Down.

The Drop Down currently gets all the chooseable folders from Z:

Now my TreeView loremPath has the right Drive with Z: but ipsumPath should go into R: but with the same Drop Down - Because the second Drive has the exact folder structure as Z: so instead of building a whole new Drop Down, I just need to change the path in ipsumPath to R: and can use one Drop Down for both Treeviews.

So I had a previous Question on StackOverflow and I got recommended to use Hardcoded paths for both TreeViews, but I can't figure out how to implement that.


My whole Code:

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using IWshRuntimeLibrary;
using System.Reflection;

namespace Form1
{
    public partial class Form1 : Form
    {
        [DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
        private static extern IntPtr CreateRoundRectRgn
         (
             int nLeftRect,
             int nTopRect,
             int nRightRect,
             int nBottomRect,
             int nWidthEllipse,
             int nHeightEllipse
         );

        public Form1()
        {
            InitializeComponent();
            this.FormBorderStyle = FormBorderStyle.None;
            Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 20, 20));
            foreach (TreeNode tn in loremPath.Nodes)
            {
                tn.Expand();
            }
            foreach (TreeNode tn in ipsumPath.Nodes)
            {
                tn.Expand();
            }
            loremDropDown.DisplayMember = "Name";
            loremDropDown.ValueMember = "FullName";
            loremDropDown.DataSource = new DirectoryInfo("Z:\\").GetDirectories();
            string folder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            CreateShortcutToCurrentAssembly(folder);
        }

        private void CreateShortcutToCurrentAssembly(string saveDir)
        {
            WshShell wshShell = new WshShell();
            string fileName = saveDir + "\\" + Application.ProductName + ".lnk";
            IWshShortcut shortcut = (IWshShortcut)wshShell.CreateShortcut(fileName);
            shortcut.TargetPath = Application.ExecutablePath;
            shortcut.Save();
        }

        private void close_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.Application.Exit();
        }

        private void loremPath_AfterCheck(object sender, TreeViewEventArgs e)
        {
            if (e.Action == TreeViewAction.Unknown) return;

            foreach (TreeNode n in e.Node.Children())
                n.Checked = e.Node.Checked;

            foreach (TreeNode p in e.Node.Parents())
                p.Checked = p.Nodes.OfType().Any(n => n.Checked);
        }

        private IEnumerable GetCheckedNodes(TreeNodeCollection nodeCol)
        {
            foreach (TreeNode node in nodeCol)
            {
                if (node.Checked ||
                    node.Nodes.Cast().Any(n => n.Checked))
                {
                    yield return node;
                }

                foreach (TreeNode childNode in GetCheckedNodes(node.Nodes))
                {
                    if (childNode.Checked)
                        yield return childNode;
                }
            }
        }
        private void projektordnerGenerieren_Click(object sender, EventArgs e)
        {
            var destPath = loremDropDown.SelectedValue.ToString();
            var treeSep = loremPath.PathSeparator;
            var dirSep = Path.DirectorySeparatorChar.ToString();

            foreach (var node in GetCheckedNodes(loremPath.Nodes))
            {
                var sPath = Path.Combine(destPath, node.FullPath.Replace(treeSep, dirSep));
                Directory.CreateDirectory(sPath);
            }


        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            this.loremPath.SelectedNode = this.loremPath.Nodes[0];
            this.ipsumPath.SelectedNode = this.ipsumPath.Nodes[0];
            loremPath.SelectedNode.Text = textBox1.Text;
            ipsumPath.SelectedNode.Text = textBox1.Text;
        }

        private void ipsumPath_AfterCheck(object sender, TreeViewEventArgs e)
        {
            if (e.Action == TreeViewAction.Unknown) return;

            foreach (TreeNode n in e.Node.Children())
                n.Checked = e.Node.Checked;

            foreach (TreeNode p in e.Node.Parents())
                p.Checked = p.Nodes.OfType().Any(n => n.Checked);
        }

        public const int WM_NCLBUTTONDOWN = 0xA1;
        public const int HT_CAPTION = 0x2;

        [System.Runtime.InteropServices.DllImport("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        public static extern bool ReleaseCapture();

        private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
            }
        }

        private void alleErweitern_Click(object sender, EventArgs e)
        {
            foreach (TreeNode tn in loremPath.Nodes)
            {
                tn.Expand();
            }
            foreach (TreeNode tn in ipsumPath.Nodes)
            {
                tn.Expand();
            }
        }

        private void alleReduzieren_Click(object sender, EventArgs e)
        {
            foreach (TreeNode tn in loremPath.Nodes)
            {
                tn.Collapse();
            }
            foreach (TreeNode tn in ipsumPath.Nodes)
            {
                tn.Collapse();
            }
        }

        private void minimize_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }
    }
    static class TreeViewExtensions
    {
        public static IEnumerable Children(this TreeNode node)
        {
            foreach (TreeNode n in node.Nodes)
            {
                yield return n;

                foreach (TreeNode child in Children(n))
                    yield return child;
            }
        }

        public static IEnumerable Parents(this TreeNode node)
        {
            var p = node.Parent;

            while (p != null)
            {
                yield return p;

                p = p.Parent;
            }
        }
    }
}


What I have tried:

I tried something like:

C#
var testPath= new DirectoryInfo("R:\\").GetDirectories();
        var treeSeperator = ipsumPath.PathSeparator;
        var dirSep = Path.DirectorySeparatorChar.ToString();

        foreach (var node in GetCheckedNodes(ipsumPath.Nodes))
        {
            var sPath = Path.Combine(testPath.ToString(), node.FullPath.Replace(treeSeperator, dirSep));
            Directory.CreateDirectory(sPath);
        }

But that didn't work at all.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900