How to use Message Box C# Windows Application

Posted on 2006-11-13
Last Modified: 2007-12-19

I am new to C# and I am making Windows Application using C#

I want that If User clicks on the button on the Application Form then confirm box should appear that If user clicks yes then task should perform else Application remains as it is.

What I should write on the button to close the application on some event?

Thanks in Advance
Question by:dinesh_bali
  • 2
  • 2
LVL 11

Expert Comment

ID: 17934693

DialogResult R =  MessageBox.Show("Message here");

 if(R = DialogResult.Yes )
   // your code
LVL 11

Accepted Solution

Jase-Coder earned 250 total points
ID: 17934700
here is an example from the msdn:

private void validateUserEntry5()

    // Checks the value of the text.

    if(serverName.Text.Length == 0)

        // Initializes the variables to pass to the MessageBox.Show method.

        string message = "You did not enter a server name. Cancel this operation?";
        string caption = "No Server Name Specified";
        MessageBoxButtons buttons = MessageBoxButtons.YesNo;
        DialogResult result;

        // Displays the MessageBox.

        result = MessageBox.Show(this, message, caption, buttons);

        if(result == DialogResult.Yes)

            // Closes the parent form.





LVL 15

Expert Comment

ID: 17934701
If this form you close is the main form of the application then the application will quit when it is closed.
You can use Application.Exit() but it is better to Close() your forms in an orderly fashion.

if (MessageBox.Show("Are you sure you wish to quit this application.","Quit ?", Buttons.YesNo) == DialogResult.Yes){
    // they clicked on the yes button.
LVL 15

Expert Comment

ID: 17937381
Here is code to bring up a Yes/No messagebox and then preform an action based on the results.

        private void button1_Click(object sender, EventArgs e)
            DialogResult dr = MessageBox.Show("Do you wish to exit?", "Exit", MessageBoxButtons.YesNo);
            switch (dr)
                case DialogResult.Yes:
                    this.DialogResult = DialogResult.OK;
                    Application.Exit(); // Exit the program or this can be changed to any other action
                case DialogResult.No:
                    // Do nothing, or preform whatever other action you want

If it was to just close a form, not the applicaiton, you would change Application.Exit() to:

Form1 formShow = new Form1();

That is assuming Form1 is the main form that you want to open again, and Form2 is the one being closed.  
LVL 15

Expert Comment

ID: 17937393
Oh I forgot to say, make sure that you have this at the very top of the code:

using System.Windows.Forms

Otherwise it just won't work.

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to Decrypt in C# a string that is encrypted with Coldfusion Encrypt(string, seed) function 11 73
Export import database 4 43
SQL Login 17 40
Web Form VB.Net  import CSV 4 26
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now