StrataFrame Forum

Fun with Macros

http://forum.strataframe.net/Topic15697.aspx

By Charles R Hankey - 4/16/2008

In VFP I enjoyed the comment / uncomment right click menu option for blocks of code.  The closest I've been able to find in the VB 2008 IDE is a macro sample to comment and nothing to uncomment.  Soooo, this may be old hat to experienced VS users but if you're a newbie like me you may find this useful :

Tools / Customize / Macros

Now you can drag that Samples.VSEditor.CommentRegion right onto a toolbar.  Then, with the Customize dialog still open, right click on what you dragged onto the bar and select Default Style

Which will reduce it to a blank icon and then pick Chang Button Image to give it an icon.

Okay, now if you hightlight a block of text in code it will all be commented out with single quotes

But what of uncommenting ?

I wrote this macro which you can paste into the macro file that comes up when you open the Macros IDE ( you can drag that out of Tools in customize and onto a menu )

Sub UncommentBlock()

Dim current As String

current = DTE.Activedocument.Name

DTE.ExecuteCommand("Edit.Find")

DTE.ExecuteCommand("Edit.SwitchtoQuickReplace")

DTE.ExecuteCommand("Edit.SelectAll")

DTE.Windows.Item(current).Activate()

DTE.Find.FindWhat = "'"

DTE.Find.ReplaceWith = ""

DTE.Find.Target = vsFindTarget.vsFindTargetCurrentDocumentSelection

DTE.Find.MatchCase = False

DTE.Find.MatchWholeWord = False

DTE.Find.MatchInHiddenText = True

DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteral

DTE.Find.ResultsLocation = vsFindResultsLocation.vsFindResultsNone

DTE.Find.Action = vsFindAction.vsFindActionReplaceAll

If (DTE.Find.Execute() = vsFindResult.vsFindResultNotFound) Then

Throw New System.Exception("vsFindResultNotFound")

End If

DTE.Windows.Item("{CF2DDC32-8CAD-11D2-9302-005345000000}").Close()

End Sub

Just does a search and replace for ' inside a highlighted block

As soon as you save the macro file, you'll find that one the same place you found the CommentRegion() and you can put it on a toolbar the same way.

I'll put the tasklist thing in a separate message.

By Charles R Hankey - 4/16/2008

And here's the comment / task list token macro :

'And you can customize this even further by using YOUR initials instead of mine :-)

Sub InsertCRH()

Dim textSelection As EnvDTE.TextSelection

textSelection = CType(DTE.Activedocument.Selection(), EnvDTE.TextSelection)

textSelection.Text = "' CRH: " & System.DateTime.Now.ToString() & CrLf _

& "' "

End Sub

See prior message for putting it on a toolbar and my message about snippets in General .NET section to see about the method and reasons to put your initials into a task list token

Charles

By Greg McGuffey - 4/16/2008

Do you know about the keyboard shortcuts?



Highlight the code desired and then

Ctl +k+c to comment

Ctl +k+u to uncomment
By Trent L. Taylor - 4/17/2008

Do you know about the keyboard shortcuts?

Highlight the code desired and then
Ctl +k+c to comment
Ctl +k+u to uncomment

Very close and personal friends BigGrin

By Charles R Hankey - 4/17/2008

Do you know about the keyboard shortcuts?

Obviously no Blush

Seemed like there should be something like that but darned if I could find them ( where shoulld I have been looking to find a list of such shortcuts? )

Oh well, learned a lot of cool stuff about macros along the way. 

Goes along with my life maxim that most of the valuable stuff I know I learned on my way to looking up something else Smile

By Charles R Hankey - 4/17/2008

gotta remember to google !!!

http://blogs.msdn.com/vbteam/archive/2007/04/24/save-time-use-keyboard-shortcuts-lisa-feigenbaum.aspx

http://www.microsoft.com/downloads/details.aspx?FamilyID=6bb41456-9378-4746-b502-b4c5f7182203&DisplayLang=en

By Ivan George Borges - 4/18/2008

Hey, thanks for the links Charles. Wink
By Greg McGuffey - 4/18/2008

Goes along with my life maxim that most of the valuable stuff I know I learned on my way to looking up something else Smile




Ahmen! And I learned a lot about marcos from your example BigGrin