akkbkht 14 Newbie Poster

I am preparing a CSharp based desktop app in VS 2019. I have 4 ComboBoxes. On the form load, the comboBox1 fetches folder names in root directory in D drive. The structure is Folder The sub folder Then sub folder then MS word documents in the 4th folder. For example, Windows- -> IT ->Print-> MS word document. What I am looking for is when I click on the folder name in comboBox1, the sub folder in the D drive in that folder should automatically populated. I am facing 2 problems.

Folder are fetching in comboBox1 and the sub folders are automatically fetching in comboBox2. But when I select any folder in comboBox2, nothing is happening in comboBox 3. 2nd problem is that, If I select any folder in comboBox1, I can see sub-folders in comboBox2, but when I change the folder name in comboBox1, i can still see the previous sub-folder in comboBox2. I need it the changes to take affect on run-time. What I have tried so far is

namespace ITSDClnt 
     public partial class Form1 : Form { private string rootFolderPath = @"D:\ITSD-Test"; // Replace with your D drive path

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        PopulateComboBox(comboBox1, rootFolderPath);
    }

    private void PopulateComboBox(ComboBox comboBox, string folderPath)
    {
        if (Directory.Exists(folderPath))
        {
            string[] subdirectories = Directory.GetDirectories(folderPath);
            comboBox.Items.Clear();
            comboBox.Items.AddRange(subdirectories.Select(Path.GetFileName).ToArray());
        }
        else
        {
            MessageBox.Show("The specified directory does not exist.");
        }
    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        comboBox2.Items.Clear();
        comboBox3.Items.Clear();
        comboBox4.Items.Clear();

        string selectedFolderName = comboBox1.SelectedItem?.ToString();
        if (!string.IsNullOrEmpty(selectedFolderName))
        {
            string selectedFolderPath1 = Path.Combine(rootFolderPath, selectedFolderName);
            PopulateComboBox(comboBox2, selectedFolderPath1);
        }
    }

    private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
    {
        comboBox3.Items.Clear();
        comboBox4.Items.Clear();

        string selectedFolderName = comboBox2.SelectedItem?.ToString();
        if (!string.IsNullOrEmpty(selectedFolderName))
        {
            string selectedFolderPath2 = Path.Combine(rootFolderPath, comboBox1.SelectedItem.ToString(), selectedFolderName);
            PopulateComboBox(comboBox3, selectedFolderPath2);
        }
    }

    private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
    {
        comboBox4.Items.Clear();

        string selectedFolderName = comboBox3.SelectedItem?.ToString();
        if (!string.IsNullOrEmpty(selectedFolderName))
        {
            string selectedFolderPath3 = Path.Combine(rootFolderPath, comboBox1.SelectedItem.ToString(), comboBox2.SelectedItem.ToString(), selectedFolderName);
            PopulateFilesComboBox(selectedFolderPath3);
        }
    }

    private void PopulateFilesComboBox(string folderPath)
    {
        if (Directory.Exists(folderPath))
        {
            string[] files = Directory.GetFiles(folderPath, "*.docx");
            comboBox4.Items.Clear();
            comboBox4.Items.AddRange(files.Select(Path.GetFileName).ToArray());
        }
        else
        {
            MessageBox.Show("The specified folder does not exist.");
        }
    }

    private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
    {
        string selectedFileName = comboBox4.SelectedItem?.ToString();
        if (!string.IsNullOrEmpty(selectedFileName))
        {
            string selectedFolderPath3 = Path.Combine(rootFolderPath, comboBox1.SelectedItem.ToString(), comboBox2.SelectedItem.ToString(), comboBox3.SelectedItem.ToString());
            string filePath = Path.Combine(selectedFolderPath3, selectedFileName);

            if (File.Exists(filePath))
            {
                string fileContent = File.ReadAllText(filePath);
                richTextBox1.Text = fileContent;
            }
            else
            {
                MessageBox.Show("The selected file does not exist.");
            }
        }
    }
}
}
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.