Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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;
                }
            }
        }
    }
share|improve this question
1  
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. –  Sandeep Apr 27 at 6:08
    
Thank you @Sandeep for your suggestion. Good suggestion :-) –  spike.y Apr 27 at 6:20
add comment

2 Answers 2

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)
    }
}
share|improve this answer
add comment

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;
        }
    }
}
share|improve this answer
    
Thank you @dreza, I will use this. :-) –  spike.y Apr 27 at 6:21
1  
@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.... –  dreza Apr 27 at 6:24
    
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. :) –  spike.y Apr 27 at 7:23
1  
@spike.y you can always upvote for now :) –  dreza Apr 27 at 7:26
    
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. –  spike.y Apr 27 at 7:41
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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