The Project Object

 < Free Open Study > 



The Solution Object

The Solution object is the collection of all the projects in the current instance of the Visual Studio IDE. The Solution object also contains all solution-wide properties, such as build configurations. The Solution object contains a project object for every project in the solution.

Note 

Because you have learned either from this book or prior experience how to use the Macro Explorer to execute code that can manipulate the extensibility objects, you will use the Macro Explorer to run many of the code examples in this chapter. It is easier to demonstrate many of the procedures that I will show you in the Macro Explorer than to take the time to build them into an add-in. Again, if you want to move code from a macro to an add-in, any references to the DTE object must be replaced with a reference to the application object (oVB in my add-ins).

You can reference the Solution object by using DTE.Solution. In this section, you will create a solution, add a project to the solution, remove the solution, and close the solution.

Creating a solution is very simple. Listing 9-1 shows the code for creating a new solution in an IDE that has just been opened. After the solution is created (which only takes three lines of code), the code adds a Windows application project to the solution.

Listing 9-1: Creating a Solution and Project

start example
 Sub CreateSolution1()     Dim sln As Solution     Dim prj As Project     'Create a reference to the solution.     sln = DTE.Solution     ' Create a new solution.     sln.Create("c:\vsprojs", "MyNewSolution")     MsgBox("Solution has been created.")     ' Create a new windows application project in the solution     prj = sln.AddFromTemplate("C:\Program Files\Microsoft " & _           "Visual Studio " & _           ".NET\Vb7\VBProjects\windowsapplication.vsz", _           "c:\vsprojs\Chap9 Solution1", "My New Project", True)     MsgBox("Windows Application has been added to solution.")     sln.SaveAs("c:\vsProjs\MyNewSolution.sln")     MsgBox("Solution has been saved.") End Sub 
end example

To run the code in Listing 9-1, import the macro project from the Chapter 9 code. In the Macro Explorer, double-click the CreateSolution1 macro.

Caution 

Before you run this macro on your computer, ensure that the path to the place you want to save projects is correct. If it isn't, you should change the path in the sln.Create statement to a path where you save projects on your computer. If, by chance, the path to the Windows application template is not the same as the one hard-coded in the macro, you'll have to change that also.

The macro is highlighted in Figure 9-1. The IDE has no solution open at the time the macro is run.

click to expand
Figure 9-1: Running the CreateSolution1 macro

When the macro is invoked, the solution is created and a message box displays the fact that the IDE now has an empty solution open. Next, the code adds aWindows application to the solution; the AddFromTemplate method of the Solution object does this. As the macro runs, you can tell that the project is being opened when the Form Designer displays Form1.vb. You will recall that when aproject is created, a form is automatically added to the project.

Note 

When the project is added to the solution, a Save dialog box will appear asking if you want to save the solution. You can click the No button, as the project will be saved by the automation code.

The macro will display two additional messages. One will display when the project has been added, and the other will display when the project has been saved. Figure 9-2 shows the project that has been created. The Solution Explorer is open, showing the structure of the project. If you go to the Windows Explorer and look in the directory where the solution was stored, you should see the normal solution and project structure of directories and files displayed, just as if you had done it manually in the IDE.

click to expand
Figure 9-2: New Windows application project

Next, you'll close the solution manually and reopen it programmatically. The macro in Listing 9-2 will perform this operation. First, you must create a DTE.Solution object. Then you can use the Open method of the Solution object to open the project by simply specifying the path where the solution was created.

Listing 9-2: OpenExistingSolution1 Macro

start example
 Sub OpenExistingSolution1()     Dim sln As Solution     'Create a reference to the solution.     sln = DTE.Solution     ' Open the solution just created.     sln.Open("c:\vsProjs\MyNewSolution.sln")     MsgBox("Solution has been reopened") End Sub 
end example

Finally, you'll close the solution manually and then execute a macro to programmatically reopen the solution. The macro will remove the project from the solution and then close the solution. Listing 9-3 shows the code for this set of operations.

Listing 9-3: RemoveProjectFromSolution Macro

start example
 Sub RemoveProjectFromSolution()     'This function loads a solution, deletes the first project,     'and then closes the solution.     Dim sln As Solution     Dim prj As Project     'Create a reference to the solution.     sln = DTE.Solution     ' Open the solution just created.     sln.Open("c:\vsProjs\MyNewSolution.sln")     ' Delete the newly created VB Console application project in     'this solution.     prj = sln.Projects.Item(1)     sln.Remove(prj)     MsgBox("Project was deleted from the solution,               click Ok to close the solution)     ' Close the solution from the IDE.     sln.Close() End Sub 
end example



 < 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

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net