I have a difficult time to combine this code with checkbox.
I want the code run when the checkbox is enabled(true) and stop when the checkbox is disabled(false) but for some reason i cant and i want some help.
If anyone has a solution or sth different to propose i will be grateful.
The code is: private void Form1_Load(object sender, EventArgs e)
Choices cities = new Choices(new string[] { "google" });
Grammar gr = new Grammar(new GrammarBuilder(cities));
SpeechRecognitionEngine recognize = new SpeechRecognitionEngine();
SpeechSynthesizer Synth = new SpeechSynthesizer();
recognize.SetInputToDefaultAudioDevice();
DictationGrammar Voc = new DictationGrammar();
recognize.LoadGrammar(Voc);
recognize.RecognizeAsync(RecognizeMode.Multiple);
recognize.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recognize_SpeechRecognized);
}
**private void recognize_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)**
{
if (e.Result.Text == "google")
System.Diagnostics.Process.Start("http://www.google.com");
}
**(and possibly sth that touches the solution?)**
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked == true)
recognize.RecognizeAsync(RecognizeMode.Multiple);
else if (checkBox1.Checked == false) // turn off
recognize.RecognizeAsyncCancel();
}
THANKS
I'm guessing you want the checkbox to represent whether the speech recognition is currently active or not and use it to toggle it. The problem with your code is that the initial state of the checkbox does not correspond to the state of the speech recognition, which you start immediately.
You can solve it by either setting the initial checked state of the checkbox to true
or removing the following line in the initial block:
recognize.RecognizeAsync(RecognizeMode.Multiple);
since this code will run anyway on the checkbox event handler. You will also need to pull the definition of the recognize
variable to the class level, outside of Form1_Load
.