Manipulating IDE Tool Windows

 < Free Open Study > 



Maintaining Build Rules

Build rules are simply processes that can be run before and after a developer builds a solution. These processes can be executable programs or even files, as long as the file type is a registered file type. In this section, I demonstrate an add-in that will allow you to maintain a set of pre-and postbuild rules or processes. This simply means that you can have a set of processes run automatically for you just before the solution build process starts and/or finishes.

Note 

A registered file type example is a .txt file.When a .txt file is double-clicked in the Windows Explorer, it causes Notepad.exe to be automatically launched.

Before I show you the new add-in, I discuss the object through which these build rules can be maintained across sessions of the Visual Studio .NET IDE. This object is the Globals object.

The Globals Object

Visual Studio .NET uses a cache object for storing data throughout each session. It can even save data across sessions. This object is the Globals object. The Globals object can be used by macros. The object can also be used by an add-in because, once connected, an add-in is virtually part of the IDE. Therefore, macros can have global variables whose values persist between executions of the macro. The Globals object can be used to allow a macro to implement a default value instead of requiring the user to enter a repeating value each time the macro is executed. The variable could also be used to change the behavior of the macro after it has been executed a certain number of times. Add-ins can have global variables that persist between sessions of the IDE.

The Globals object stores data in name/variant value pairs. These pairs can optionally be saved on disk. The VariablePersists property of the Globals object can be used to allow the macro or add-in to maintain state between different sessions of Visual Studio. The Globals object can also be used to save to and retrieve from a solution (.sln) file.

Note 

Object variables cannot be saved in the Globals object unless they can be saved as a string. The length of a variable name to be saved in the Globals object cannot exceed 31 characters.

The VariablePersists property is used with the following types of Globals objects:

  • DTE.Globals: Used to persist values between sessions of the environment. The values are stored in the User Profiles directory.

  • Solutions.Globals: Used to persist values in the solution (.sln) file so that the data is saved when the solution is closed and reloaded when the solution is reloaded. If you want to save values with a particular solution, use the DTE.Solution.Globals object.

  • Project.Globals: Allows persistence of values in the project file.

Any variables saved will overwrite previously saved values. To remove a variable from the saved file, set the VariablePersists property to False. The environment will remove its value during the next Save operation. Values can be saved and retrieved by using the VariableValue property. The use of the Globals object and its properties will be demonstrated in the MaintainBuildRules add-in.

MaintainBuildRules Add-in

In this section I demonstrate the use of the Globals object as well as the processing of build rules before and after a solution is built. This demonstration also introduces the use of DTE events processing, which Chapter 11 explores in more detail.

In order to demonstrate these objects and events, I have created a new add-in. I have named the add-in MaintainBuildRules. I built this add-in using the Add-in Wizard, but I will not bore you by documenting that process again. Once the wizard created the base code for the add-in, I made numerous modifications and additions to the Connect class. Listing 9-6 shows the code for the Connect class after the modifications. The new code is shown in bold.

Listing 9-6: Enhanced Connect Class

start example
 Imports Microsoft.Office.Core imports Extensibility imports System.Runtime.InteropServices Imports EnvDTE Imports System.IO Imports System #Region " Read me for Add-in installation and setup information. " ' When run, the Add-in wizard prepared the registry for the Add-in. ' At a later time, if the Add-in becomes unavailable for ' reasons such as: '   1) You moved this project to a computer other than '      the one it was '      originally created on. '   2) You chose 'Yes' when presented with a message asking '      if you wish '      to remove the Add-in. '   3) Registry corruption. '      you will need to re-register the Add-in by building the '      MaintainBuildRulesSetup project '      by right-clicking the project in the Solution Explorer, '      then choosing install. #End Region <GuidAttribute("19125D5B-4728-42B6-8206-7B69784E9217"), _ ProgIdAttribute("MaintainBuildRules.Connect")> _ Public Class Connect    Implements Extensibility.IDTExtensibility2    Implements IDTCommandTarget    Dim CommandObj As Command    Dim WithEvents bldevents As BuildEvents    Dim form1 As MaintainBuildRules    Dim rules As String()    Dim oVB As EnvDTE.DTE    Dim addInInstance As EnvDTE.AddIn    Public Sub OnBeginShutdown(ByRef custom As System.Array) _       Implements Extensibility.IDTExtensibility2.OnBeginShutdown    End Sub    Public Sub OnAddInsUpdate(ByRef custom As System.Array) _       Implements Extensibility.IDTExtensibility2.OnAddInsUpdate    End Sub    Public Sub OnStartupComplete(ByRef custom As System.Array) _       Implements Extensibility.IDTExtensibility2.OnStartupComplete    End Sub    Public Sub OnDisconnection(ByVal RemoveMode As _       Extensibility.ext_DisconnectMode, _       ByRef custom As System.Array) _       Implements Extensibility.IDTExtensibility2.OnDisconnection       If Not CommandObj Is Nothing Then          oVB.CommandBars.Item("Tools").Controls.              Item(CommandObj.Name).Delete()          CommandObj.Delete()          End If          bldevents = Nothing    End Sub    Public Sub OnConnection(ByVal application As Object, _       ByVal connectMode As Extensibility.ext_ConnectMode, _       ByVal addInInst As Object, _       ByRef custom As System.Array) _       Implements Extensibility.IDTExtensibility2.OnConnection       oVB = CType(application, EnvDTE.DTE)       addInInstance = CType(addInInst, EnvDTE.AddIn)       Dim objAddIn As AddIn = CType(addInInst, AddIn)       Try          CommandObj = oVB.Commands.AddNamedCommand(objAddIn, _             "MaintainBuildRules", "MaintainBuildRules", _             "Executes the command for MaintainBuildRules", _             True, 59, Nothing, 1 + 2)          CommandObj.AddControl(oVB.CommandBars.Item("Tools"))          bldevents = CType(oVB.Events.BuildEvents,             EnvDTE.BuildEvents)       Catch e As System.Exception       End Try    End Sub    Public Sub Exec(ByVal cmdName As String, _       ByVal executeOption As vsCommandExecOption, _       ByRef varIn As Object, _       ByRef varOut As Object, _       ByRef handled As Boolean) _       Implements IDTCommandTarget.Exec       handled = False       If (executeOption =          vsCommandExecOption          .vsCommandExecOptionDoDefault) Then          If cmdName =              "MaintainBuildRules.Connect              .MaintainBuildRules" Then             form1 = New MaintainBuildRules(oVB)             form1.ShowDialog()             handled = True             Exit Sub          End If       End If    End Sub    Public Sub QueryStatus(ByVal cmdName As String, _       ByVal neededText As vsCommandStatusTextWanted, _       ByRef statusOption As vsCommandStatus, _       ByRef commandText As Object) _       Implements IDTCommandTarget.QueryStatus       If neededText =           EnvDTE.vsCommandStatusTextWanted.           vsCommandStatusTextWantedNone _          Then          If cmdName =           "MaintainBuildRules.Connect.              MaintainBuildRules" Then             statusOption =                  CType(vsCommandStatus.                  vsCommandStatusEnabled + _                 vsCommandStatus.                  vsCommandStatusSupported, vsCommandStatus)          Else             statusOption =                 vsCommandStatus.vsCommandStatusUnsupported          End If       End If    End Sub    Private Sub bldevents_OnBuildDone(ByVal Scope As         EnvDTE.vsBuildScope,         ByVal Action As EnvDTE.vsBuildAction)         Handles bldevents.OnBuildDone        'apply postbuild rules when the build event is done        Dim i As Integer = 0        Dim tmps As String        Dim globs As Globals        globs = oVB.Solution.Globals        'go through rules saved in solution.globals,        ' parse them and then execute        While globs.VariableExists("SolutionPostBuildRule" & _           i.ToString()) = True           If globs.VariableValue("SolutionPostBuildRule" & _               i.ToString()) <> "" Then               tmps = globs.VariableValue("SolutionPostBuildRule" & _                  i.ToString())               bldRules = Split(tmps, " ", 2)               bldRules(0).Replace("\", "\\")               If bldRules.Length = 2 Then                  System.Diagnostics.Process.                      Start(bldRules(0), bldRules(1))               Else                  System.Diagnostics.Process.Start(bldRules(0))               End If           End If           i = i + 1        End While    End Sub    Private Sub bldevents_OnBuildBegin(ByVal Scope As            EnvDTE.vsBuildScope,            ByVal Action As            EnvDTE.vsBuildAction)            Handles bldevents.OnBuildBegin       ' apply prebuild rules when solution starts to build       Dim i As Integer = 0       Dim tmps As String       Dim globs As Globals       globs = oVB.Solution.Globals       'go through rules saved in solution.globals,       ' parse them and then execute       While globs.VariableExists("SolutionPreBuildRule" &           i.ToString()) = True          If globs.VariableValue("SolutionPreBuildRule" &              i.ToString()) <> "" Then             tmps = globs.VariableValue("SolutionPreBuildRule" &                i.ToString())               bldRules = Split(tmps, " ", 2)               bldRules(0).Replace("\", "\\")               If bldRules.Length = 2 Then                  System.Diagnostics.Process.Start(bldRules(0),                     bldRules(1))               Else                  System.Diagnostics.Process.Start(bldRules(0))               End If          End If          i = i + 1       End While    End Sub End Class 
end example

I have made the usual changes to the Connect class, such as changing the name of the applicationObject, changing the scope of the command object, and so forth. In addition to these changes and additions to the methods created by the Add-in Wizard, you will notice that there are two new event handlers. They are named bldevents_OnBuildBegin and bldevents_OnBuildDone, and you can see them at the end of the class. These two methods handle the OnBuildBegin and OnBuildDone events. These methods will fire when the solution build begins and finishes. They check to see if there are any pre- and postbuild rules, respectively. If any rules are found in the Globals object, in either of the events, the rules (processes) are executed using the Start method of the System.Diagnostics.Process object. This method simply launches the specified process (file, executable, and so forth). The method accepts either one or two parameters. The first is the name of the process and the second is an optional command line parameter to be placed on the command line call to the process. Starting a process by specifying its file name and arguments is analogous to typing the file name and command line arguments in the Run dialog box of the Windows Start menu. For this reason, the file name does not need to represent an executable file. It can use any file whose extension has become associated with an application installed on the system.

In addition to the Connect class, the add-in has a form that is used to load and maintain the build rules. I display this form later in the chapter when I show the execution of the add-in. For now, Listing 9-10 shows the code for the form.

Listing 9-10: Maintain Build Rules Form

start example
 Option Strict Off Imports System.IO Imports System.Diagnostics Imports System.Runtime.InteropServices Imports process = System.Diagnostics.Process <ComVisible(False)> Public Class MaintainBuildRules     Inherits System.Windows.Forms.Form     Dim appObject As EnvDTE.DTE     Dim mbDirty As Boolean     Const PreBuild = "SolutionPreBuildRule"     Const PostBuild = "SolutionPostBuildRule" #Region " Windows Form Designer generated code "     Public Sub New()        MyBase.New()        'This call is required by the Windows Form Designer.        InitializeComponent()        'Add any initialization after the InitializeComponent() call     End Sub     Public Sub New(ByVal applicationObject As EnvDTE.DTE)        MyBase.New()        appObject = applicationObject        'This call is required by the Windows Form Designer.        InitializeComponent()        'Add any initialization after the InitializeComponent() call     End Sub     'Form overrides dispose to clean up the component list.     Protected Overloads Overrides Sub Dispose(ByVal disposing As             Boolean)        If disposing Then           If Not (components Is Nothing) Then              components.Dispose()           End If        End If        MyBase.Dispose(disposing)     End Sub     Friend WithEvents Addnewruletextbox As System.Windows.Forms.TextBox     Friend WithEvents MoveDown As System.Windows.Forms.Button     Friend WithEvents MoveUp As System.Windows.Forms.Button     Friend WithEvents OK As System.Windows.Forms.Button     Friend WithEvents RemoveButton As System.Windows.Forms.Button     Friend WithEvents AddButton As System.Windows.Forms.Button     Friend WithEvents Addanewrule As System.Windows.Forms.Label     Friend WithEvents MyPostBuildRule As System.Windows.Forms.Label     Friend WithEvents MaintainBuildRuleListBox As System.Windows.Forms.ListBox     Friend WithEvents btnSaveRules As System.Windows.Forms.Button     'Required by the Windows Form Designer     Private components As System.ComponentModel.Container     'NOTE: The following procedure is required by the     'Windows Form Designer     'It can be modified using the Windows Form Designer.     'Do not modify it using the code editor.     Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox     Friend WithEvents RadioButton1 As System.Windows.Forms.RadioButton     Friend WithEvents rbPreBuild As System.Windows.Forms.RadioButton     <System.Diagnostics.DebuggerStepThrough()> _     Private Sub InitializeComponent()        Me.OK = New System.Windows.Forms.Button()        Me.Addanewrule = New System.Windows.Forms.Label()        Me.Addnewruletextbox = New System.Windows.Forms.TextBox()        Me.MaintainBuildRuleListBox = New            System.Windows.Forms.ListBox()        Me.MoveUp = New System.Windows.Forms.Button()        Me.AddButton = New System.Windows.Forms.Button()        Me.MoveDown = New System.Windows.Forms.Button()        Me.MyPostBuildRule = New System.Windows.Forms.Label()        Me.RemoveButton = New System.Windows.Forms.Button()        Me.GroupBox1 = New System.Windows.Forms.GroupBox()        Me.btnSaveRules = New System.Windows.Forms.Button()        Me.btnLoadRules = New System.Windows.Forms.Button()        Me.RadioButton1 = New System.Windows.Forms.RadioButton()        Me.rbPreBuild = New System.Windows.Forms.RadioButton()        Me.GroupBox1.SuspendLayout()        Me.SuspendLayout()        '        'OK        '        Me.OK.Location = New System.Drawing.Point(291, 267)        Me.OK.Name = "OK"        Me.OK.Size = New System.Drawing.Size(80, 24)        Me.OK.TabIndex = 2        Me.OK.Text = "&OK"        '        'Addanewrule        '        Me.Addanewrule.Font = _           New System.Drawing.Font("Microsoft Sans Serif", 8.25!, _           System.Drawing.FontStyle.Regular, _           System.Drawing.GraphicsUnit.Point, CType(0, Byte))        Me.Addanewrule.Location = New System.Drawing.Point(10, 215)        Me.Addanewrule.Name = "Addanewrule"        Me.Addanewrule.Size = New System.Drawing.Size(100, 16)        Me.Addanewrule.TabIndex = 4        Me.Addanewrule.Text = "Add a new rule"        '        'Addnewruletextbox        '        Me.Addnewruletextbox.Location =            New System.Drawing.Point(10, 231)        Me.Addnewruletextbox.Name = "Addnewruletextbox"        Me.Addnewruletextbox.Size = New System.Drawing.Size(256, 20)        Me.Addnewruletextbox.TabIndex = 3        Me.Addnewruletextbox.Text = ""        '        'MaintainBuildRuleListBox        '        Me.MaintainBuildRuleListBox.Location =            New System.Drawing.Point(10, 32)        Me.MaintainBuildRuleListBox.Name = "MaintainBuildRuleListBox"        Me.MaintainBuildRuleListBox.Size =            New System.Drawing.Size(256, 134)        Me.MaintainBuildRuleListBox.TabIndex = 0        '        'MoveUp        '        Me.MoveUp.Location = New System.Drawing.Point(17, 176)        Me.MoveUp.Name = "MoveUp"        Me.MoveUp.Size = New System.Drawing.Size(80, 24)        Me.MoveUp.TabIndex = 2        Me.MoveUp.Text = "Move &Up"        '        'AddButton        '        Me.AddButton.Location = New System.Drawing.Point(290, 231)        Me.AddButton.Name = "AddButton"        Me.AddButton.Size = New System.Drawing.Size(80, 24)        Me.AddButton.TabIndex = 2        Me.AddButton.Text = "&Add Rule"        '        'MoveDown        '        Me.MoveDown.Location = New System.Drawing.Point(102, 176)        Me.MoveDown.Name = "MoveDown"        Me.MoveDown.Size = New System.Drawing.Size(80, 24)        Me.MoveDown.TabIndex = 2        Me.MoveDown.Text = "Move &Down"        '        'MyPostBuildRule        '        Me.MyPostBuildRule.Font = New _            System.Drawing.Font("Microsoft Sans Serif", 8.25!, _            System.Drawing.FontStyle.Regular, _            System.Drawing.GraphicsUnit.Point, CType(0, Byte))        Me.MyPostBuildRule.Location = New System.Drawing.Point(10, 8)        Me.MyPostBuildRule.Name = "MyPostBuildRule"        Me.MyPostBuildRule.Size = New System.Drawing.Size(112, 16)        Me.MyPostBuildRule.TabIndex = 1        Me.MyPostBuildRule.Text = "Build Rules"        '        'RemoveButton        '        Me.RemoveButton.Location = New System.Drawing.Point(186, 176)        Me.RemoveButton.Name = "RemoveButton"        Me.RemoveButton.Size = New System.Drawing.Size(80, 24)        Me.RemoveButton.TabIndex = 2        Me.RemoveButton.Text = "&Remove"        '        'GroupBox1        '        Me.GroupBox1.Controls.AddRange           (New System.Windows.Forms.Control()            {Me.btnSaveRules, Me.btnLoadRules,            Me.RadioButton1, Me.rbPreBuild})        Me.GroupBox1.Location = New System.Drawing.Point(274, 9)        Me.GroupBox1.Name = "GroupBox1"        Me.GroupBox1.Size = New System.Drawing.Size(124, 152)        Me.GroupBox1.TabIndex = 7        Me.GroupBox1.TabStop = False        Me.GroupBox1.Text = "Load && Save"        '        'btnSaveRules        '        Me.btnSaveRules.Location = New System.Drawing.Point(18, 107)        Me.btnSaveRules.Name = "btnSaveRules"        Me.btnSaveRules.Size = New System.Drawing.Size(80, 24)        Me.btnSaveRules.TabIndex = 10        Me.btnSaveRules.Text = "&Save Rules"        '        'btnLoadRules        '        Me.btnLoadRules.Location = New System.Drawing.Point(19, 75)        Me.btnLoadRules.Name = "btnLoadRules"        Me.btnLoadRules.Size = New System.Drawing.Size(80, 24)        Me.btnLoadRules.TabIndex = 9        Me.btnLoadRules.Text = "&Load Rules"        '        'RadioButton1        '        Me.RadioButton1.Location = New System.Drawing.Point(9, 47)        Me.RadioButton1.Name = "RadioButton1"        Me.RadioButton1.Size = New System.Drawing.Size(102, 16)        Me.RadioButton1.TabIndex = 8        Me.RadioButton1.Text = "PostBuild Rules"        '        'rbPreBuild        '        Me.rbPreBuild.Checked = True        Me.rbPreBuild.Location = New System.Drawing.Point(8, 21)        Me.rbPreBuild.Name = "rbPreBuild"        Me.rbPreBuild.Size = New System.Drawing.Size(99, 16)        Me.rbPreBuild.TabIndex = 7        Me.rbPreBuild.TabStop = True        Me.rbPreBuild.Text = "PreBuild Rules"        '        'MaintainBuildRules        '        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)        Me.ClientSize = New System.Drawing.Size(410, 301)        Me.Controls.AddRange(New System.Windows.Forms.Control()            {Me.GroupBox1,            Me.MyPostBuildRule, Me.Addanewrule, Me.AddButton,            Me.RemoveButton, Me.OK,           Me.MoveDown, Me.MoveUp, Me.MaintainBuildRuleListBox, _           Me.Addnewruletextbox})        Me.FormBorderStyle =            System.Windows.Forms.FormBorderStyle.FixedSingle        Me.Name = "MaintainBuildRules"        Me.Text = "Build Rules Maintenance"        Me.GroupBox1.ResumeLayout(False)        Me.ResumeLayout(False)     End Sub #End Region    Private Sub MaintainBuildRules_Load(ByVal sender As        System.Object,        ByVal e As System.EventArgs) Handles MyBase.Load       mbDirty = False    End Sub    Private Sub GetSelectedBuildRules()       Dim i As Integer = 0       Dim globs As EnvDTE.Globals       Dim s As String       If Me.rbPreBuild.Checked Then          s = PreBuild       Else          s = PostBuild       End If       globs = appObject.Solution.Globals       MaintainBuildRuleListBox.Items.Clear()       'load postbuild rules save in solution.globals to the form       While (globs.VariableExists(s & i.ToString()) = True)          If globs.VariableValue(s & i.ToString()) <> "" Then             MaintainBuildRuleListBox.              Items.Add(globs.VariableValue(s &                 i.ToString()))             MaintainBuildRuleListBox.SetSelected(i, True)          End If          i = i + 1    End While End Sub 'move the selected item in the rule list down Private Sub MoveDown_Click(ByVal sender As System.Object, _    ByVal e As System.EventArgs) _    Handles MoveDown.Click    Dim temp As Object    Dim index As Integer    mbDirty = True    If ((MaintainBuildRuleListBox.SelectedIndex = _       (MaintainBuildRuleListBox.Items.Count - 1)) Or _       (MaintainBuildRuleListBox.SelectedItem = Nothing)) _       Then       Exit Sub    End If    index = MaintainBuildRuleListBox.SelectedIndex + 1    temp = MaintainBuildRuleListBox.SelectedItem    MaintainBuildRuleListBox.Items.        RemoveAt(MaintainBuildRuleListBox.        SelectedIndex)    MaintainBuildRuleListBox.Items.Insert(index, temp)    MaintainBuildRuleListBox.SetSelected(index, True) End Sub 'remove the selected item from the rule list Private Sub RemoveButton_Click(ByVal sender As System.Object, _    ByVal e As System.EventArgs) Handles RemoveButton.Click    Dim sindex As Integer    mbDirty = True    If (MaintainBuildRuleListBox.Items.Count = 0) Then       Exit Sub    Else       sindex = MaintainBuildRuleListBox.SelectedIndex       MaintainBuildRuleListBox.Items.          RemoveAt(MaintainBuildRuleListBox.          SelectedIndex)       If (sindex <> 0) Then          MaintainBuildRuleListBox.SetSelected(sindex - 1, True)       End If    End If End Sub 'add a rule to the rule list Private Sub AddButton_Click(ByVal sender As System.Object, _    ByVal e As System.EventArgs) Handles AddButton.Click    If ((Addnewruletextbox.Text = "") Or _       (Addnewruletextbox.Text.IndexOf(" ") = 0)) _       Then       Addnewruletextbox.Clear()       Exit Sub    End If    mbDirty = True    MaintainBuildRuleListBox.Items.Add(Addnewruletextbox.Text)    MaintainBuildRuleListBox.SetSelected        (MaintainBuildRuleListBox.        Items.Count - 1, True)    Addnewruletextbox.Clear() End Sub 'move the selected item in the rule list up Private Sub MoveUp_Click(ByVal sender As System.Object, _    ByVal e As System.EventArgs) _    Handles MoveUp.Click    Dim temp As Object    Dim index As Integer    If ((MaintainBuildRuleListBox.SelectedIndex = 0) Or _       (MaintainBuildRuleListBox.SelectedItem = Nothing)) Then       Exit Sub End If mbDirty = True If ((MaintainBuildRuleListBox.SelectedIndex - 1) < 0) Then    index = 0 Else    index = MaintainBuildRuleListBox.SelectedIndex - 1 End If temp = MaintainBuildRuleListBox.SelectedItem MaintainBuildRuleListBox.Items.RemoveAt     (MaintainBuildRuleListBox.SelectedIndex) MaintainBuildRuleListBox.Items.Insert(index, temp) MaintainBuildRuleListBox.SetSelected(index, True) End Sub    'click OK button on the form    Private Sub OK_Click(ByVal sender As System.Object, _       ByVal e As System.EventArgs) Handles OK.Click       If mbDirty Then          If MsgBox("Rules have been changed, " & _             "do you want to close without saving?", _             MsgBoxStyle.YesNo, _             "Confirm Closing") = _             MsgBoxResult.Yes Then             Exit Sub          End If       End If    Close() End Sub Private Sub SaveRules()    Dim i As Integer = 0    Dim globs As EnvDTE.Globals    Dim s As String    If Me.rbPreBuild.Checked Then       s = PreBuild    Else       s = PostBuild    End If    globs = appObject.Solution.Globals    'clean up the solution.globals variables    While (globs.VariableExists(s & i.ToString()) = True)       globs.VariableValue(s & i.ToString()) = ""       globs.VariablePersists(s & i.ToString()) = False       i = i + 1    End While    'write the rules from the form to solution.globals variables    For i = 0 To MaintainBuildRuleListBox.Items.Count - 1       globs.VariableValue(s & i.ToString()) = _          MaintainBuildRuleListBox.Items.Item(i)       globs.VariablePersists(s & i.ToString()) = True    Next    ' show no save pending    mbDirty = False End Sub Private Sub btnLoadRules_Click(ByVal sender As System.Object, _    ByVal e As System.EventArgs) Handles btnLoadRules.Click    GetSelectedBuildRules() End Sub Friend WithEvents btnLoadRules As System.Windows.Forms.Button Private Sub btnSaveRules_Click(ByVal sender As System.Object, _    ByVal e As System.EventArgs) Handles btnSaveRules.Click    SaveRules()    End Sub End Class 
end example

The form whose code is shown in Listing 9-10 is a fairly simple maintenance form. It allows you to add pre- and postbuild rules and save them in the DTE.Globals object. It also has buttons for removing and reordering the rules. Option buttons determine which rules, prebuild or postbuild, are to be loaded into the list box and saved from the List Box.

The form is loaded from the Connect class when the user clicks the add-in's menu option. The application object (oVB) is passed to the form via the overload Sub New. This allows the form to have a pointer to the IDE's DTE object so that it can reference the DTE.Globals object.

Now, I load a Windows application project into the IDE. I also connect the MaintainBuildRules add-in. Next, I click the add-in's menu item, which will cause the MaintainBuildRules form to be loaded as shown in Figure 9-8.

click to expand
Figure 9-8: Adding a postbuild rule

You can see in Figure 9-8 that I have entered a postbuild rule. This is a simple load of Notepad.exe, passing it a command line parameter specifying the file to be loaded. I have also entered a prebuild rule that simply loads Notepad.exe without passing it a file to load. After closing the form, I start a build of the solution by selecting Build Build Solution. Immediately, Notepad will be loaded before the build does anything. Because loading Notepad will not stop the build process, the build will be done and a second instance of Notepad will be loaded when the build process completes. In Figure 9-9, you can see that I have positioned the last instance of Notepad over the first. This depicts the order in which the two instances of Notepad were loaded. This is the way that I scheduled their loading by setting them up as pre- and postbuild processes.

click to expand
Figure 9-9: Notepad loaded as pre- and postbuild processes



 < 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