Set the default button when displaying a MessageBox in C#

The fifth parameter to MessageBox.Show indicates the default button. The following code shows how the program displays a MessageBox with the second button as the default so it is initially selected when the message box appears.

// Display a message box with the second button the default.
private void btnButton2_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show(
"The second button is the default.",
"Caption",
MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Information,
MessageBoxDefaultButton.Button2);
MessageBox.Show("You selected " + result.ToString());
}

  

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.