A common example of this is when a program asks the user if they accept terms and conditions. The user can either confirm the dialog, or exit. In a windows form application, you can create confirmation boxes programmatically using C#.

While creating the dialog box, you can display a message, and add buttons to allow the user to respond to the confirmation dialog. You can then display the new window to the user.

How to Add an Event to Trigger the Confirmation Dialog Box

A confirmation dialog should work like other types of dialog boxes. A Winforms input dialog box is another example of a dialog box you can create.

The confirmation dialog will show when the user triggers an event in the program. You can trigger events in a Windows Form app using a button click, progression to another stage, or any other type of event.

Create a new Windows Forms application, and add a button to trigger the confirmation dialog to show:

Open Visual Studio and create a new Windows Forms application. Drag a button from the Toolbox onto the canvas. Navigate to the properties window on the bottom right of Visual Studio. Change the properties of the new button to the following:Property New Value Name termsAndConditionsButton Size 400, 100 Text Terms and Conditions This should give the button the following appearance: Drag a label from the Toolbox to the canvas. In the properties window, change the properties of the label to the following values:Property New Value Property New Value Name responseLabel Text Your response: Visible False Which will result in a label looking like this: Double-click on the Terms and Conditions button on the canvas. Visual Studio will open the C# Code-behind file, which is where you can add programming logic. The program will generate a new function called termsAndConditionsButton_Click(). This function will run when the user clicks on this button at runtime. private void termsAndConditionsButton_Click(object sender, EventArgs e){    // Code for when the user clicks on the Terms and Conditions button}

How to Display the Confirmation Box to the User

Inside the termsAndConditionsButton_Click() function, display the confirmation dialog to the user. Record their response and display it back onto the screen using the “responseLabel” label.

Inside the termsAndConditionsButton_Click() function, add the possible values that the user can select. Include a yes, no, and cancel value. You can read more about DialogResult in Microsoft’s official documentation. DialogResult[] results = { DialogResult. Yes, DialogResult. No, DialogResult. Cancel }; Declare a variable to store the user’s response, based on the button they click on. string userResponse = “”; Display the confirmation dialog, which will call the ConfirmationBox() function. The ConfirmationBox() function will create the content inside the confirmation dialog. You will create this function in the next steps. if (results. Contains(ConfirmationBox(ref userResponse))){} Inside the if statement, make the label visible. Display the result of what button the user selected back onto the screen. responseLabel. Visible = true;responseLabel. Text = “Your response: " + userResponse;

How to Generate Yes, No, and Cancel Buttons and Add Them to the Dialog Window

Create the ConfirmationBox() function. Inside the function, generate the content for the Confirmation dialog window itself.

Create a new function called ConfirmationBox(). public static DialogResult ConfirmationBox(ref string userResponse){} Inside the function, create the dialog window and give it a title. Form form = new Form();form. Text = “Confirmation Dialog”; Add a message for the user to read and confirm. Add other properties for the message to configure its location and sizing. Label message = new Label();message. Text = “Do you agree to the terms and conditions?";message. SetBounds(36, 36, 372, 13);message. AutoSize = true; Create the button objects that will display on the confirmation dialog. Start by adding the Yes button, and configure some of its properties such as its value and location. Button buttonYes = new Button();buttonYes. Text = “Yes”;buttonYes. DialogResult = DialogResult. Yes;buttonYes. SetBounds(150, 160, 150, 60);buttonYes. Anchor = AnchorStyles. Bottom | AnchorStyles. Right; Add the No button to the confirmation dialog. Configure some of its properties such as value and location. Button buttonNo = new Button();buttonNo. Text = “No”;buttonNo. DialogResult = DialogResult. No;buttonNo. SetBounds(310, 160, 150, 60);buttonNo. Anchor = AnchorStyles. Bottom | AnchorStyles. Right; Add the Cancel button to the confirmation dialog. Configure some of its properties such as value and location. Button buttonCancel = new Button();buttonCancel. Text = “Cancel”;buttonCancel. DialogResult = DialogResult. Cancel;buttonCancel. SetBounds(470, 160, 150, 60);buttonCancel. Anchor = AnchorStyles. Bottom | AnchorStyles. Right; Add properties for the confirmation dialog window itself. This includes the size of the window, borders, start position, and other maximizing properties. form. ClientSize = new Size(796, 307);form. FormBorderStyle = FormBorderStyle. FixedDialog;form. StartPosition = FormStartPosition. CenterScreen;form. MinimizeBox = false;form. MaximizeBox = false; Add the message and button objects to the dialog window. form. Controls. AddRange(new Control[] { message, buttonYes, buttonNo, buttonCancel }); Configure quick actions. The confirmation dialog will select the accept button when the user presses the Enter button on the keyboard. It will also select the cancel button when the user presses the escape button on the keyboard. form. AcceptButton = buttonYes;form. CancelButton = buttonCancel; Display the confirmation dialog to the user. DialogResult dialogResult = form. ShowDialog(); Configure the possible values that the function will return. This includes “Yes,” “No”, and “Cancel”. if (dialogResult == DialogResult. Yes){    userResponse = “Yes”;} if (dialogResult == DialogResult. No){    userResponse = “No”;} if (dialogResult == DialogResult. Cancel){    userResponse = “Cancel”;} return dialogResult;

How to Run the Confirmation Dialog

Run the Windows Forms application using the run button. Open the confirmation dialog and click on one of the buttons.

Click on the green play button at the top of the Visual Studio application. Wait for the program to compile and run. Click on the Terms and Conditions button. Click on either the “Yes”, “No”, or “Cancel” buttons in the confirmation dialog. View the result of the button you clicked on in the main page of the application.

Creating and Using Confirmation Dialogs in a Windows Form Application

In a windows form application, you can create confirmation dialogs to display a message to the user and wait for their response. To create a confirmation dialog, create a function that will display it when an event occurs.

When creating the confirmation dialog, create a new window and add a message to display to the user. Add buttons onto the confirmation dialog for the user to click on, and return the result.

You can customize the design of your dialog boxes by changing their background color, border styles, and theme. Explore how to add different themes to your application.