调用组合框的选定文件夹(C#)

时间:2016-09-01 11:44:00

标签: c# .net visual-studio combobox

我正在使用两个组合框,其中第一个框将在该下拉列表中显示指定位置的文件夹我将选择所需的文件夹,因此选择后第二个组合框应该仅列出文件,具体取决于第一个框&# 39;选择的文件夹。(注意:我没有使用数据库,只是访问计算机文件夹)。我正在使用C#的visual studio 2013。提前谢谢。

DirectoryInfo di = new DirectoryInfo(@"C:\Users\jeeva\Desktop\1234");
DirectoryInfo[] folders = di.GetDirectories(); 
comboBox1.DataSource = folders; 
string selected =comboBox1.SelectedItem.ToString(); 
String fullpath = Path.Combine(@"C:\Users\jeeva\Desktop\1234", selected);
DirectoryInfo di1 = new DirectoryInfo(fullpath); 
DirectoryInfo[] folders1 = di1.GetDirectories(); 
comboBox2.DataSource = folders1

5 个答案:

答案 0 :(得分:1)

基本上分2步完成。您只需要在代码中将它们分开。

1)将文件夹放入组合框中(可能已经在Form的构造函数中):

DirectoryInfo di = new DirectoryInfo(@"C:\Users\jeeva\Desktop\1234");
DirectoryInfo[] folders = di.GetDirectories(); 
comboBox1.DataSource = folders; 

2)双击设计器中的comboBox1。这将创建一个在用户选择项目时触发的事件。在其中,您可以获取所有子文件夹,并将DataSource作为ComboBox提供给private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { string selected =comboBox1.SelectedItem.ToString(); String fullpath = Path.Combine(@"C:\Users\jeeva\Desktop\1234", selected); DirectoryInfo di1 = new DirectoryInfo(fullpath); DirectoryInfo[] folders1 = di1.GetDirectories(); comboBox2.DataSource = folders1 } ,然后显示它们。

explode

答案 1 :(得分:0)

您可以使用Directory.GetFiles("folderName")方法填充第二个组合框。 Directory类位于System.IO命名空间。

答案 2 :(得分:0)

在Combobox1的Onchange事件中:

ComboBox2.dataSource = System.IO.Directory.GetFiles(ComboBox1.SelectedText)

这应该填充Combobox2中的所有文件

答案 3 :(得分:0)

您可以通过以下方式获取文件夹列表:

DirectoryInfo obj = new DirectoryInfo("E:\\");//you can set your directory     path here
DirectoryInfo[] folders = obj.GetDirectories();
YourDirCombo.DataSource = folders ;

您可能有一种方法可以听取您的第一次组合更改:

private void YourDirCombo_SelectedIndexChanged(object sender, EventArgs e)

现在你可以获得所选文件夹的文件:

string [] fileEntries = Directory.GetFiles(YourDirCombo.SelectedValue);
YourFileCombo.DataSource = fileEntries;

答案 4 :(得分:0)

以下几种方式可以进行优化和推广 - 我正在“长手”地为您提供尽可能多的信息。

private void initializeComboBoxes()
    {
        ComboBox c = new ComboBox();
        c.Name = "cbx_One";
        c.Items.Add("Select a File");
        foreach(string direc in System.IO.Directory.GetDirectories(@"PathToYourFiles"))
        {
            c.Items.Add(direc);
        }
        c.SelectedIndex = 0;
        c.SelectedIndexChanged += loadComboBox2;
        Controls.Add(c);
        ComboBox c1 = new ComboBox();
        c1.Name = "cbx_Two";
        c1.Items.Add("Waiting for file selection");
        c1.SelectedIndex = 0;
        c1.SelectedIndexChanged += loadFile;
        Controls.Add(c1);
        areComboBoxesUpdating = false;
    }
    bool areComboBoxesUpdating = true;
    protected void loadComboBox2(object sender, EventArgs e)
    {
        if (!areComboBoxesUpdating)
        {
            ComboBox c1 = sender as ComboBox;
            ComboBox c2 = Controls.Find("cbx_Two", true)[0] as ComboBox;
            c2.Items.Clear();
            if (c1.SelectedIndex == 0)
            {
                c2.Items.Add("Waiting for file selection");
            }
            else
            {
                c2.Items.Add("Please select a file");
                //assuming c1 is the list of directories
                foreach (string file in System.IO.Directory.GetFiles(c1.SelectedItem.ToString()))
                {
                    c2.Items.Add(Path.GetFileName(c1.SelectedItem.ToString()));
                }
            }
            areComboBoxesUpdating = true;
            c2.SelectedIndex = 0;
            areComboBoxesUpdating = false;
        }
    }
    protected void loadFile(object sender, EventArgs e)
    {
        //a selection has been made from the second box - you have directory in box1 and filename in box2
        ComboBox c = sender as ComboBox;
        if (c.SelectedIndex > 0)
        {
            string directory = ((ComboBox)Controls.Find("cbx_One", true)[0]).SelectedItem.ToString();
            string file = c.SelectedItem.ToString();
            //do something
        }
    }