C#WinForms - Multiselect无法在OpenFileDialog&列表框

时间:2011-03-29 21:04:50

标签: c# winforms listbox openfiledialog multi-select

嘿大家, 很抱歉打扰你,但我在调试时通过winforms应用程序中的文件浏览器窗口选择多个xlsx文件时遇到问题,并且无法弄清楚我做错了什么。

问题:我在OpenFileDialog下设置了Multiselect = true,但我仍然无法选择多个文件。

  • 我需要更改什么才能使multiSelect功能正常工作?
  • 我是否需要在sourceFileOpenFileDialog方法下添加任何内容?
  • 我是否需要在listBoxSourceFiles_SelectedIndexChanged方法下添加任何内容以获取文件名以在列表框中加载correclty?

    // When the user clicks on Select Files Button, this happens  
    private void sourceFiles_Click(object sender, EventArgs e)
    {
        Stream myStream;
        int i = 0;
        OpenFileDialog sourceFileOpenFileDialog = new OpenFileDialog();
    
        this.sourceFileOpenFileDialog.InitialDirectory = "i:\\CommissisionReconciliation\\Review\\";
        this.sourceFileOpenFileDialog.Filter = "Excel Files (*.xls;*.xlsx;)|*.xls;*.xlsx;|" + "All Files (*.*)|*.*";
        this.sourceFileOpenFileDialog.FilterIndex = 2;
        this.sourceFileOpenFileDialog.RestoreDirectory = true;
        this.sourceFileOpenFileDialog.Multiselect = true;
        this.sourceFileOpenFileDialog.Title = "Please Select Excel Source File(s) for Consolidation";
    
        if (sourceFileOpenFileDialog.ShowDialog() == DialogResult.OK)
        {
            try
            {
                if ((myStream = sourceFileOpenFileDialog.OpenFile()) != null)
                {
                    using (myStream)
                    {
                        foreach (string FileName in sourceFileOpenFileDialog.FileNames)
                        {
                            sourceFileOpenFileDialog.FileNames[i] = FileName;
                            listBoxSourceFiles.Items.Add(FileName);
                            Log("Source Files: " + sourceFileOpenFileDialog.FileNames[i]);
                            i++;
                        }
                    }
                }
    
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
            }
    }
    
    //method for the listbox. Do I need anything here? 
    private void listBoxSourceFiles_SelectedIndexChanged(object sender, EventArgs e)
    {
    
    }
    
    //method for the sourceFileOpenFileDialog.  Do I need anything here?
    private void sourceFileOpenFileDialog_FileOk(object sender, CancelEventArgs e)
    {
    
    }
    

我更新了代码以反映sourceFileOpenFileDialog并且MultiSelect或Title不起作用...也许我引用了onfiledialog错误?这是正确的前缀吗?

感谢您的期待!

3 个答案:

答案 0 :(得分:4)

您正在使用两个 OpenFileDialogs。您显示sourceFilesList但是您初始化了sourceFileOpenFileDialog。虔诚地使用一致的命名规则是避免像btw这样的错误的好方法。

下一个问题,当您选择多个文件时,OpenFile()应该做什么? myStream实际上用于什么?

答案 1 :(得分:1)

您正在设置sourceFileOpenFileDialog但是然后使用sourceFileList !!!下定决心,只使用一个。

答案 2 :(得分:0)

通过以下方式修复了不工作的MultiSelect:

  • 更新代码只在整个方法中使用一个变量sourceFileOpenFileDialog,而MultiSelect或Title也没有用......
  • 删除对myStream的所有引用。 myStream用在一个例子中,我把我的代码关闭了,但我拿出来了,multiSelect有效!

这是工作代码:

    // When the user clicks on Select Files Button, this happens  
    private void sourceFiles_Click(object sender, EventArgs e)
    {
        Stream myStream;
        int i = 0;
        OpenFileDialog sourceFileOpenFileDialog = new OpenFileDialog();

        this.sourceFileOpenFileDialog.InitialDirectory = "i:\\CommissisionReconciliation\\Review\\";
        this.sourceFileOpenFileDialog.Filter = "Excel Files (*.xls;*.xlsx;)|*.xls;*.xlsx;|" + "All Files (*.*)|*.*";
        this.sourceFileOpenFileDialog.FilterIndex = 2;
        this.sourceFileOpenFileDialog.RestoreDirectory = true;
        this.sourceFileOpenFileDialog.Multiselect = true;
        this.sourceFileOpenFileDialog.Title = "Please Select Excel Source File(s) for Consolidation";

        if (sourceFileOpenFileDialog.ShowDialog() == DialogResult.OK)
        {
            try
            {
                string tempFolder = System.IO.Path.GetTempPath();

                foreach (string FileName in this.sourceFileOpenFileDialog.FileNames)
                {
                    this.sourceFileOpenFileDialog.FileNames[i] = FileName;
                    listBoxSourceFiles.Items.Add(FileName);
                    Log("Source Files: " + sourceFileOpenFileDialog.FileNames[i]);
                    i++;
                    System.IO.File.Copy(FileName, tempFolder + @"\" + FileName);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
            }
    }

    //method for the listbox. Do I need anything here? 
    private void listBoxSourceFiles_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    //method for the sourceFileOpenFileDialog.  Do I need anything here?
    private void sourceFileOpenFileDialog_FileOk(object sender, CancelEventArgs e)
    {

    }