Click here to Skip to main content
15,867,568 members
Articles / Programming Languages / C#

How to create custom dialog boxes using the Web Setup Project in Visual Studio 2010

Rate me:
Please Sign up or sign in to vote.
4.81/5 (21 votes)
20 Apr 2011CPOL6 min read 79.8K   2.5K   44   14
A step by step walk through of creating custom dialog boxes and adding them to your web setup projects.

Introduction

For most software, the development is not considered complete as late as the deployment of the solution, whether it is a website or a desktop solution. There are numerous ways of deploying your software for the end user. For example, when deploying a site, you can either publish the site using Visual Studio's Web Deploy options, or you can create a Web Setup Project (a great article on creating a web setup project is available on Scott Gu’s blog: http://weblogs.asp.net/scottgu/archive/2007/06/15/tip-trick-creating-packaged-asp-net-setup-programs-with-vs-2005.aspx), or you can even simply copy and paste the whole website to the server and do all the configuration manually.

If you are using the VS Web Setup Project, there is quite a possibility that you would like to perform some additional tasks which may require you to add some custom logic for your software to start some kind of service or for setting the connection string for your application. For that purpose, you may require some additional input from the user which can be obtained by simply using the dialog boxes provided in the VS Web Setup Projects. These are a set of predefined dialog templates which are setup according to our own requirements. There are a few limitations though, which are listed below:

  • You cannot add a combination of controls other than those which are already provided, i.e., you may want to use a check box, two text boxes, and a radio button. In this case, you cannot use the predefined dialogs.
  • You have no access whatsoever over the controls in the dialog box; for example, you would want to have a Form where the user checks the “Windows Authentication” option and you would want to hide the username and password fields provided for SQL Server authentication. This cannot be achieved using the predefined dialog boxes.
  • You will have to do tedious work in order to get a variable from your dialog to a custom action where you actually want to use the values provided in the form. For example, you will have to add something like below for your custom action data from your user interface, and will use the context in the custom action to get the value, which can become confusing at times.
  • /targetdir="[TARGETDIR]\" /targetvdir="[TARGETVDIR]" /targetsite="[TARGETSITE]"
  • You cannot add any kind of validation for your input; there is no way to make sure the user has entered an input and that it is valid.

Sometimes you would want to go beyond the provided functionality in the setups and have some control over the flow and execution of operations which you want to perform during the setup. The good news is, you can simply use your self-created Windows Form and get it to execute during the setup process. This is a good way of achieving full control over your custom operations.

Now I will give a step by step walk through of creating custom dialog boxes and adding them to your web setup projects.

Creating a Web Site

  1. Open your Visual Studio and click File -> New -> Project, and from the installed templates section, select Web, and select the template you want to use for your website. In this case, I am using ASP.NET Web Application.
  2. Image 1

  3. In the default page, I change the standard “WELCOME TO ASP.NET!” text to “WELCOME TO ASP.NET” to make sure this is our page.
  4. Image 2

Creating a Windows Form for our Custom Dialog

  1. Right click the solution in Solution Explorer, and click the “New Project” sub menu item in the Add menu.
  2. Image 3

  3. In “Windows” Installed Templates, choose Windows Forms Application. Enter “Custom Window” in the Name field for the project.
  4. Image 4

  5. A blank Windows Form will be shown in the Designer. Design the window as per your requirements. I have added a few controls like below, but in the actual setup, you would make the screen as close as possible to the setup steps.
  6. Image 5

Using this form, you can do whatever you want with full control over all the objects on the form.

Creating a Custom Action Class Library

  1. Right click the solution in Solution Explorer, click the “New Project” sub menu item in the Add menu.
  2. Image 6

  3. Select Class Library from the Visual C# installed templates. This will be our custom action from which we will call our dialog box showDialog() function.
  4. Image 7

  5. Delete the class name Class1.cs.
  6. Add System.ComponentModel, System.Windows.Forms, and System.Configuration.Install in namespaces.
  7. Add the [RunInstaller(true)] attribute on the class.
  8. Image 8

  9. Add a new class named CustomAction.cs and inherit it from System.Configuration.Install.Installer. You will have to add a reference to the System.Configuration.Install component to your project.
  10. You will now override the Install function of the Installer class.
  11. Image 9

  12. You can skip the steps from 9 – 11 by adding the Installer class in the General category in Installed Templates.
  13. Image 10

  14. Add a reference to the CustomWindow project in the CustomAction project.
  15. Add the following lines of code in the override:
  16. Image 11

Creating a Web Setup Project

  1. Now we will add the Web Setup Project. For that, right click the solution in Solution Explorer, and click the “New Project” sub menu item in the Add menu.
  2. Image 12

  3. Expand the “Other Project Types” node in the Installed Templates pane. Under the “Setup and Deployment” node, click “Visual Studio Installer” and select “Web Setup Project” from the templates pane. Enter “MyInstaller” in the Name field and click OK.
  4. Image 13

  5. Once the project is created, right click the “MyInstaller” project in Solution Explorer, and in the “Add” menu, click “Project Output”.
  6. Image 14

  7. The “Add Project Output Group” window will be shown. Select “MySite” from the Project dropdown list and select “Primary output” and “Content Files”, and press OK.
  8. Image 15

  9. Similarly, add the primary output of the CustomAction project in the MyInstaller project.
  10. Image 16

  11. The output from the MySite project and CustomAction will be added to the installer. See figure below.
  12. Image 17

  13. To add the custom action to show our dialog box, right click the “MyInstaller” project and in the “View” menu, click “Custom Actions”.
  14. Image 18

  15. The Customer Action tab will be opened:
  16. Image 19

  17. Right click the “Install” folder under CustomActions and click “Add Custom Action…”
  18. Image 20

  19. A dialog will open and in the Web Application Folder, select “Primary output from CustomAction” and click OK.
  20. Image 21

  21. And that should be it, phew. Build the solution and our setup will be created in the Debug or Release folder of our project depending on our configuration.

Running the Setup

Now we are going to run the setup we just created. Just double click Setup.exe and our setup will start.

Image 22

Click Next for the web configuration options:

Image 23

Click Next to confirm installation and click Next to start setup.

Image 24

You will see the custom action dialog which was added.

Image 25

And when you click Next after performing your operation, when the dialog is closed, the control will be given back to the installer and it will complete the installation.

Image 26

Click Close to complete the installation.

Passing the Context to a Custom Dialog

In some cases, you would want to pass the context to your custom dialog and use the context variable. You can create a class member of Install and create a constructor for the form class which accepts an InstallContext object and assigns it to our formContext object.

Image 27

And when creating the instance of our dialog in a custom action, pass it as a single argument for the constructor.

Image 28

Conclusion

By using this technique, you can get custom functionality while running your setup with full control over the execution of your custom logic.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Ciklum Pakistan
Pakistan Pakistan
My name is Imran and I have been in software development since 2007, I am working on Microsoft Technologies and specializing in web based technologies like ASP.NET with SQL Server and WCF. I have worked on .Net Framework 2.0, 3.5 and 4.0, SQL Server 2000, 2005, and 2008.

I also have great admiration for different open source projects such as Python and DJango.

Comments and Discussions

 
QuestionUser is able to Cancel from the setup window when custom dialog is displaying..? Pin
chantiben11-Dec-13 22:34
chantiben11-Dec-13 22:34 
QuestionDebug setup.exe file it is not working Pin
ajaykmrsingh8629-Jul-13 22:14
ajaykmrsingh8629-Jul-13 22:14 
GeneralMy vote of 5 Pin
saxenaabhi615-Aug-12 19:33
saxenaabhi615-Aug-12 19:33 
QuestionHow to rollback when user cancel dialog box Pin
AditSheth25-May-12 9:01
AditSheth25-May-12 9:01 
GeneralMy vote of 5 Pin
cpquest27-Mar-12 0:38
cpquest27-Mar-12 0:38 
GeneralMy vote of 5 Pin
ddalmia20008-Dec-11 1:43
ddalmia20008-Dec-11 1:43 
QuestionCan i make open window to Modal Dialog? Pin
Sunasara Imdadhusen28-Aug-11 21:39
professionalSunasara Imdadhusen28-Aug-11 21:39 
AnswerRe: Can i make open window to Modal Dialog? Pin
Muhammad Imran Saleem28-Aug-11 23:03
Muhammad Imran Saleem28-Aug-11 23:03 
Hi Sunasara,

Thanks for liking the article.

Well, I tried to achieve the Modal like behavior by a number of ways but unfortunately it didn't work. Even if you use frmDialog.ShowModalDialog() the result will be the same. It seem that the setup opens the window as a separate application and just moves data between the setup and the window and the not the parent child heirarchy

Regards

Sunasara Imdadhusen wrote:
Hi

Muhammad,

 

Thanks for sharing nice article with us, but i have requirement like.

 

Modal Dialog (Custom Form) should be modal. The mail window (Setup) would only be active after user click on either Next or Cancel from custom form. (user can not able to go main setup window without closing custom form)



Please provide you help for the same.Thumbs Up | :thumbsup:

 

My vote of 5![Rose]

 

I am waiting for your reply.

Thanks,

Imdadhusen

GeneralRe: Can i make open window to Modal Dialog? Pin
Sunasara Imdadhusen28-Aug-11 23:25
professionalSunasara Imdadhusen28-Aug-11 23:25 
GeneralRe: Can i make open window to Modal Dialog? Pin
Muhammad Imran Saleem28-Aug-11 23:46
Muhammad Imran Saleem28-Aug-11 23:46 
GeneralRe: Can i make open window to Modal Dialog? Pin
Sunasara Imdadhusen28-Aug-11 23:59
professionalSunasara Imdadhusen28-Aug-11 23:59 
GeneralHow to call custom user's dialog before install? Pin
Win32nipuh12-May-11 22:29
professionalWin32nipuh12-May-11 22:29 
GeneralRe: How to call custom user's dialog before install? Pin
Muhammad Imran Saleem15-May-11 19:48
Muhammad Imran Saleem15-May-11 19:48 
GeneralNice Article Pin
Kashif Abbas13-Apr-11 20:08
Kashif Abbas13-Apr-11 20:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.