How do I reload a component, in a project, without reloading the project or solution? I have made a number of unwanted changes to a form or code window and would like to return the component to it's unchanged state and discard the changes automatically.
Assuming that you have not saved the active window since making the unwanted changes, you can reload the current window, regardless of whether it is a Form or Code Window.
Although it was not exposed in the IDE, VB6 extensibility exposed a ReloadComponent method to add-in developers. That feature apparently was not exposed in the extensibility model of Visual Studio .NET.
The code shown below can be used in an add-in or a macro. The code is currently set up for add-in code. The object "oVS" is the applicationObject. To change the code for use in a macro, simply substitute "DTE" for every instance of "oVS".
This code demonstrates the programmatic removal and subsequent addition of a project item from a project.
| Public Overloads Sub ReloadComponent() ' Removes current window projectitem w/o saving ' and reloads it into the project. This emulates ' the VB6 ReloadComponent extensibility method ' which was not implemented in .NET. Dim projs As System.Array Dim proj As Project If MsgBox("Are you sure you want to reload " & oVS.ActiveWindow.Caption & "?", _ MsgBoxStyle.Question + vbYesNo) = vbNo Then Exit Sub End If Try Dim sln As String = oVS.Solution.Item(1).Name Dim pn As String Dim s As String Dim s2 As String = oVS.ActiveDocument.FullName Dim awn As String = oVS.ActiveDocument.Name projs = oVS.ActiveSolutionProjects() proj = CType(projs.GetValue(0), EnvDTE.Project) pn = proj.Name s = sln & "\" & pn & "\" & awn oVS.Windows.Item(Constants. _ vsWindowKindSolutionExplorer).Activate() oVS.ActiveWindow.Object.GetItem(s).Select( _ vsUISelectionType.vsUISelectionTypeSelect) oVS.ExecuteCommand("Project.ExcludeFromProject") System.Windows.Forms.Application.DoEvents() oVS.Windows.Item(Constants. _ vsWindowKindSolutionExplorer).Activate() System.Windows.Forms.Application.DoEvents() oVS.ItemOperations.AddExistingItem(s2) oVS.Windows.Item(Constants. _ vsWindowKindSolutionExplorer).Activate() oVS.ActiveWindow.Object.GetItem(s) _ .Select(vsUISelectionType.vsUISelectionTypeSelect) oVS.ActiveWindow.Object.DoDefaultAction() Catch ex As System.Exception End Try End Sub |