I was using a MessageBox to show a confirmation message when the user goes to delete an item and initially used MessageBoxButtons.YesNoCancel. Later I changed it to YesNo instead because a user pointed out that there was no real difference in "No" and "Cancel" in this case. My question is...what is the difference? Is there ever a reason to use YesNoCancel instead of YesNo?
In your case there is no difference as your question results in just one action and then finishes.
In standard usage, the Yes No Cancel usually asks a question, Yes or No will chose a different action and then proceed to do yet another action (like quitting a form), Cancel will abandon all actions.
For example: quitting Word, do you wish to save? "Yes, No, Cancel". Yes and No will continue to quit with or without saving, cancel will not save or quit.
Whatever you do, make sure Cancel does what the user expects most - I forever spam cancel if bombarded with message boxes when I have got to focus on something else. If I cancel something I don't want to lose a lot of work because I didn't have time to stop and handle it properly.
Users abuse cancel :-)
Sure there could be. For example, if there is a save dialog and you enter a filename that already exists, the dialog could ask you if you want to overwrite the file.
Yes would mean overwrite the file. No might mean append a "(1)" at the end of the file name, or prompt for a different file name. Cancel might mean don't save after-all.
You should note that Yes, No, and Cancel are all different enums and do not have the same value, so you can treat them differently.
REMEMBER TO HANDLE CANCEL ANYWAY because if the user clicks the x button in the top right corner of your dialog screen, the result of ShowDialog() is DialogResult.Cancel!
Here's an example of when YesNoCancel would be appropriate:
"Would you like to save your changes before quitting?"
Yes - Save and quit
No - Don't save and quit
Cancel - I pressed the button on accident, don't quit.
Im sure its easy enough to come up with a scenario where there would be a symantic difference between "No" as a response and "Cancel". Traditionally "Cancel" should return the program to its state before starting the current series of operations. "No" certainly doesnt have this same rule.
Example:
"Do you wish to delete file 4 of 10?"
Yes: Delete the file
No: Dont delete the file, move on to file 5 of 10
Cancel: Exit this operation and return to having not deleted any files.
I guess "Cancel" is used to abort the whole operation. If you are dealing with a huge procedure, for example, moving a set of files from one directory to another, you may want to ask something to the user about a specific file - for instance, to confirm if the user really wants to move a protected file. If the user presses "No", you ignore that item and continue the action. If the user presses "Cancel", you abort the whole action (and, maybe, rollback the previous action).
Of course, for a small procedure or a simple situation, "Cancel" and "No" have no difference.
Here's an example:
Say you are exiting an application with an unsaved file, like a word processor.
There is a confirmation when exiting that says: "Your file has changes that haven't been saved. Would you like to save them?"
In this case:
Yes = save the file and exit
No = exit and lose the changes
Cancel = abort the exit and go back to the application
Related
I have a list of tasks running and would like to show the progress in a (WinForms) form with a Cancel button.
I am aware, that there are several async options, but I have two restraints: The tasks must not run on a separate thread and the solution must be compatible with .NET 3.5 (it is an AddIn for a program, I have no access to).
It is fine, if one task finishes, before the cancellation comes into force. So I wonder, if there is some chance to check in synchronous code, if a mouse click on a button happened while having performed some task?
edit: This is the intended code:
foreach (IStep step in Steps)
{
if (Cancelled)
return;
step.Run();
ReportProgress(100.0 * completedWeight / totalWeight, step.Description);
completedWeight += step.Weight;
}
ReportProgress(100, "Completed");
So IStep contains a Run() method, and I am perfectly fine with completing a step before cancelling. I do not know how to catch mouse click on the Cancel button while executing some step to set Cancelled to true.
Obviously there is no "standard" solution here, so we have to think outside the box...
Say you have your application (AddIn or whatever, doesn't matter) and you can't control the loop from a button.
You read/write to the database.
On top of your loop, where it says:
if (Cancelled)
return;
We have to replace with:
If(CheckIsCancelled())
You have to find a way to make a button that can be clicked, either another form near the current one, but it must be able to run independently from the current form that is blocked by your loop.
Create a database parameter in some sort of Config/Util table.
E.g. CancelMyLoop - Bit
On that button click - set the parameter value to true.
And back to the method: CheckIsCancelled()
it will go in the db and read that value every time.
Downside is performance, but you want the impossible so you have to settle with a workaround like this...
You can create your own implementation, just giving you an idea.
My program needs to display a dialog box to the user, which prompts the user to select the save folder, and then displays a Yes-No buttons messageBox to ask the user to confirm that they wish to continue.
This is my code:
/* Wait until user has selected a save folder */
do { } while (sSaveFolder == null);
/* Cancel operation if user clicks on cancel when in folder selection window */
if (sSaveFolder == "<cancel>")
{
worker.ReportProgress(0, "Operation Cancelled\r\n\r\n**********\r\n");
return;
}
/* Check for confirmation */
if (MessageBox.Show("Please confirm whether or not to continue.", "Do you wish to continue?", MessageBoxButtons.YesNo) == DialogResult.No)
{
worker.ReportProgress(0, "Operation Cancelled\r\n\r\n**********\r\n");
return;
}
The problem I'm getting is that I can run this once, click No and the worker thread terminates. But, if I click on the button to run the worker thread again, I get the message box popping up at the same time as the save folder dialog box - which, for obvious reasons is problematic. So does anyone know why this might be happening and how to solve it?
I found a work around to my particular problem by moving the message box to before the save folder dialog box but, as this is a weird problem, I thought I'd ask about it crops up again in the future.
Thanks in advance :)
I cannot see anywhere in your code where the value of sSaveFolder would be reset.
Since you are reusing the same object the previous value may still be set, so the do...while completes very quickly and therefore the messagebox is displayed.
Resetting the value of sSaveFolder before you display the dialog should fix your problem.
Please guide me as how to save/discard values of controls of a form, upon pressing Ok/Cancel button accordingly in Visual Studio C#?
Controls in a form include TablelayoutPanel(TextBoxes), NumericUpDown.
Need your expert guidance
Regards
Asad
With both of your buttons, inside the "onclick" event, call a function that will save the content of the form. You also need this call in the "onclose" event of the form, in case the user presses the top-right X button (or not, if you dont want data to be saved at that moment)
Inside that function, you will need some code that will save data to the registry.
Writing in the registry is easy. This webpage also explain how to get the data back. The values you will write will be the textbox.Value and such
The question isn't clear, but in a WinForm you can call
this.Close()
on the Click event of your Close button.
Every object or variable used by the form will be destroyed. Be careful! running background threads will still be alive until they terminate.
About saving the status of your variables it completely depends on what you need to with them after; you can either keep them in memory and pass them around like parameters or write on a disk (maybe with serialization?).
We need to know more.
edit
You may want to take a look at Application Configuration ( http://msdn.microsoft.com/en-us/library/ms184658(VS.80).aspx ).
Is it possible to set the OpenFileDialog's cancel button enable = false? If so, How?
I'm using winforms
Edit
OpenFileDialog file_open_dialog = new OpenFileDialog();
I don't believe so, and with good reason! Are you sure that you want to disable the Cancel button? Being that this is a modal dialog this would prevent the user from continuing (and hence making your application unusable) until they selected a file - there are almost certainly going to be scenarios where this simply isn't possible, or the user doesn't want to be forced into saving a file.
Of course I could be wrong and there could be a completely legit scenario that I'm not aware of - however I'm afraid that Microsoft were also unaware of this scenario and so the OpenFileDialog doesn't support disabling the cancel button.
I'm afraid that if you really don't want a cancel button you will need to create your own clone of the dialog.
handle the cancel in a loop if you want? might frustrate users ;)
do
{}
while (openFileDialog1.ShowDialog() != DialogResult.OK);
I have a created a custom keyboard shortcut for my application,
when the user press combination keys of CTRL + ALT + Q, i show
a messagebox "Are you sure you want to log out ?" Then if clicked
YES, i log out of the application.
Problem :
I want to make sure that, only once instance of message box shows.
No matter how many times the user presses the shortcut.
currently it shows multiple message box, on pressing multiple
shortcuts.
How to overcome this ?
From MSDN
A message box is a modal dialog box,
which means no input (keyboard or
mouse click) can occur except to
objects on the modal form. The program
must hide or close a modal form
(typically in response to some user
action) before input to another form
can occur.
File a bug on connect.microsoft.com !
Taking ck's comment into consideration...If you are showing a custom dialog (form) then you need to invoke the form using Form.ShowDialog() and not Show().
A quick and dirty way would be to have a class level boolean variable that tracks when the user is trying to exit. If they are, it's set to true, and your routine to display the dialog box can check this flag, then return without doing anything.
Seems like Singleton Pattern is your option.
I think you can create your own form and use the mymessageboxform.show() method, and check its dialogue result.
You'll want to make your application single instance so it can only be started once.
Single Instance App