Adding Controls to the Form

 < Free Open Study > 

Creating the Windows Application Project

The code snippet in Listing 6-3 is responsible for creating the Windows application project. I will walk through each line of code in which what is being done is not obvious and give an explanation of the code. I have numbered the lines of code in this listing so that I can refer to the individual lines that need special explanation.


If you are going to run this add-in a second or subsequent time, you will have to go to the default directory where Visual Studio stores projects and delete WinFormsPrj. If you do not delete the project, the add-in will not work.

Listing 6-3: Creating the Project

start example
 01    " Winforms automation sample code starts here 02    " Create a Windows Forms project and give the 03    " designer focus 04    " uncomment the next line and comment the 2nd 05    "line down to create 06    ' a VC# project instead of a VB project 07    'Dim templatePath As String = _ 08    '    oVB.Solution.TemplatePath  09    '    ("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") & _ 10    '    "CSharpEXE.vsz" 11    Dim templatePath As String = _ 12        oVB.Solution.TemplatePath  13        ("{F184B08F-C81C-45f6-A57F-5ABD9991F28F}") & _ 14        "WindowsApplication.vsz" 15 16    Dim targetDir As String = _ 17        CStr(oVB.Properties("Environment", _ 18        "ProjectsAndSolution").Item 19 ("ProjectsLocation").Value) & _ 20        "\WinFormPrj" 21 22    oVB.Solution.AddFromTemplate(templatePath, _ 23        targetDir, "WinFormPrj") 24 Catch e As System.Exception 25    MsgBox("You probably need to delete WinFormPrj") 26    Exit Sub 27 End Try 28 28 MsgBox("The Windows Application Project " & _ 29        "has been added to the IDE.") 
end example

First, you have to create the project itself. The code in Listing 6-3 does that. There are Dim statements at line 07 and line 11. Line 07 is commented out, but it is shown here in order to demonstrate how to create a Visual C# project. Line 22 actually creates a Visual Basic Windows application project. Line 11 sets the path to the Visual Studio template for a Visual Basic Windows application. You will note that the string will have the GUID of {F184B08F-C81C-45f6-A57F-5ABD9991F28F} concatenated into it. Line 11 will use the templatePath variable to retrieve the path to the template file from the registry. If you open the Regedit program and search for the GUID, you will see the layout of the template pointers in the registry, as shown in Figure 6-1. The actual path to the template is as follows:

 C:\Program Files\Microsoft Visual Studio.NET\Vb7\VBProjects\      WindowsApplication.vsz 

click to expand
Figure 6-1: Registry pointer to project templates

When Line 18 is executed, Visual Studio will create a new Windows application project in the IDE.

You will remember that when you start to debug an add-in, a second (client) instance of Visual Studio is started automatically. When the second project opens, you should try to size and position the two copies of the IDE so that they appear side by side, without overlaying each other. This will allow you to see both of them during the automation process. Because all of the work in the second copy (client) will be shown in the upper left corner of the designer, the IDE does not have to be very large.

Once line 22 of the code in Listing 6-3 has been executed, a message box display in the code stops the automation process. This will allow you to view the newly created project. Figure 6-2 shows the newly created project in the second IDE.

click to expand
Figure 6-2: Newly created project

You will see that when the Windows application project is created, a Windows Form (Form1.vb) is automatically created.

Click the OK button on the message box to continue the automation process.

 < Free Open Study > 

Writing Add-Ins for Visual Studio  .NET
Writing Add-Ins for Visual Studio .NET
ISBN: 1590590260
EAN: 2147483647
Year: 2002
Pages: 172
Authors: Les Smith © 2008-2017.
If you may any questions please contact us: