Friday, June 24, 2011

Visual Studio macro to hide/show the Output window after a build…

I do not like the behaviors allowed in Visual Studio regarding showing the Errors List and Output window during a build. So, I finally created a macro to get the exact behavior I want.. And, I love it!

Step #1:

First off, you need to turn off two options in Visual Studio settings, under Projects and Solutions | General.

Un-check the following:

  • Always show Error List if build finishes with errors
  • Show Output window when build starts

The script does the following:

  1. When a build starts it (optionally) shows the Output window (line #9).
  2. When an individual project is finished compiling, notify the user (line #22) and (optionally) stop building any further projects (line #25).
  3. When the entire build has finished and one or more projects failed, display the Output window (line #35). If there were no failures, show and then hide the Output window (line #38) and notify the user.

Note: When the build finishes successfully, the reason I show and then hide the Output window, is because it may already be open and I want to be sure I close it. So far, I haven’t found an easy way to close the Output window, when it may or may not have focus in the Visual Studio IDE. As it is, I only rarely see it flicker open then closed. If you have any ideas, please let me know at kody@bricksoft.com!

Read the comments in the code below for details on how its done!

   1:      Private BuildFailed As Boolean = False


   2:   


   3:      ' The build has started. Could be one or more projects or the whole solution.


   4:      Private Sub BuildEvents_OnBuildBegin() Handles BuildEvents.OnBuildBegin


   5:          ' Reset the global flag.


   6:          BuildFailed = False


   7:   


   8:          ' Optionally, show the Output window.


   9:          'DTE.ExecuteCommand("View.Output")


  10:      End Sub


  11:   


  12:      ' The current project has finished building.


  13:      Private Sub BuildEvents_OnBuildProjConfigDone(ByVal Project As String, _


  14:              ByVal ProjectConfig As String, ByVal Platform As String, _


  15:              ByVal SolutionConfig As String, ByVal Success As Boolean) _


  16:              Handles BuildEvents.OnBuildProjConfigDone


  17:          If (Not Success) Then


  18:              ' Set the global flag.


  19:              BuildFailed = True


  20:   


  21:              ' Notify the user the build failed.


  22:              NotifySuccess(False)


  23:   


  24:              ' Optionally, you can cancel any further project builds.


  25:              'DTE.ExecuteCommand("Build.Cancel")


  26:          End If


  27:      End Sub


  28:   


  29:      ' The build is finished. Show or hide the Output window based on 


  30:      ' whether it was successful or not.


  31:      Private Sub BuildEvents_OnBuildDone(ByVal Scope As EnvDTE.vsBuildScope, _


  32:              ByVal Action As EnvDTE.vsBuildAction) Handles BuildEvents.OnBuildDone


  33:          If BuildFailed Then


  34:              ' Show the output window.


  35:              DTE.ExecuteCommand("View.Output")


  36:          Else


  37:              ' The build succeeded, so hide the Output window.


  38:              DTE.ExecuteCommand("View.Output")


  39:              DTE.ExecuteCommand("Window.CloseToolWindow")


  40:   


  41:              ' Notify the user the build finished successfully.


  42:              NotifySuccess(True)


  43:          End If


  44:      End Sub


  45:   


  46:      Private Sub NotifySuccess(ByVal Success As Boolean)


  47:          If Success Then


  48:              'Beep()


  49:              'Threading.Thread.Sleep(250)


  50:              'Beep()


  51:          Else


  52:              'Beep()


  53:              'Threading.Thread.Sleep(250)


  54:              'Beep()


  55:              'Threading.Thread.Sleep(250)


  56:              'Beep()


  57:              'Threading.Thread.Sleep(250)


  58:              'Beep()


  59:          End If


  60:      End Sub


Thursday, October 7, 2010

How to ensure Outlook is already running..


When I am working, I like to close applications that I am not currently using. This conserves memory (not important) and eliminates desktop clutter (very important). Too often however, Outlook gets closed down in the process. This generally only happens when I need to be reminded of a meeting or whenever I receive important/timely emails.

Here is a simple AutoHotKey (AHK) script that will ensure that Outlook is always open and running.

OutlookStayAlive.ahk

;
; Ensure Microsoft Outlook is running
; Author: Kody Brown (kody.brown@venafi.com)
; Created: Sept 2, 2010
; Copyright: Free
;

#Persistent
SetTimer, OutlookStayAlive, 5000
Return

OutlookStayAlive:
If WinExist("ahk_class rctrl_renwnd32")
{
;WinActivate
}
Else
{
IfExist, C:\Program Files\Microsoft Office\Office12\Outlook.exe
Run, "C:\Program Files\Microsoft Office\Office12\Outlook.exe"
Else IfExist, C:\Program Files (x86)\Microsoft Office\Office12\Outlook.exe
Run, "C:\Program Files (x86)\Microsoft Office\Office12\Outlook.exe"
Else IfExist, C:\Program Files\Microsoft Office\Office11\Outlook.exe
Run, "C:\Program Files\Microsoft Office\Office11\Outlook.exe"
Else IfExist, C:\Program Files (x86)\Microsoft Office\Office11\Outlook.exe
Run, "C:\Program Files (x86)\Microsoft Office\Office11\Outlook.exe"
Return
}
Return

Wednesday, September 23, 2009

Customize how search results are displayed

I created a simple utility that can customize the Visual Studio Find Results output. I think using it should be pretty self-explanatory..

VSFindResultsEditor

I wanted something new to do this morning, what can I say!?

Download it here!

 

You can also check out Sara Ford’s blog for all the glorious details about customizing the Visual Studio Find Results output:

http://blogs.msdn.com/saraford/archive/2008/11/24/did-you-know-you-can-customize-how-search-results-are-displayed-in-the-find-results-window-363.aspx

Excellent Visual Studio tips roundup by Sara Ford

http://blogs.msdn.com/saraford/archive/2009/09/09/teched-australia-25-visual-studio-2008-ide-tips.aspx

 

The ones I found most useful (or didn’t already know) include:

Prevent copying blank lines with Ctrl+C

Look in Tools - Options - Text Editor - All Languages - General. Unchecking 'Apply Cut or Copy commands to blank lines when there is no selection' prevents copying a blank line into the clipboard.  How did I ever miss this checkbox!?

 

Incremental Search

This allows me to jump to the next match as I type it out. I LOVE it!!

   1. Press Ctrl+I
   2. Start typing the text you are searching for.  note: you'll see the cursor jump to the first match, highlighting the current search string.
   3. Press Ctrl+I again to jump to the next occurrence of the search string
   4. (advanced tip) Press Ctrl+Shift+I to search backwards
   5. To stop searching, press ESC

You’ll note that it is case-sensitive, unfortunately. I’ll have to look for a way to change that..

 

And, my total favorite is..

Customize how search results are displayed

This is one of the most irritating things ever about Visual Studio. The Find Results window is almost always filled with only the paths of the found files, instead of the line details. I always have to scroll to the right to see the details. Here is the direct link to the article, bypassing the roundup.

http://blogs.msdn.com/saraford/archive/2008/11/24/did-you-know-you-can-customize-how-search-results-are-displayed-in-the-find-results-window-363.aspx

Friday, September 18, 2009

Build macro for web projects

Using this macro and batch file you can map a keyboard shortcut and/or a toolbar button in Visual Studio to restart IIS and build the selected project asynchronously.

1. Create a batch file that includes the following:

@echo off 
net stop w3svc
net start w3svc


For projects that tend to get hung up more frequently in IIS, you can replace the 'net stop' line above with net 'stop iisadmin' instead. (Don’t change the ‘net start’ line.)



2. Create a new macro project that includes events (or add a new module in existing macro project).



3. Paste the following code into the module, inserting the path to the new batch file:



Sub BuildSelectedWebProject() 
' restart iis
Shell("<your batch file path>", AppWinStyle.MinimizedNoFocus, False)
' start the build
DTE.ExecuteCommand("Build.BuildSelection")
End Sub


4. Map a keyboard shortcut or toolbar button to the new macro: Macros.<Macro Project Name>.BuildSelectedWebProject



 



Build Solution



Optionally, you can also re-map Ctrl+Shift+B (Build Solution) to the following if desired:



Sub BuildSolution() 
' restart iis
Shell("<your batch file path>\iisrestart.bat", AppWinStyle.MinimizedNoFocus, False)
' start the build
DTE.ExecuteCommand("Build.BuildSolution")
End Sub

Visual Studio 2008 Macro Fun

Here are a few macros that I use, that I thought might be of use to others. They should be fairly easy to set up in your environment. I’m always excited to hear about new macros and plugins and other time-saving functionality for Visual Studio, so if you have any – please let me know!!

Misc Built-in Macros

Here are some basic macros that came with Visual Studio 2008 (and VS 2005) that are very useful and easily map to your favorite keyboard shortcut(s).

Macros.Samples.Accessibility.IncreaseTextEditorFontSize
Macros.Samples.Accessibility.DecreaseTextEditorFontSize
Macros.Samples.Accessibility.UpdateTextEditorFontSizeToMatchDisplayProperties
Macros.Samples.Utilities.TurnOnLineNumbers
Macros.Samples.Utilities.TurnOffLineNumbers
Macros.Samples.VSEditor.BeginningOfFunction
Macros.Samples.VSEditor.LineEmUp (Good for aligning variable assignments)
Macros.Samples.DevStudio6Editor.AutoCompleteFromFile


Format Source Code



I map F2 to the standard Edit.FormatDocument command and Ctrl+F2 to the FormatCSharp macro method.



Public Sub FormatCSharp()

Try
' Remove and sort using statements if PowerCommands is installed.
DTE.ExecuteCommand("Edit.RemoveAndSort")
Catch ex As Exception

End Try

Try
DTE.ExecuteCommand("Macros.Samples.VSEditor.FixLineEnds")
Catch ex As Exception
Exit Sub
End Try

DTE.ExecuteCommand("Edit.FormatDocument")

End Sub






Attach to IIS Worker Processes



I map Ctrl+P to this macro, replacing the default Print Current Document shortcut.



Sub MacroAttachToAllWebProcesses() 
Try
Dim dbg2 As EnvDTE80.Debugger2 = DTE.Debugger
Dim trans As EnvDTE80.Transport = dbg2.Transports.Item("Default")
Dim dbgeng(3) As EnvDTE80.Engine

dbgeng(0) = trans.Engines.Item("T-SQL")
dbgeng(1) = trans.Engines.Item("T-SQL")
dbgeng(2) = trans.Engines.Item("Managed")

' Use . for local machine or type machine name
For Each theProcess As EnvDTE80.Process2 In dbg2.GetProcesses(trans, ".")
If theProcess.Name.Contains("aspnet_wp.exe") Or theProcess.Name.Contains("w3wp.exe") Then
theProcess.Attach2(dbgeng)
End If
Next

Catch ex As System.Exception
MsgBox(ex.Message)
End Try

End Sub


Solution Events



' Automatically close all documents when the solution is closed.
Private Sub SolutionEvents_BeforeClosing() Handles SolutionEvents.BeforeClosing
DTE.ExecuteCommand("Window.CloseAllDocuments")
End Sub


Build Events



' A project failed to compile, so cancel remaining and beep
Private Sub BuildEvents_OnBuildProjConfigDone(ByVal Project As String, ByVal ProjectConfig As String, ByVal Platform As String, ByVal SolutionConfig As String, ByVal Success As Boolean) Handles BuildEvents.OnBuildProjConfigDone
If Success = False Then
DTE.ExecuteCommand("Build.Cancel")
' System.Windows.Forms.MessageBox.Show("Build failed!")
Beep()
Threading.Thread.Sleep(500)
Beep()
Threading.Thread.Sleep(500)
Beep()
Threading.Thread.Sleep(500)
Beep()
End If
End Sub

' The build finished successfully
Private Sub BuildEvents_OnBuildDone(ByVal Scope As EnvDTE.vsBuildScope, ByVal Action As EnvDTE.vsBuildAction) Handles BuildEvents.OnBuildDone
If (Not failed) Then
' System.Windows.Forms.MessageBox.Show("Build is complete!")
Beep()
Threading.Thread.Sleep(250)
Beep()
End If
End Sub

Tuesday, July 21, 2009

Simple Visual Studio 2005 regular expression to swap variable assignments

Here is a simple regular expression to use in Visual Studio 2005 to swap the variable/object and assignment.

Find What: mySettings.{.+} = {.+};
Replace With: \2 = mySettings.\1;