Click here to Skip to main content
15,910,872 members
Articles / Programming Languages / C#

MemoryBox: A Yes/No/Yes to all/No to all Dialog for C#

Rate me:
Please Sign up or sign in to vote.
3.58/5 (11 votes)
29 Jan 2007CPOL 54.9K   846   22   6
MemoryBox is a Yes/No/Yes to all/No to all dialog with a similar implementation to the standard Windows MessageBox.

Introduction

MemoryBox is a simple to use MessageBox-style dialog with memory functionality. It has "Yes to All" and "No to All" options like many file dialogs, and can be used in many operations.

Sample screenshot

Implementing

First, you must add the MemoryBox to your project. This is done by using the Add Existing Item option in your project navigator. The source file you need is included in the MemoryBox demo, called "MemoryBox.cs".

MemoryBox should be declared and constructed outside of the loop in which you wish to use the MemoryBox, as shown in the example below:

C#
// Memory box is declared before the loop
MemoryBox memoryBox = new MemoryBox();
// Loop 5 times, counting the yeses and noes.
for (int i = 0; i < 5; i++)
{
    MemoryBoxResult result = memoryBox.ShowMemoryDialog("This demo was " + 
       "written by Chris Johanson of Twin Rose " + 
       "Software.\r\nYou will be asked 5 times a yes or no, " + 
       "and given a running total.\r\n", "Demo Memory Box");
    if (result == MemoryBoxResult.Cancel)
        break;
    if (result == MemoryBoxResult.Yes || result == MemoryBoxResult.YesToAll)
    {
        yesCount++;
    }
    if (result == MemoryBoxResult.No || result == MemoryBoxResult.NoToAll)
    {
        noCount++;
    }
    label1.Text = "Yes: " + yesCount + " No: " + noCount;
}

That should do it! If you have any comments, questions, or suggestions, I would love to hear them!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generaltoo many buttons Pin
colin39919-Aug-09 10:14
colin39919-Aug-09 10:14 
QuestionUse it for...? Pin
zeltera29-Jan-07 21:36
zeltera29-Jan-07 21:36 
AnswerRe: Use it for...? Pin
Chris Johanson29-Jan-07 23:14
Chris Johanson29-Jan-07 23:14 
Sure! Your users will get tired if you make them answer the same question, over and over again. I ran into this with an import from the first version of my software, into the second. The same would be true with most file operations - "Do you wish to overwrite existing?"

Perhaps I wasn't clear - I assumed that anyone reading the article had been given a choice of "Yes, No, Yes to all, or Not to all." If you don't want your users to experience this, and you want a more complex dialog, this won't help you.

Essentially, what you want for the end-user experience is this: He chooses to apply certain changes. A loop begins, which gives options of "Yes" or "No". When "Yes to all" is clicked, the question is bypassed... and the answer is always yes. When "No to all" is the choice... Well, you get the idea.

Thank you for the question, feel free to ask any more. Smile | :) I am sad that someone decided to give me a bad rating with the comment quoting Dave Berry Frown | :( If you have a problem with my free code, please... Give actual comments.

GeneralRe: Use it for...? Pin
Chris Johanson29-Jan-07 23:22
Chris Johanson29-Jan-07 23:22 
GeneralGood job Pin
aprenot29-Jan-07 13:05
aprenot29-Jan-07 13:05 
GeneralRe: Good job Pin
Jason McBurney29-Jan-07 13:34
Jason McBurney29-Jan-07 13:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.