HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Keep asking the user [after performing an action] until they wish to stop

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
afterperformingtheuntiluserkeepwishactionstopasking

Problem

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;
                }
            }
        }
    }

Solution

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

Code Snippets

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

Context

StackExchange Code Review Q#48302, answer score: 4

Revisions (0)

No revisions yet.