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 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 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 lets start. Follow these steps:

  1. Create a new project and choose Windows Forms Application. Name the project and click OK.
  2. Drag a Button Control from a ToolBox (View > Toolbox).
    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.
  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. To give this new window some basic functionality, lets add two buttons to it. Open the form in the Designer 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. Visual Studio Properties WindowSelect one of the buttons in PopupForm form. The properties window should appear on the bottom right side of the Visual Studio. 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
      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:

  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:

    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:

    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.
      • X button on windows form

        X button

        If X button was clicked, the form will return the value of DialogResult.Cancel

      • If the button DialogResult property is set to DialogResult.None, the form will not close.
  • Line 5:

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

  • Line 13:

    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.

Tags:

C#, Winforms

Share this page

15 Comments

Add a Comment

Back to Top