C# MessageBox

April 2, 2009

C# MessageBox

Using “MessageBox” in C#

A MessageBox is basically a pop-up box that shows a message to the user. It may or may not provide buttons to get feedback from the user.

What you will learn here:
1) How to use MessageBox.Show with various parameters
2) How to find out which button the user clicked-on in the MessageBox
3) How to change the caption of the MessageBox

Code for you to copy:


        private void buttonOkay_Click(object sender, EventArgs e)
        {
            DialogResult dr =
                 MessageBox.Show("Hello World",
                "My Caption",
                MessageBoxButtons.YesNo);
            if (dr == DialogResult.Yes)
            {
                txtResults.Text = "You chose the 'Yes' button";
            }
            if (dr == DialogResult.No)
            {
                txtResults.Text = "You chose the 'No' button";
            }
        }

Video Demonstration:

Filed under C# MessageBox by Administrator

Permalink Print Comment