# vb.net events not firing



## shuggans (May 6, 2013)

Ive created an event in my code, however the sub I created to handle the event is not firing - any ideas o nthe code below?


```
Imports System.Speech.Recognition
Public Class Form1

    Public Shared Recognizer As SpeechRecognizer
    Public Event LoadGrammarCompleted As EventHandler(Of LoadGrammarCompletedEventArgs)

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Recognizer = New SpeechRecognizer
        Dim dictation As Grammar = New DictationGrammar
        Recognizer.LoadGrammar(dictation)

    End Sub
    Public Sub LoadGrammar(ByVal sender As Object, ByVal e As LoadGrammarCompletedEventArgs) Handles MyClass.LoadGrammarCompleted
        MessageBox.Show("Bing!")
    End Sub

End Class
```


----------



## FordGT90Concept (May 6, 2013)

The event is never raised.


http://msdn.microsoft.com/en-us/library/fwd3bwed(v=vs.71).aspx

LoadGrammar needs RaiseEvent LoadGrammarCompleted(e)


Edit: Actually, you got some circular logic going on there. because Completed points to LoadGrammar and LoadGrammar would point to LoadGrammar.

You should have a GrammarLoaded function or something that handles the completed event.

More info:
http://msdn.microsoft.com/en-us/library/2z7x8ys3(v=vs.80).aspx


You RaiseEvent and the Event runs the Handles.


----------



## shuggans (May 6, 2013)

loadgrammarcompleted looks for sender as obect and e as system.speech.recognition.loadgrammarcompletedeventargs

I cant find what it's looing for.  Ive tried me, e and get an error saying "Unable to cast object of type 'system.eventargs' to type 'system.speech.recognition.loadgrammarcompletedeventargs.'"


----------



## FordGT90Concept (May 6, 2013)

You have to call New LoadGrammarCompletedEventArgs() most likely and make sure to give it what it requires.

I don't know what code you're working with but perhaps you aren't supposed to raise the event at all, it does.  You just need to start whatever process in that library that would raise it (a .Start() or something).

Also, the Recognition code should have the event, not your form.  Your form needs to hook into its event, not create a new one.


----------

