Winform : How to create a new popup window using C#

winform - creating simple popup window thumbnail

When I started to learn WinForm programming, one thing that I wondered was how to create additional window forms on the already existing Winform Project. For example, I wanted to create a popup window for editing purposes. I knew what code to write that would instantiate a new form but didn't know how to edit that form in the designer.

In this article we are going to create a Winform Project from scratch that will contain main form and also one additional regular form which will act as a popup dialog window with  OK / Cancel buttons. This popup dialog box will be shown when a button on the main form is clicked. When the popup window closes, the main form will check which button (either OK or Cancel) was clicked.

Finished project will look something like this:

Winform Example using Popup Window with OK and Cancel buttons

Note:In the real world application you most likely will not create these kind of simple Dialog boxes as shown here but would instead just call a static method MessageBox.Show.

So let's start. Follow these steps:

  1. Start the Visual Studio and create a new WinForms project. If you are unsure how to do that or you have trouble finding the WinForms Template, check the How to create WinForms project using Visual Studio article for more detail.
  2. Open a ToolBox (View > Toolbox). Once opened, expand the "Common Controls" and drag a Button Control into a form.
    Note:You can also add a control to the Form by double clicking the button in the Toolbox.
  3. Now lets add another Form Window to the project. You achieve this by right-click on project name in Solution Explorer and then select  Add > Window Form from the context menu as shown below.

    Visual Studio - Add new form

    Click image to enlarge
  4. Add New Item dialog window will appear with Windows Form already selected. Give it a name PopupForm and click Add or just press enter.
  5. The newly created form will automatically be opened in the designer window. To give it some basic functionality, lets add two buttons to it. Open the Toolbox window and then drag two Button Controls to the form (same way as in step 2). For each of the button we are going to modify two properties.
  6. Select one of the buttons in PopupForm form. The properties window should appear on the bottom right side of the Visual Studio.
    Visual Studio Properties Window
    Find the properties listed below and modify their value as instructed:

    • In Text property type OK
    • In DialogResult property the dropdown menu will be shown. Choose OK as shown in the image above.
      Note: DialogResult will be discussed later when we examine code line by line
  7. Now select the other button and modify the following properties:
    • In Text property type Cancel
    • In DialogResult property the dropdown menu will be shown. Here also choose Cancel
  8. Lets also modify appearance of the form by shrinking its size and align the buttons to the center, so that it looks like a standard dialog window:
    Winform Popup Window with OK and Cancel buttons
  9. Only thing left is to add the code. We need to create a new instance of popup Form when button on the main form is clicked, so select the main form in the Designer. If it is not opened, just Double-click on Form1.cs in Solution Explorer.
  10. In the main form Double-click on OK button that you dragged in step 2. This will automatically create an empty method named button1_Click. This method is going to be called when button is clicked.  We want this button to open new popup window, so all the necessary code will be contained in this method.
  11. Add the following code to the button1_Click method as shown below:
    private void button1_Click(object sender, EventArgs e)
    {
        PopupForm popup = new PopupForm();
        DialogResult dialogresult = popup.ShowDialog();
        if (dialogresult == DialogResult.OK)
        {
            Console.WriteLine("You clicked OK");
        }
        else if (dialogresult == DialogResult.Cancel)
        {
            Console.WriteLine("You clicked either Cancel or X button in the top right corner");
        }
        popup.Dispose();
    }
    
  12. Run the application by pressing F5 or choose Debug > Start Debugging

Line by line code explanation

Now Let's examine the above code more closely by focusing on highlighted lines 3, 4, 5 and 13:

  • Line 3:

    PopupForm popup = new PopupForm();
    

    Here we are instantiating the PopupForm class. This class was created in steps 3 and 4 when we selected Add > Window Form and named it PopupForm.

  • Line 4:

    DialogResult dialogresult = popup.ShowDialog();
    

    For this code  you need to be aware of the following things:

    1. ShowDialog method will open the form as a modal dialog box. This basically means that the user will not be able to interact with the main form until the PopupForm is closed.
    2. Another feature of the modal forms is that the code after ShowDialog method will not get executed, you guessed it, until  the popup form  is closed.
      Note:In reality the form gets only hidden and can be shown again without creating a new instance of the form.
    3. When the form / dialog box closes, it is going to return DialogResult Enumeration value and it will be the same value as the DialogResult property of the button that was clicked.
      • If X button was clicked, the form will return the value of DialogResult.Cancel
        X button on windows form
        X button
      • If the button DialogResult property is set to DialogResult.None, the form will not close.
  • Line 5:

    if (dialogresult == DialogResult.OK)
    

    Type of variable dialogresult is DialogResult Enumeration, so we compare that variable against the possible values of that enumeration.

  • Line 13:

    popup.Dispose();
    

    When we do not need the popup form anymore, we dispose of it by calling Dispose method. Reason for calling Dispose method is that when you click either OK or Close button (X button), the popup form will only get hidden and not closed. Without Dispose() we would create additional instances with every button click on the main form.

I hope you found this article useful. If you still have questions, drop me a comment and I might include your problem with the solution in any future update.

23 Comments

Click HERE to add your Comment
  1. niti s
    February 10, 2013
    • admin
      February 10, 2013
  2. Al
    July 15, 2014
    • admin
      July 15, 2014
  3. Thanh Nguyen Van
    September 4, 2014
    • admin
      September 4, 2014
  4. Abhay
    September 25, 2014
  5. Jithin
    December 17, 2014
  6. spela
    May 13, 2015
  7. Kevin
    June 25, 2015
  8. Eric
    September 8, 2015
  9. Suhas
    February 12, 2016
    • admin
      February 12, 2016
  10. Fons
    April 16, 2016
    • admin
      April 16, 2016
  11. Rahul Pal
    February 15, 2017
    • admin
      February 15, 2017
  12. bob
    March 31, 2017
  13. Muhammad
    October 2, 2017
  14. KennZAney1
    March 19, 2018
  15. tu jinyu
    August 13, 2019
  16. marijn (dutch)
    April 20, 2022
  17. Cheyne
    September 20, 2022

Write a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.