KnowDotNet Visual Organizer

Writing to the Output Window From a Visual Studio .NET Add-in

by Les Smith
Print this Article Discuss in Forums

Can I write to the Output window from an add-in in Visual Studio .NET?  Yes, you can and you can also create your own pane to write your output.  Through automation, you can enumerate the existing output windows, add a new Output Window Pane, and write text to the existing panes or to your newly created panes.  Place the following code blocks in the Macro IDE and execute them to see the results of their respective functionality.  To use the code in an add-in, change all references to DTE to applicaltionObject.

The following code will list the Name Property of each existing Output Window Pane in the respective pane.

    Public Sub WriteToOutputWindow()
        
Dim win As Window = _
          DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput)
        
Dim ow As OutputWindow = win.Object
        
Dim owPane As OutputWindowPane
        
For i As Integer = 1 To ow.OutputWindowPanes.Count
            owPane = ow.OutputWindowPanes.Item(i)
            owPane.Activate()
            owPane.OutputString(ow.OutputWindowPanes.Item(i).Name)
        
Next
    End Sub

The following code will add a new pane to the collection of panes in the Output Window and then write some text in the new pane.

    Public Sub WriteToMyNewPane()
        
Dim win As Window = _
           DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput)
        
Dim ow As OutputWindow = win.Object
        
Dim owPane As OutputWindowPane
        
Dim cnt As Integer = ow.OutputWindowPanes.Count
        owPane = ow.OutputWindowPanes.Add("My New Output Pane")
        owPane.Activate()
        owPane.OutputString("My text1" & vbCrLf)
        owPane.OutputString("My text2" & vbCrLf)
        owPane.OutputString("My text3" & vbCrLf)
    
End Sub

Writing Add-Ins for Visual Studio .NET
Writing Add-ins for Visual Studio .NET
by Les Smith
Apress Publishing