5,696,038 members and growing! (13,456 online)
Email Password   helpLost your password?
General Programming » Macros and Add-ins » VS.NET Macros     Intermediate

Visual Studio .NET Macros for quick insertion of C# Conditional and Iteration Statements

By Kevin McFarlane

These macros insert C# code constructs into your document at the insertion point.
C++, WindowsVS.NET2002, Visual Studio, Dev

Posted: 14 May 2002
Updated: 14 May 2002
Views: 34,238
Bookmarked: 12 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
6 votes for this Article.
Popularity: 3.02 Rating: 3.88 out of 5
2 votes, 50.0%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
0 votes, 0.0%
4
2 votes, 50.0%
5

Introduction

These macros insert C# code constructs into your document at the insertion point. To use, open Macro Explorer and copy and paste into an existing or new macro project. To run a macro place the cursor at the required insertion point in your document and double-click the name of the macro in Macro Explorer. You may wish to define more convenient keyboard shortcuts for each macro via the Tools->Customize menu.

Imports EnvDTE
Imports System.Diagnostics

Public Module CSharp
  ' Description: Inserts for loop

  Sub ForLoop()
    DTE.ActiveDocument.Selection.Text = "for (int i = 0; i < ; i++)"
    DTE.ActiveDocument.Selection.NewLine()
    DTE.ActiveDocument.Selection.Text = "{"
    DTE.ActiveDocument.Selection.NewLine()
    DTE.ActiveDocument.Selection.NewLine()
    DTE.ActiveDocument.Selection.Text = "}"
    DTE.ActiveDocument.Selection.LineUp(False, 3)
    DTE.ActiveDocument.Selection.CharRight(False, 19)
  End Sub

  ' Description: Inserts while loop

  Sub WhileLoop()
    DTE.ActiveDocument.Selection.Text = "while ()"
    DTE.ActiveDocument.Selection.NewLine()
    DTE.ActiveDocument.Selection.Text = "{"
    DTE.ActiveDocument.Selection.NewLine()
    DTE.ActiveDocument.Selection.NewLine()
    DTE.ActiveDocument.Selection.Text = "}"
    DTE.ActiveDocument.Selection.LineUp(False, 3)
    DTE.ActiveDocument.Selection.CharRight(False, 6)
  End Sub

  ' Description: Inserts do-while loop

  Sub DoWhileLoop()
    DTE.ActiveDocument.Selection.Text = "do"
    DTE.ActiveDocument.Selection.NewLine()
    DTE.ActiveDocument.Selection.Text = "{"
    DTE.ActiveDocument.Selection.NewLine()
    DTE.ActiveDocument.Selection.NewLine()
    DTE.ActiveDocument.Selection.Text = "} while ();"
    DTE.ActiveDocument.Selection.CharLeft(False, 2)
  End Sub

  ' Description: Inserts foreach loop

  Sub ForEach()
    DTE.ActiveDocument.Selection.Text = "foreach ( in )"
    DTE.ActiveDocument.Selection.NewLine()
    DTE.ActiveDocument.Selection.Text = "{"
    DTE.ActiveDocument.Selection.NewLine()
    DTE.ActiveDocument.Selection.NewLine()
    DTE.ActiveDocument.Selection.Text = "}"
    DTE.ActiveDocument.Selection.LineUp(False, 3)
    DTE.ActiveDocument.Selection.CharRight(False, 8)
  End Sub

  'Description: Inserts if block

  Sub IfNoElse()
    DTE.ActiveDocument.Selection.Text = "if ()"
    DTE.ActiveDocument.Selection.NewLine()
    DTE.ActiveDocument.Selection.Text = "{"
    DTE.ActiveDocument.Selection.NewLine()
    DTE.ActiveDocument.Selection.NewLine()
    DTE.ActiveDocument.Selection.Text = "}"
    DTE.ActiveDocument.Selection.LineUp(False, 3)
    DTE.ActiveDocument.Selection.CharRight(False, 3)
  End Sub

  ' Description: Inserts if-else block

  Sub IfElse()
    DTE.ActiveDocument.Selection.Text = "if ()"
    DTE.ActiveDocument.Selection.NewLine()
    DTE.ActiveDocument.Selection.Text = "{"
    DTE.ActiveDocument.Selection.NewLine()
    DTE.ActiveDocument.Selection.NewLine()
    DTE.ActiveDocument.Selection.Text = "}"
    DTE.ActiveDocument.Selection.NewLine()
    DTE.ActiveDocument.Selection.Text = "else"
    DTE.ActiveDocument.Selection.NewLine()
    DTE.ActiveDocument.Selection.Text = "{"
    DTE.ActiveDocument.Selection.NewLine()
    DTE.ActiveDocument.Selection.NewLine()
    DTE.ActiveDocument.Selection.Text = "}"
    DTE.ActiveDocument.Selection.LineUp(False, 7)
    DTE.ActiveDocument.Selection.CharRight(False, 3)
  End Sub

  ' Description: Inserts switch block

  Sub Switch()
    DTE.ActiveDocument.Selection.Text = "switch ()"
    DTE.ActiveDocument.Selection.NewLine()
    DTE.ActiveDocument.Selection.Text = "{"
    DTE.ActiveDocument.Selection.NewLine()
    DTE.ActiveDocument.Selection.Text = "case :"
    DTE.ActiveDocument.Selection.NewLine()
    DTE.ActiveDocument.Selection.Text = "break;"
    DTE.ActiveDocument.Selection.NewLine()
    DTE.ActiveDocument.Selection.Text = "case :"
    DTE.ActiveDocument.Selection.NewLine()
    DTE.ActiveDocument.Selection.Text = "break;"
    DTE.ActiveDocument.Selection.NewLine()
    DTE.ActiveDocument.Selection.Text = "default:"
    DTE.ActiveDocument.Selection.NewLine()
    DTE.ActiveDocument.Selection.Text = "break;"
    DTE.ActiveDocument.Selection.NewLine()
    DTE.ActiveDocument.Selection.Text = "}"
    DTE.ActiveDocument.Selection.LineUp(False, 8)
    DTE.ActiveDocument.Selection.CharRight(False, 7)
  End Sub

  ' Description: Inserts generic exception block

  Sub Exception()
    DTE.ActiveDocument.Selection.Text = "try"
    DTE.ActiveDocument.Selection.NewLine()
    DTE.ActiveDocument.Selection.Text = "{"
    DTE.ActiveDocument.Selection.NewLine()
    DTE.ActiveDocument.Selection.NewLine()
    DTE.ActiveDocument.Selection.Text = "}"
    DTE.ActiveDocument.Selection.NewLine()
    DTE.ActiveDocument.Selection.Text = "catch (System.Exception ex)"
    DTE.ActiveDocument.Selection.NewLine()
    DTE.ActiveDocument.Selection.Text = "{"
    DTE.ActiveDocument.Selection.NewLine()
    DTE.ActiveDocument.Selection.NewLine()
    DTE.ActiveDocument.Selection.Text = "}"
    DTE.ActiveDocument.Selection.LineUp(False, 5)
  End Sub

End Module
    

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Kevin McFarlane



Occupation: Web Developer
Location: United Kingdom United Kingdom

Other popular Macros and Add-ins articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
GeneralGreat JobmemberAvery Moore5:00 16 Mar '04  
GeneralRe: Great JobmemberKevin McFarlane9:17 16 Mar '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 14 May 2002
Editor: Chris Maunder
Copyright 2002 by Kevin McFarlane
Everything else Copyright © CodeProject, 1999-2008
Web08 | Advertise on the Code Project