How to Create a Confirmation Dialog Box in a Winforms Application

Walk through the process of creating a confirmation box with Windows Forms with this step-by-step guide.


A confirmation dialog is a useful way to display messages to the user and ask if they understand and accept the information.


A common example of this is when a program asks the user to accept the terms and conditions. The user can either confirm or end the dialog. In a Windows Form application, you can create confirmation boxes programmatically using C#.

When creating the dialog box, you can display a message and add buttons to allow the user to respond to the confirmation dialog box. You can then show 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 dialogs. A winforms input dialog box is another example of a dialog box that you can create.

The confirmation dialog is displayed when the user triggers an event in the program. You can trigger events in a Windows Form app by clicking a button, moving to a different stage, or using a different event type.

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

  1. Open Visual Studio and create a new Windows Forms application.
  2. Drag a button from the toolbox onto the canvas.
  3. Navigate to the properties panel at the bottom right of Visual Studio. Change the properties of the new button as follows:
    Property New value
    Surname ConditionsAndConditionsButton
    size 400, 100
    text Terms and Conditions

    This should give the button the following appearance:

  4. Drag a label from the toolbox onto the canvas.
  5. In the Properties window, change the label’s properties to the following values:
    Property New value
    Property New value
    Surname ResponseLabel
    text Your answer:
    Visible NOT CORRECT

    Which results in a label that looks like this:

  6. Double-click the Terms and Conditions button on the canvas. Visual Studio opens the C# code-behind file where you can add programming logic. The program generates a new function called termsAndConditionsButton_Click(). This function runs when the user clicks this button at runtime.
    private void termsAndConditionsButton_Click(object sender, EventArgs e)
    {
    }

How to display the confirmation box to the user

In the termsAndConditionsButton_Click() function, show the user the confirmation dialog. Record their response and display it back on screen with the label “responseLabel”.

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

How to generate yes, no and cancel buttons and add them to the dialog box

Create the ConfirmationBox() function. Within the function, generate the content for the confirmation dialog box yourself.

  1. Create a new function called ConfirmationBox().
    public static DialogResult ConfirmationBox(ref string userResponse)
    {
    }
  2. Within the function, create the dialog box and give it a title.
    Form form = new Form();
    form.Text = "Confirmation Dialog";
  3. Add a message for the user to read and acknowledge. Add more properties for the message to configure its location and size.
    Label message = new Label();
    message.Text = "Do you agree to the terms and conditions?";
    message.SetBounds(36, 36, 372, 13);
    message.AutoSize = true;
  4. Create the button objects that appear in the confirmation dialog box. Start by adding the Yes button and configure some of its properties like 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;
  5. Add a No button to the confirmation dialog. Configure some of its properties like 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;
  6. Add a Cancel button to the confirmation dialog. Configure some of its properties like 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;
  7. Add properties for the confirmation dialog itself. This includes the window’s size, border, start position, and other maximize properties.
    form.ClientSize = new Size(796, 307);
    form.FormBorderStyle = FormBorderStyle.FixedDialog;
    form.StartPosition = FormStartPosition.CenterScreen;
    form.MinimizeBox = false;
    form.MaximizeBox = false;
  8. Add the message and button objects to the dialog box.
    form.Controls.AddRange(new Control[] { message, buttonYes, buttonNo, buttonCancel });
  9. Configure Quick Actions. The confirmation dialog selects the accept button when the user presses the Enter key on the keyboard. It will also select the cancel key when the user presses the escape key on the keyboard.
    form.AcceptButton = buttonYes;
    form.CancelButton = buttonCancel;
  10. Show the user the confirmation dialog.
    DialogResult dialogResult = form.ShowDialog();
  11. Configure the possible values ​​that the function returns. These include 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 one of the buttons.

  1. Click the green play button at the top of the Visual Studio application. Wait for the program to compile and run.
  2. Click the Terms and Conditions button.
  3. In the confirmation dialog, click either the Yes, No, or Cancel button.
  4. Look at the result of the button you clicked on 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 dialog boxes to display a message to the user and wait for their response. To create a confirmation dialog, create a function that displays 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 to the confirmation dialog for the user to click and return the result.

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

Leave a Reply

Your email address will not be published. Required fields are marked *