Voice Recognition in VB .NET

Having recently installed VB .NET 2005, I figured it would be a good idea to upgrade the Voice Recognition articles to include VB .NET as well. Because most of the basics are the same, I will cover only the changes necessary to get the Speech SDK to work on VB .NET. Part 1 covered using the Voice Command Control. Now, I will cover how to use it in VB .NET. After installing the Speech SDK, start a new project in VB .NET.

On the Tools menu, select Choose Toolbox Items and select the COM Objects tab. In the list, tick vCommand Class and click OK. vCommand Class is now listed under Common Controls. Select it and add it to your Form. You now can use it as described in Part One. Included in the downloads is a VB .NET version of the first project.

Note: Some of the properties and methods have sightly different names in Vb .NET. For example, VoiceCmd.Enabled is now VoiceCmd.CtlEnabled.

Look closer at the project and you will see a few minor differences. The only one I’m going to cover is in the Form load event.

Private Sub Frm_Voice_Command_Load(ByVal eventSender As System.Object, _
   ByVal eventArgs As System.EventArgs) Handles MyBase.Load
Dim Category, Command, Description, Action As String
Dim Flags As Integer
Command     = vbNull
Description = vbNull
Category    = vbNull
Action      = vbNull
VoiceCmd.Initialized = 1
My_menu = VoiceCmd.get_MenuCreate("My Commands", "commands State", 4)
VoiceCmd.CtlEnabled = 1
.....
VoiceCmd.AddCommand(My_menu, 1, "stop listening", "Stop Listen", _
                    "listen list", 0, "")
VoiceCmd.Activate(My_menu)
Cmd_Start.Text = "Stop Listening"
Lst_Commands.Items.Clear()
TCount = VoiceCmd.get_CountCommands(My_menu)
For Loop_1 = 1 To TCount
   VoiceCmd.GetCommand(My_menu, Loop_1, Command, Description, _
                       Category, Flags, Action)
   Lst_Commands.Items.Add(Command)
Next Loop_1

The first few seem to be a requirement from VB .NET; the variables have to be assigned a value before they can be passed to the control. So, you set them to VbNull to overcome the problem. The last one is the name change explained in the note above.

Part 2 covered adding the Voice Diction Control to your application. Again, most of the methods and properties are identical, with only minor changes as listed previously required.

To add the Voice Diction control, you use the same process as before, except, in the list, select the vDict Class. Included is a VB .NET download of Project 3.

Part 4 covered adding the Direct Speach Synthesizer Control to your application. To add the Direct Speach Synthesizer Control, you use the same process as before, except, in the list, select the DirectSS Class. Again, there are no major changes required to get it to work. The code ports into VB .NET without complications. In the downloads is a VB .NET version of the datapad clone project.

As you may notice when working on this project, Microsoft has made a lot of changes to the advanced options of VB in the .NET range, but most of the basics remain the same. Using voice recognition in .NET may take some time to debug 100%, but it is not as difficult as you may assume.

Happy programming…

More by Author

Must Read