5
\$\begingroup\$

I have a form that requires the user to select photos to be included in their portfolio before they can proceed to use the app for the first time.

I don't want to spend the rest of my life implementing a custom file browser, so I've decided to just use the FolderBrowserDialog to allow them to select a folder containing photos. I've also decided to let them browse as many times as they need in case they have photos scattered in other locations.

I can't think of a better way to write this, but it seems like there would be a better way. I'd appreciate any thoughts/suggestions on how to improve this piece or advice on a better way to write it:

    private void Main_Shown(object sender, EventArgs e)
    {
        /* Let them select photos to be included in the portfolio
         * and move them to the permanent Photo Collection folder,
         * and display them on the form.
         */

        bool selectingPhotos = true;

        while (selectingPhotos) {
            using (FolderBrowserDialog folderPicker = new FolderBrowserDialog())
            {
                folderPicker.Description = "Please browse to, and select the folder containing the files you'd like to add.";

                if (folderPicker.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
                {
                    // Add files to list for processing later.

                    // Ask if they want to add more files from another directory.
                    if (MessageBox.Show("Do you want to select more photos from another location?", "Confirmation", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                    {
                        selectingPhotos = true;
                    }
                    else
                    {
                        selectingPhotos = false;
                    }
                }
                else
                {
                    selectingPhotos = false;
                }
            }
        }
    }
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Since it is a winform application so I would rather change its UI. Most of the applications that I have come across offers a "Add More" button to do the same. \$\endgroup\$
    – Sandeep
    Apr 27, 2014 at 6:08
  • \$\begingroup\$ Thank you @Sandeep for your suggestion. Good suggestion :-) \$\endgroup\$
    – spike.y
    Apr 27, 2014 at 6:20

2 Answers 2

4
\$\begingroup\$

Since you have a task you want to repeat at least once, the do-while is the most suitable. How about the following:

private void Main_Shown(object sender, EventArgs e)
{
    /* Let them select photos to be included in the portfolio
     * and move them to the permanent Photo Collection folder,
     * and display them on the form.
     */

    using (FolderBrowserDialog folderPicker = new FolderBrowserDialog())
    {
        folderPicker.Description = "Please browse to, and select the folder containing the files you'd like to add.";

        do
        {               
            if (folderPicker.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                // Add files to list for processing later.
            }
            else
            {
                break;
            }

        } while (MessageBox.Show("Do you want to select more photos from another location?", "Confirmation", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
    }
}
\$\endgroup\$
3
\$\begingroup\$

I'm typically not a user of the continue statement but here is one alternative using this method. In this case it may be a simpler implementation and removes the need for any extra local variable.

private void Main_Shown(object sender, EventArgs e)
{
    /* Let them select photos to be included in the portfolio
     * and move them to the permanent Photo Collection folder,
     * and display them on the form.
     */

    using (FolderBrowserDialog folderPicker = new FolderBrowserDialog())
    {
        folderPicker.Description = "Please browse to, and select the folder containing the files you'd like to add.";

        while (true) 
        {               
            if (folderPicker.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                // Add files to list for processing later.

                // Ask if they want to add more files from another directory.
                if (MessageBox.Show("Do you want to select more photos from another location?", "Confirmation", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                {
                    continue;
                }
            }

            break;
        }
    }
}
\$\endgroup\$
4
  • 1
    \$\begingroup\$ @spike.y no problem. Just a suggestion. If you leave the question un-accepted for a few days you are more likely to get a few more comments, many of which may be better suggestions than mine :) Many ways to skin a cat so to speak.... \$\endgroup\$
    – dreza
    Apr 27, 2014 at 6:24
  • \$\begingroup\$ Lol. Cool, I'm using your code right now and it's working out great plus it's a bit easier to manage. I will wait a few days and see if anything else comes along. :) \$\endgroup\$
    – spike.y
    Apr 27, 2014 at 7:23
  • 1
    \$\begingroup\$ @spike.y you can always upvote for now :) \$\endgroup\$
    – dreza
    Apr 27, 2014 at 7:26
  • \$\begingroup\$ I did try :-) But I don't have enough reputation to up-vote. I'm looking for answers that I can answer so I can. I will be back. \$\endgroup\$
    – spike.y
    Apr 27, 2014 at 7:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.