如何使用comboBox项目选择? [英] How do I work with comboBox item selection?

查看:94
本文介绍了如何使用comboBox项目选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Windows应用程序。我使用2个组合框,其中第一个comboBox1列出物理目录中的所有文件夹,comboBox2列出comboBox1中列出的文件夹中的所有文件,当从comboBox1中选择特定文件夹名称时。



我试图弄清楚如何在comboBox2中为每个可用文件夹列出文件夹中的所有文件,并检查文件夹中文件的数量,当文件夹中的文件到达文件末尾时,所选索引应移动comboBox1中的下一个文件夹。



由于文件夹在每个文件夹中没有相同数量的文件,我正在尝试像递归一样。



有任何建议和帮助表示赞赏吗?



ComboBox1:列出物理位置的所有文件夹。(工作正常)

I am working on windows application. I am using 2 comboboxes, where first comboBox1 lists all the folders in the physical directory and comboBox2 lists all the files from the folders listed in comboBox1, when particular folder name is selected from comboBox1.

I am trying to figure out the way to list all the files from the folder in comboBox2 for every available folder and check count of files within the folder and when files in the folder reached end of file, selected index should move to next folder in the comboBox1.

As folder don't have same number of files in each folder I am trying something like recursion.

Any suggestions and help is appreciated?

ComboBox1: Lists all the folders from physical location.(Works fine)

private void Form_Load(object sender, EventArgs e)
{
    DirectoryInfo di = new DirectoryInfo("@\\server\CAMR");
    paths = new String[di.GetDirectories().Count()];
    int i = 0;
    foreach (DirectoryInfo fi in di.GetDirectories())
    {
       comboBox1.Items.Add(fi.Name);
    }

    foreach (DirectoryInfo fi in di.GetDirectories())
    {
       paths[i] = fi.FullName;
       i++;
       comboBox2.Sorted = true;
    }
    comboBox1.Text = "";
}



ComboBox2:截至目前,我正在为comboBox1中的每个选定索引手动完成


ComboBox2: As of now I am doing it manually for each selected index from comboBox1

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{

    if (comboBox1.SelectedIndex == 1)
    {
        DirectoryInfo dinf = new DirectoryInfo(@"\\server\CAMR\Mxxxxxxxxxxx");
        paths = new String[dinf.GetFiles().Count()];
        int i = 0;
        comboBox2.Items.Clear();
        pictureBox2.Image = Image.FromFile("@\\server\CAMR\Mxxxxxxxxxxx\SCN0001.tif");
        foreach (FileInfo finf in dinf.GetFiles())
        {
            comboBox2.Items.Add(finf.Name);
        }

        foreach (FileInfo finf in dinf.GetFiles())
        {
            paths[i] = finf.FullName;
            i++;
        }
        comboBox2.Text = "SCN0001.tif";
        txtovrbn.Text = comboBox1.Text;
        txtovrts.Text = DateTime.Now.ToString();
    }
}





我想在这里找到递归,所以它会处理所有的文件夹在comboBox1中列出。



I am trying to find recursion here, so it will take care of all the folder listed in comboBox1.

推荐答案

在第二个组合框上使用事件SelectedValueChanged或SelectedIndexChanged,检查currentIndex == combobox.Items.Count -1然后是combobox1.SelectedIndex = combobox1.SelectedIndex +1





Use event SelectedValueChanged or SelectedIndexChanged on second combobox, check that currentIndex == combobox.Items.Count -1 then combobox1.SelectedIndex = combobox1.SelectedIndex +1


protected void combobox2_SelectedIndexChanged(object sender, EventArgs e) {
    if (combobox2.SelectedIndex = combobox2.Items.count -1) {
            // full code for clarity, there are several shortcuts for this
            combobox1.SelectedIndex = combobox1.SelectedIndex +1

// this index change should reload cb2
    }
}


这篇关于如何使用comboBox项目选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆