I have a dialog box popup in a VSTO addin for outlook 2013. I test for DialogResult.Yes and No, to which I have set the two buttons' results. They work fine, but I want yet another behavior when the user cancels out of the box. When they press cancel the code just continues. Is there something I can call to stop the addin from executing if they cancel the dialog box? How can I test for the cancel button? I tried res == DialogResult.Cancel but it can't cast res to bool and it's type DialogResult because I also test for Yes and No.
How can I tell if they press the cancel button, and how can I exit the addin. In python the command would be sys.exit() What is the C# equivalent?
If you use the System.Windows.Forms.MessageBox class for displaying dialog boxes in the add-in you may use the following code for checking the chosen option:
// Display message box
DialogResult result = MessageBox.Show(messageBoxText, caption, button, icon);
// Process message box results
switch (result)
{
case MessageBoxResult.Yes:
// User pressed Yes button
// ...
break;
case MessageBoxResult.No:
// User pressed No button
// ...
break;
case MessageBoxResult.Cancel:
// User pressed Cancel button
// ...
break;
}
See Dialog Boxes Overview in MSDN for more information.
If you developed your own window you may add an event handler for the button's Click event.
Related
I have a dialog box that I display over my main form (not an MDI)
I call it this way:
dlgPets dlgPet = new dlgPets();
dlgPet.Show();
Once on the dialog, the user will click a button and that is where i verify what was entered. If it fails, I just return
What happens is the dialog box goes away.
I have also tried adding:
Dialog result = dlgPet.ShowDialog();
if (result == DialogResult.OK)
{
return;
}
I need to stay on the dlgPet dialog box until the user clicks the cancel button.
Thanks in advance
Mark
In my opinion you should use
dlgPet.TopMost = true;
I'm coding a windows form application running on a barcode scanner.
The plantform is .Net2.0CF/C#.
What i want is whenever user input wrong, the app will pop a messagebox and block the next input(actually,a scan action) until user click the OK on the screen.
But normally the user will continuously scan the next stuff as they didn't find anything went wrong, this will insert a Enter keydown so the messagebox will be closed, in one word, the messagebox does not stop the user.
How can i code this? Below is a very simple code snippet
private void tb_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode.ToString() == "Return")
{
if(!ValidateInput(tb.Text))
MessageBox.Show("Error");
}
}
You can create your own window (Form) that displays the error message, but does not react on the enter key.
It should contain a button which the user can click (as you wrote), however you need to make sure the button does not have focus when the window is displayed. (Because if it had focus, pressing the return key will "click" the button.)
A simple way for doing this is adding another control which has TabStop set to true (e.g. a textbox, another button) and which has a lower TabIndex property than the button.
Additionally, maybe you might want to do a
System.Media.SystemSounds.Beep.Play();
when showing the window to draw the user's attention to the window.
I have a form, wherein I prohibit the user from closing it when the user clicks the Close (X) button. Is it possible to show a tooltip on the Close (X) button whenever it is clicked? I want to do it to notify the user why the form would not close.
I thought of a messagebox but then I thought it would be too annoying to close the messagebox every time you click the Close (X) button.
Is there a better way to notify the user, than what I'm trying to do?
EDIT:
This is my code for the FormClosing event
private void InputForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
if (!mCloseReason)
{
e.Cancel = false;
}
}
}
I have a method that will save the inputs in the form. Once that method is finished, I need to automatically close the form. The if-statement will be true once I call this.Close(), this means I can't close the form. That's why I used a variable called mCloseReason to be able to close the form automatically. Now, that's why I was asking if I can notify the user through a tooltip once the Close (X) button is clicked.
I believe the default tooltip for the close(X) button is not editable without some hacking (and it is not practical).
Maybe what you could do is have a status field below your form, so when the user clicks the close button, it says in the status field "cannot close form (...)"
Or another idea is to have a message pop up somewhere on the form and go away after a little, indicating the form cannot be closed.
Another good idea was mentioned by Roger... just have a Close button somewhere on the form with a tooltip coded to it, and hide the forms' title bar?
Within the application I am writing for testing/learning C#, I use the hidden/visible property to open and close windows.
It is a WPF application.
In the main window, I have a "close" button that triggers this method:
public void buttonQuit_Click(object sender, RoutedEventArgs e)
{
var message = exitmessage;
var title = exitTitle;
var result = MessageBox.Show(
message, // the message to show
title, // the title for the dialog box
MessageBoxButton.YesNo, // show two buttons: Yes and No
MessageBoxImage.Question); // show a question mark icon
// lets see what has been pressed
switch (result)
{
case System.Windows.MessageBoxResult.Yes: // Yes button pressed
CloseAllWindows();
break;
case System.Windows.MessageBoxResult.No: // No button pressed
break;
default: // Neither Yes nor No pressed (just in case)
MessageBox.Show("Oh noes! What did you press?!?!");
break;
}
}
This way I make sure that all windows get closed, including the hidden ones.
But now is the catch; when the user presses (in the main window) the top right red X in toolbar to close, only that main window gets closed, but in the background the hidden ones are still there.
So in fact it is 2 questions:
Is CloseAllWindows(); really sufficient to get the app 100% closed down?
How do I "catch" the event when the user presses that red X in the toolbar, and make this also trigger the right closing event?
You should be handling either the Closing or Closed event for your window(s). The former allows you to cancel the close, while the latter just allows you to perform necessary cleanup in response to the window being closed.
So, in this case, you should place the code from your buttonQuit_Click method into a handler method attached to the Closing event so that it gets triggered regardless of how the window is closed.
Then, your buttonQuit_Click method can simply call the window's Close method. That will close the window, in turn raising the Closing event, and running your code in the attached handler method.
As far as your other question, CloseAllWindows will do exactly what it says: it will close all of the windows that your application has opened. In most cases, that should be sufficient to close the application, but it might not be, especially if you've created non-background threads or depending on the ShutdownMode setting.
App.Current.Shutdown will work unconditionally.
You could use the Closing event of the window for that.
Some more info
http://msdn.microsoft.com/en-us/library/system.windows.window.closing.aspx
C#: How do you send OK or Cancel return messages of dialogs when not using buttons?
How would you return the OK message in the condition of a textbox that will proceed when the user presses Enter, and will send Cancel when the user presses Ctrl+Q?
Disregard:
solution- this.dialogresult = dialogresult.ok or dialogresult.cancel.
Set the form's DialogResult:
this.DialogResult = DialogResult.OK;
this.Close();
This would cause any opener that opened this form with ShowDialog() to get the given DialogResult as the result.
I assume you're using Windows Forms...
A couple of ways.
For OK - set AcceptButton on the form to the OK button.
For Cancel - set Cancelbutton on the form to the cancel button.
OR, you can manually set the forms DialogResult to DialogResult.OK or DialogResult.Cancel and then close the form programatically.
Directly, in the properties of the button itself, there is the DialogResult property that can be set to OK/Cancel/Yes/No/etc... As the others have said, this can also be set programmatically.
In the properties of the form the button is on, set the AcceptButton property to your button. This will also do things like trigger the button when you hit the enter key, and highlight the button.