Yes-No MessageBox appearing too early in a worker thread - c#

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.

Related

How to check if a dialog is open in PowerPoint?

I have an application written in C# using Microsoft.Office.Interop.PowerPoint. This application opens a PowerPoint presentation file to the user. The user interacts with the file. After applying some modifications, the user submits the file through the application submit button.
The Problem:
When user modifies the content of the presentation, for example changes font color of the text to red, by using a dialog and keeps the dialog open without clicking on "Apply" or "OK" button and thereby submits the file by clicking on application submit button, those dialog done changes aren't reflected in the submitted file and hence such changes can't be tracked of.
I somehow want to alert a user to close any open dialog before clicking on submit button.
I do this easily on Word and excel files by checking Exception on saving such files because Word and Excel throws exception on using save method if there is any dialog open, like the Following:
try{
document.Save();
}
catch (Exception e){
//Alert user here here
}
but this doesn't work for PowerPoint files. I tried the following:
PowerPointApplication application = new Microsoft.Office.Interop.PowerPoint.Application();
Microsoft.Office.Interop.PowerPoint.Presentations presentations = application.Presentations;
Microsoft.Office.Interop.PowerPoint.Presentation presentation =
presentations
.Open(file, WithWindow: Microsoft.Office.Core.MsoTriState.msoTrue,ReadOnly:Microsoft.Office.Core.MsoTriState.msoFalse,Untitled:Microsoft.Office.Core.MsoTriState.msoFalse);
The following is triggered on Submit button click event:
try
{
presentation.Save();
}
catch (Exception e)
{
//alert user here
}
Is there a way to track if there are any open dialog boxes?
Typically if any dialog window is displayed to a user the code is blocked because the dialog window uses the main thread for running. Do you use multiple threads in the code?
Anyway, you can use Windows API functions for detecting the active window, see GetActiveWindow which retrieves the window handle to the active window attached to the calling thread's message queue. To get the handle to the foreground window, you can use GetForegroundWindow. To get the window handle to the active window in the message queue for another thread, use GetGUIThreadInfo.
After retrieving the window handle you can use the GetWindowText function which copies the text of the specified window's title bar (if it has one) into a buffer. If the specified window is a control, the text of the control is copied.

The last time window popped out overrides those earlier in c#

Not sure My title is correct or not actually.
I have a button, when I click on it, it will pop up a small window (based on data in a repeater) to let the user key-in information and click the OK button to proceed to next step.
Below is my code of the Button.Click :
protected void btnRedeemAll_Click(object sender, EventArgs e)
{
foreach( RepeaterItem itm in repGiftResults.Items )
{
if (pr.AdditionalFieldsEnabled == true)
{
AdditionalInfoGiftProcess(pid, giftId, txtQty, txtToken);
}
}
}
divAdditionalInfo is a division ID, divAdditionalInfo.Style["visibility"] = "visible"; and divAdditionalInfo.Style["display"] = "table-cell"; is to prompt out a window to let user key in information and click on "OK" button to continue :
The following the the code of AdditionalInfoGiftProcess() function :
public void AdditionalInfoGiftProcess(int productID, int giftID, TextBox txtQty, UserControls_TokenControl txtToken)
{
/*
Some logic here
*/
//generate pop up window
divAdditionalInfo.Style["visibility"] = "visible";
divAdditionalInfo.Style["display"] = "table-cell";
}
And below is my code of divAdditionalInfo in html :
<div
id="divAdditionalInfo"
class="BlackCover"
style="VISIBILITY:hidden; DISPLAY:none;" runat="server">
<!--
html code that generate the pop out window.
consist of text box and `OK` button, and "Cancel" button.
-->
</div>
When I click on temoBtn button, I only get 1 time window(div) pop out, means I only can enter 1 time information and click on OK button.
I want the window(div) to pop out 3 times one by one, so that I can key in information for 3 times and click on the OK button 1 by 1.
But, the last time window pop out seen like already override those early pop out window.
Any idea to solve this?
p.s.: Actually you can ignore the html code. It is just a code to generate the pop out window. Means, when I click on tempBtn button, the division with id = "divAdditionalInfo" will prompt out as a window to let the user to key-in info and click on the OK button.
You don't have anything to accept input, in your loop.
All you are doing is setting the visibility 3 times, then moving on.
In your handler for the OK and cancel buttons, you could check for any pending messages and show the next one, if necessary.
What do you want to happen here? When the user clicks OK, should anything happen? Or does the user need to click OK three times in order for the popup to go away?
In any case - as your code stands now, the server is generating a div that is visible. Making it visible once has the same effect as making it visible three times. Once you're by the client, you can try to use JavaScript to force the user to work with the div three times before it will go away.
This doesn't sound like a good situation for the user; maybe consider using separate elements for your three popups?
EDIT:
I'm still not sure how you are setting the style of different elements with your code. Where is the div declared? Inside the template of a repeater? If so - I'm surprised that your code compiles. You would need to loop through the generated template instances of your repeater and call FindControl("divAdditionalInfo") on each of them to get access to each instance of the div.

Add button to copy text in MessageBox

I have a program that contains a list of names, and a button that displays a random name in a MessageBox. Is there any way I can add a button "Copy" next to the OK to the MessageBox, that when clicked copies the name and then closes?
If the above isn't possible, is there a way to enable copying the text in a MessageBox?
Thank you.
edit: My users won't understand Ctrl+C, Highlight and Right Click > Copy is what I'm looking for (if a Copy button isn't possible)
If a user presses Ctrl-C while the MessageBox has focus, the message, the MessageBox caption and the MessageBoxButtons labels are copied to the clipboard.
I googled your title and found this..
Or if you really need a button that says copy you can create your own MessageBox with a new windows form and then do what you want with the buttons. Open it like this to keep the MessageBox feel :
var myMessageBox = new CustomMessageBox();
myMessageBox.ShowDialog();
It sounds like maybe you are looking for the Clipboard class.
Clipboard.SetText(variableWithValue);
There is also another answer here about manipulating the contents of a Message Box.
It also might be easier to simply make a modal dialog that emulates a MessageBox without actually using the MessageBox class.

Difference between "No" and "Cancel" in MessageBoxButtons

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

c# - Stop program execution incase of error

I have a winform application that can catch any number of possible errors. I have found though, that once a messagebox or some other method of displaying the error has been displayed (within a catch block), the program execution continues.
How can I simply stop complete execution of the program in this event and simply just keep the form open? It is only a one form app.
After the message box is displayed, simple call Application.Exit();
This will suffice as long as you don't have any other running threads in the background, but in your case it seems that this is just a simple single threaded application.
You might want to set the owner window of the modal dialog to your form. That way the execution isn't suspended, but the form is deactivated.
Presuming you have something like this:
Private Sub Button1_OnCLick(....) handles button1.onclick
If somecondition then
MsgBox("it failed")
End if
'more code here
and you want to avoid executing 'more code' when the message box has been shown, then add
Exit Sub
just after the MsgBox line
DialogResult result = MessageBox.Show("There was an error, would you like to continue?", "Error",
MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.No)
{
// Terminate
}

Categories

Resources