Check Message Box Result


To check the message box result use the Key word “DialogResult” and its option after the giving dot “.”  in if condition The sample code is:


if (DialogResult.Yes == MessageBox.Show("Hello word", "hello", MessageBoxButtons.YesNo, MessageBoxIcon.Question))

{

MessageBox.Show("you press 'yes' button.", "hello", MessageBoxButtons.OK, MessageBoxIcon.Information);

}

else

{

MessageBox.Show("you press 'no' button.", "hello", MessageBoxButtons.OK, MessageBoxIcon.Information);

}

Description

Above code first message box will display with the message of “hello word”, and the title of the message box is “hello” it contain two button yes and no.

We using the if condition to check the result which key user press if it is yes or no,it was check by DialogResult.yes if it was  yes the condition is true and display another message box with the message “you press yes button.” Title is “hello”.
If the IF condition is false control go to else part and display the other message box with the message “you press no button.”

MessageBox.show() function is use to display the message box. the first field of messagebox is use to display the message and the second field is use to display the title of message box and third field is use to set the buttons,which type of button you want you can select it from list and fourth field is icon field of message box by using it you can give icon to your message box and last field is default button by using this you give the default selection to your button.