开发者

C# WinForms - Multiselect not working on OpenFileDialog & Listbox

Hey Everyone, Sorry to bother you with this, but I'm having an issue with selecting multiple xlsx files through a file browser window in a winforms application when debugging and can't figure out what I did wrong.

Problem: I set Multiselect=true under the OpenFileDialog but I still cannot select more than one file.

  • What do I n开发者_开发知识库eed to change to get the multiSelect feature to work?
  • Do I need to add anything under sourceFileOpenFileDialog method?
  • Do I need to add anything under listBoxSourceFiles_SelectedIndexChanged method to get the filenames to load correclty in the listbox?

    // 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)
    {
    
    }
    

I updated the code to reflect sourceFileOpenFileDialog and the MultiSelect or Title doesn't work... Perhaps I'm referencing the onfiledialog wrong? is this the proper prefix to use?

Thanks for looking!


You are using two OpenFileDialogs. You display sourceFilesList but you initialized sourceFileOpenFileDialog. Using consistent naming rules religiously is a great way to avoid bugs like these btw.

Next problem, what is OpenFile() supposed to do when you selected more than one file? What is myStream actually used for?


You are setting up sourceFileOpenFileDialog but then use sourceFileList!!! Make up your mind and only use one.


Fixed the non-working MultiSelect by:

  • Updating the code to only use one variable sourceFileOpenFileDialog throughout the method and the MultiSelect or Title didnt work...
  • Removing all references to myStream. myStream was used in an example which i based my code off but i took it out and the multiSelect works!

Here's the working code:

    // 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)
    {

    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜