I have a confirmation message box for the user in one of my apps. Below is the code for that,
MessageBoxResult res= System.Windows.MessageBox.Show("Could not find the folder, so the D: Drive will be opened instead.");
if (res == MessageBoxResult.OK)
{
MessageBox.Show("OK");
}
else
{
MessageBox.Show("Do Nothing");
}
Now, when the user clicks on the OK button, I want certain code to execute but when they click on the red cross at the upper right corner, I just want the messagebox to close without doing anything. In my case I get 'OK' displayed even when I click on the red cross icon at the upper right corner. Is there a way I can have 'Do Nothing' displayed when I click on the cross. I not want to add any more buttons.
Yes there is a very simple way, just add the "MessageBoxButtons.OKCancel" param to your MessageBox.Show method. this way you will have two buttons (OK and Cancel). this way if the user clicks the cancel button or the red cross the DialogResult.Cancel message will be returned. the following code described the solution:
System.Windows.Forms.DialogResult result = System.Windows.Forms.MessageBox.Show("Could not find the folder, so the D: Drive will be opened instead.",
"", System.Windows.Forms.MessageBoxButtons.OKCancel);
if (result == System.Windows.Forms.DialogResult.OK)
MessageBox.Show("OK");
else
MessageBox.Show("Do nothing.");
No, there isn't.
You could make your own custom dialog form.
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 have a mainform where you can open another window and change options. One of the options is to copy highlighted text to the clipboard. if the user doesn't highlight text and clicks btnCopy then I want a message to be shown that no text was highlighted. When the user selects 'ok' I want the messagebox to close but I want the 'options' window to stay open.
Right now when the user clicks 'ok' both the message box and 'options' window closes. Why is the 'options' window closing?
Here is my code:
private void btnCopy_Click(object sender, EventArgs e)
{
string copySearch = txtSavedSearches.SelectedText;
if (copySearch == "")
{
DialogResult dialog = MessageBox.Show("You did not select anything to copy. Please select the query to copy.", "Copy search", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
Clipboard.SetText(copySearch);
this.Close();
}
}
You obviously set the DialogResult property of btnCopy to something different than DialogResult.None.
If - in a Form that is not the application's main window - a Button is clicked that has the DialogResult property set (to something different than None), this click causes the Form to close and the calling ShowDialog() method to return that DialogResult.
Find out where you set that property and remove it.
From MSDN (Button.DialogResult):
If the DialogResult for this property is set to anything other than None, and if the parent form was displayed through the ShowDialog method, clicking the button closes the parent form without your having to hook up any events. The form's DialogResult property is then set to the DialogResult of the button when the button is clicked
Why is the 'options' window closing?
The following line will cause the options form to close:
this.Close();
You don't need to do anything to close a MessageBox; it goes away by itself when the user clicks OK, and then your code resumes running from the point where MessageBox.Show was called. MessageBox.Show is a method that returns a value denoting which button the user clicked to get the box to go away (the value varies depending on a) which buttons you chose to show as part of the call to .Show(...) and b) which button the user clicked
Right now when the user clicks 'ok' both the message box and 'options' window closes. Why is the 'options' window closing?
This cannot be, as the message box is shown in the do-if-true part of the IF, and the call to close the options form is called in the ELSE (do if false) part. These two parts cannot run in succession, they must be one or the other. Either your option form closes without a messagebox showing, or a messagebox shows and your form doesn't close
-
Edit:
Renee believes you have set this property:
on your btnCopy button to be something other than None
And then you have also opened your options form like this:
OptionsForm f = new OptionsForm();
f.ShowDialog();
These two things combined will conspire to cause your form options to close any time that btnCopy is clicked (unless the clickevent is canceled)
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.
I have a button that when you click it a popup is supposed to be displayed just showing a simple text message until the user clicks on the canvas. Do I have to do something like this in my button click method?:
Window win2 = new Window();
win2.Show();
this.Close();
I just don't want to have another .xaml file for that window when all it's going to be doing is displaying a message for a short amount of time. Right now I'm using a Popup TextBlock but it's not quite what I want, since I can't make it movable.
WPF has a built in message box which you can use to display information then can easily be closed. This is used in many Windows applications as error popups.
MessageBoxResult result = MessageBox.Show("Text here ","Error", MessageBoxButton.OK, MessageBoxImage.Question);
Just place this inside your button click method and change the "Text Here" to whatever you want.
https://msdn.microsoft.com/en-us/library/system.windows.messagebox(v=vs.110).aspx
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.