I want to make simple Yes/No choiced MessageBox, but I think it is nonsense to design a form for that. I thought I could use MessageBox, add buttons, etc. to accomplish this
This simple code worked for me. I grabbed it from MSDN here:
https://social.msdn.microsoft.com/Forums/en-US/d1092a96-96b0-4ca4-b716-0c8e55e42ee9/how-can-i-manage-messagebox-result-?forum=Vsexpressvcs
if (System.Windows.Forms.MessageBox.Show
("Are you sure you want to add the audit?", "Add",
System.Windows.Forms.MessageBoxButtons.YesNo,
System.Windows.Forms.MessageBoxIcon.Question)
==System.Windows.Forms.DialogResult.Yes)
// Do stuff after 'YES is clicked'
else
// DO stuff after 'NO is clicked'
You can also use this variant with text strings, here's the complete changed code (Code from Mikael), tested in C# 2012:
// Variable
string MessageBoxTitle = "Some Title";
string MessageBoxContent = "Sure";
DialogResult dialogResult = MessageBox.Show(MessageBoxContent, MessageBoxTitle, MessageBoxButtons.YesNo);
if(dialogResult == DialogResult.Yes)
{
//do something
}
else if (dialogResult == DialogResult.No)
{
//do something else
}
You can after
.YesNo
insert a message icon
, MessageBoxIcon.Question
Try this:
if (MessageBox.Show("Are you sure", "Title_here", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
Do something here for 'Yes'...
}
This should do it:
DialogResult dialogResult = MessageBox.Show("Sure", "Some Title", MessageBoxButtons.YesNo);
if(dialogResult == DialogResult.Yes)
{
//do something
}
else if (dialogResult == DialogResult.No)
{
//do something else
}
The MessageBox does produce a DialogResults
DialogResult r = MessageBox.Show("Some question here");
You can also specify the buttons easily enough. More documentation can be found at http://msdn.microsoft.com/en-us/library/ba2a6d06.aspx
if (MessageBox.Show("Please confirm before proceed" + "\n" + "Do you want to Continue ?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
//do something if YES
}
else
{
//do something if NO
}