Solved

How to use Message Box C# Windows Application

Posted on 2006-11-13
5
2,767 Views
Last Modified: 2007-12-19
Hi,

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
0
Comment
Question by:dinesh_bali
  • 2
  • 2
5 Comments
 
LVL 11

Expert Comment

by:Jase-Coder
ID: 17934693

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

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

Accepted Solution

by:
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.

            this.Close();

        }

    }

}

0
 
LVL 15

Expert Comment

by:ozymandias
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.
    this.Close();
}
0
 
LVL 15

Expert Comment

by:angus_young_acdc
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
                   
                    break;
                case DialogResult.No:
                    // Do nothing, or preform whatever other action you want
                    break;
        }

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

Form1 formShow = new Form1();
formShow.Show();
this.Close();

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

Expert Comment

by:angus_young_acdc
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.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

791 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