Home DotNet How to write a C# confirmation dialog with one line of code
Author:

When writing application you will often come across a requirement to get confirmation from the user before going ahead to delete a record. This is a common practice for all the applications that has been built to confirm with the user before the application is closed or a record is deleted. C# has a nice set of built in libraries which does all this for you so you dont have to write any code. For a quick and simple cofirmation you can use the MessageBox library.

View the example below to get the user to confirm before you ahead with the deletion:

private void btnDeleteFL_Click(object sender, EventArgs e)

{

    if (MessageBox.Show("Are you sure you want to delete this feature?",

        "Confirm delete", MessageBoxButtons.YesNo) == DialogResult.Yes)

    {

        // Delete the require record according to the user command.               

    }

}

C-Confirmation-Dialog-Box-example-ok-cancel

Pretty straight forward isn't it? Please comment if you think this code can by improve any further.



Comments (0)
Write comment
Your Contact Details:
Comment:
Security
Please input the anti-spam code that you can read in the image.

"