Partager via


ISpeechRecoGrammar CmdSetRuleState method (SAPI 5.3)

Microsoft Speech API 5.3

Interface: ISpeechRecoGrammar

CmdSetRuleState Method

The CmdSetRuleState method activates or deactivates a rule by its name.

  
    ISpeechRecoGrammar.CmdSetRuleState(
     Name As String,
     State As SpeechRuleState)

Parameters

  • Name
    The name of the rule to be changed. If Name is an empty string (""), all TopLevel and Active rules in the grammar will be changed.
  • State
    The rule state to which the rule or rules will be changed.

Return Value

None.

Example

The following Visual Basic form code demonstrates the use of the Rules method, the CmdSetRuleState method and the CmdSetRuleIdState method. To run this code, create a form with the following control:

  • A command button called Command1

Paste this code into the Declarations section of the form.

The Form1_Load procedure creates a grammar and loads it with the solitaire grammar sol.xml, and uses the Rules method to create a collection of the rules contained in the grammar. The Command1_Click procedure creates an ISpeechGrammarRule object for the first rule contained in the grammar, and inactivates this rule using the CmdSetRuleState method and the rule's Name property. The procedure then creates an ISpeechGrammarRule object for the second rule, and inactivates that rule using the CmdSetRuleIdState method and the rule's ID property.

  
Option Explicit

Dim MyRecoContext As SpeechLib.SpSharedRecoContext
Dim MyGrammar As SpeechLib.ISpeechRecoGrammar
Dim Rules As SpeechLib.ISpeechGrammarRules

Dim Rule As SpeechLib.ISpeechGrammarRule

Private Sub Command1_Click()
    On Error GoTo EH

    'Get first rule in rules collection and set it inactive
    'Use CmdSetRuleState method and the rule NAME.

    Set Rule = Rules.Item(0)
    MyGrammar.CmdSetRuleState Rule.Name, SGDSInactive

    'Get next rule in rules collection and set it inactive, too
    'Use CmdSetRuleIdState method and the rule ID.

    Set Rule = Rules.Item(1)
    MyGrammar.CmdSetRuleIdState Rule.Id, SGDSInactive

EH:
    If Err.Number Then ShowErrMsg
End Sub

Private Sub Form_Load()
    On Error GoTo EH

    Set MyRecoContext = New SpSharedRecoContext
    Set MyGrammar = MyRecoContext.CreateGrammar

    'Load Solitaire grammar dynamically so it can be changed.
    Call MyGrammar.CmdLoadFromFile("c:\sol.xml", SLODynamic)

    Set Rules = MyGrammar.Rules     'Get the collection of rules

EH:
    If Err.Number Then ShowErrMsg
End Sub

Private Sub ShowErrMsg()

    ' Declare identifiers:
    Const NL = vbNewLine
    Dim T As String

    T = "Desc: " & Err.Description & NL
    T = T & "Err #: " & Err.Number
    MsgBox T, vbExclamation, "Run-Time Error"
    End

End Sub