Click here to Skip to main content
Licence CPOL
First Posted 14 May 2002
Views 43,980
Bookmarked 15 times

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

By | 14 May 2002 | Article
These macros insert C# code constructs into your document at the insertion point.

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Kevin McFarlane

Web Developer

United Kingdom United Kingdom

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralGreat Job PinmemberAvery Moore4:00 16 Mar '04  
GeneralRe: Great Job PinmemberKevin McFarlane8:17 16 Mar '04  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 15 May 2002
Article Copyright 2002 by Kevin McFarlane
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid