Stupid Visual Studio Tricks: Speak ‘N Code

by Jon Davis 7. March 2009 01:58

I got bored tonight and created a Visual Studio macro that reads my code to me as I type it. First I had it read the keys (“a”, “b”) like a Speak ‘N Spell, but then I realized it’s better if it actually just waits for me to spell out the words instead. It also correctly pronounces a few special characters.

I have absolutely no use for this but for “duhh huh huh huh” giggles.

If you care to play with it, just open Visual Studio, choose Tools –> Macros –> Macros IDE from the menu, double-click MyMacros on the left, double-click EnvironmentEvents, then paste the following code inside the module body.

Private Speech As Object
Private Sub TextDocumentKeyPressEvents_BeforeKeyPress(ByVal Keypress _
As String, ByVal Selection As EnvDTE.TextSelection, _
ByVal InStatementCompletion As Boolean, ByRef CancelKeyPress As Boolean) _
Handles TextDocumentKeyPressEvents.BeforeKeyPress
If Speech Is Nothing Then
Speech = Activator.CreateInstance(Type.GetTypeFromProgID("sapi.spvoice"))
Speech.Voice = Speech.GetVoices("gender=male")(0)
End If
Dim Key As String
Key = Keypress
If Key = vbBack Then Return
Select Case Keypress
Case "!"
Key = "not"
Case "%"
Key = "mod"
Case "@"
Key = "at"
Case "#"
Key = "pound"
Case "^"
Key = "caret"
Case "&"
Key = "and"
Case "*"
Key = "star"
Case "/"
Key = "slash"
Case "\"
Key = "backslash"
Case ","
Key = "comma"
Case "<"
Key = "less than"
Case ">"
Key = "greater than"
Case ":"
Key = "colon"
Case "("
Key = "" ' "pahrens"
Case ")"
Key = "" ' "close pahrens"
Case "."
Key = "dot"
Case "{"
Key = "" ' "begin brace"
Case "}"
Key = "" ' "close brace"
Case ";"
Key = "" ' "semicolon"
Case vbTab
Key = "" ' "tab"
Case " "
Key = "" ' "space"
Case "["
Key = "" ' "left bracket"
Case "]"
Key = "" ' "right bracket"
Case """"
Key = "" ' "double quotes"
Case "'"
Key = "single quote"
Case vbCr, vbLf, vbCrLf
Key = "" ' "line break"
End Select
Dim Say As String
Const a2z As String = "abcdefghijklmnopqrstuvwxyz"
If Not a2z.Contains(Keypress.ToLower()) Then
Say = Key
Dim editPoint = Selection.ActivePoint().CreateEditPoint()
If Not IsWhitespace(editPoint.GetText(1)) Then
Dim Word = editPoint.GetText(Selection.ActivePoint.AbsoluteCharOffset - editPoint.AbsoluteCharOffset)
Dim HasLetter As Boolean
For Each letter In Word
If a2z.Contains(letter) Then HasLetter = True
If HasLetter Then
If Not IsWhiteSpace(Keypress) Then
Say = Word & ", " & Say
Say = Word
End If
End If
End If
End If
If Say <> "" Then
Speech.Speak(Say, SpeechVoiceSpeakFlags.SVSFlagsAsync + SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak)
End If
End Sub
Function IsWhitespace(ByVal s As String)
Dim rgx As New System.Text.RegularExpressions.Regex("\s*")
Return rgx.Match(s).Length > 0
End Function
Enum SpeechVoiceSpeakFlags
'SpVoice Flags
SVSFDefault = 0
SVSFlagsAsync = 1
SVSFPurgeBeforeSpeak = 2
SVSFIsFilename = 4
SVSFPersistXML = 32
'Normalizer Flags
SVSFNLPSpeakPunc = 64
SVSFNLPMask = 64
SVSFVoiceMask = 127
SVSFUnusedFlags = -128
End Enum

2 Corrections: LINQ *Can* Use POCO/SQL, and VB.NET *Doesn't* Suck

by Jon Davis 8. January 2009 21:17

Just a couple observations here.

1. LINQ *can* work with POCO/SQL.

A few (two or three?) posts ago, I complained that LINQ-to-SQL doesn't really "speak" SQL / ADO.NET, you're forced to use generated classes and a designer to work with the provider. Thus, if you want to avoid the designer and generated classes, you're forced to using plain old ADO.NET.

I am VERY happy to say that I need to go do more homework. LINQ-to-SQL does have POCO support, and in fact when reading this blog entry I realized that a week's worth of work on my pet project is garbage because it is exactly to detail what this blog seems to show that LINQ already designed it. 

Why does this matter? Because I believe that a business object should be database / provider / ORM agnostic. It should NEVER implement database code within itself, and it shouldn't even inherit an ORM library object. I would be willing to tack on attributes/markers that don't do anything except provide mapping cues for an external ORM.

But even attributes can be avoided, and in the above-linked article's case, avoiding them is suggested. (I don't agree with that, though, because some members are database-bound, sometimes they are not, sometimes they are named differently from the database table columns, etc.)  

2. VB.NET Actually Doesn't Suck

While I think the syntax of VB.NET is painful to look at, to say nothing of writing, I must say that I greatly admire the features that set it apart from C#. Before v2008 I didn't think they were enough. ("My"?! Come on! Even the Windows Vista team was smart enough to eliminate "My" verbiage from Windows. You're going the wrong way!)

But at a .NET users group meeting I attended a couple months ago, I noticed the profound value of v2008's integrated XML support. I'm not talking about just Dim xx = <myxml>..</myxml>. That in itself is incredibly impressive, no doubt. But what impresses me more is how you can bury such XML expressions in LINQ statements. Add on top of that the ASP-style templating that is supported with it; you can do this: Dim xx = <myxml><%=myXmlValue%></myxml> And even this: 

Dim xx = <myxml>
<% For Each x In y %>
<% Next %>

I haven't validated that but that's what I understand it to offer. That demonstration doesn't say much until you see it in action... 

The blog link I mentioned above (under #1) has at the bottom of it a "crosspost" link, the recent article of which reminded me of this VB.NET functionality and made me realize that this really is something worth blogging about. And if I have to work with VB.NET in the future, so long as it is with .NET 3.5 / Visual Studio 2008, I shouldn't pass it up anymore. 

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

Software Development | VB.NET


Powered by BlogEngine.NET
Theme by Mads Kristensen

About the author

Jon Davis (aka "stimpy77") has been a programmer, developer, and consultant for web and Windows software solutions professionally since 1997, with experience ranging from OS and hardware support to DHTML programming to IIS/ASP web apps to Java network programming to Visual Basic applications to C# desktop apps.
Software in all forms is also his sole hobby, whether playing PC games or tinkering with programming them. "I was playing Defender on the Commodore 64," he reminisces, "when I decided at the age of 12 or so that I want to be a computer programmer when I grow up."

Jon was previously employed as a senior .NET developer at a very well-known Internet services company whom you're more likely than not to have directly done business with. However, this blog and all of have no affiliation with, and are not representative of, his former employer in any way.

Contact Me 

Tag cloud


<<  May 2021  >>

View posts in large calendar