For everyone who doesn't know how to implement this with a speech synthesizer, do as follows:
Step 1:
add a reference to the System.Speech Name space like this:
Right click on the references folder and click "Add Reference..."
then a window should pop up, click the tab that says ".NET" then scroll down the list until you find the entry named System.Speech. select it and click the "Ok" button
Step 2:
add the following to the the top of your winform app code(we are adding a reference to the System.Speech.Synthesis namespace)
using System.Speech.Synthesis;
Step 3: right above the apps constructer(at the class level) add this:
SpeechSynthesizer synth = new SpeechSynthesizer();
synth.SpeakProgress += new EventHandler<speakprogresseventargs>(synth_SpeakProgress);
synth.SpeakCompleted += new EventHandler<speakcompletedeventargs>(SpeakOperationCompleted);
</speakcompletedeventargs></speakprogresseventargs>
Step 4: now add this method:
#region SpeakProgress
private void synth_SpeakProgress(object sender, SpeakProgressEventArgs e)
{
if (currentWord == theWordsLocations.Count)
{
WhateverTheNameOfYourTextBox.SelectionColor = previousSelectionForeColor;
WhateverTheNameOfYourTextBox.SelectionBackColor = previousSelectionBackColor;
}
else
{
previousSelection = new Point(TB.SelectionStart, TB.SelectionLength);
if (previousSelection != Point.Empty)
{
WhateverTheNameOfYourTextBox.SelectionColor = previousSelectionForeColor;
WhateverTheNameOfYourTextBox.SelectionBackColor = previousSelectionBackColor;
}
previousSelectionForeColor = TB.SelectionColor;
previousSelectionBackColor = TB.SelectionBackColor;
var location = theWordsLocations[currentWord];
WhateverTheNameOfYourTextBox.Select(location.X, location.Y);
WhateverTheNameOfYourTextBox.SelectionColor = HighLightForeColor;
WhateverTheNameOfYourTextBox.SelectionBackColor = HighLightBackColor;
if (currentWord == nWords)
{
WhateverTheNameOfYourTextBox.SelectionColor = previousSelectionForeColor;
WhateverTheNameOfYourTextBox.SelectionBackColor = previousSelectionBackColor;
}
currentWord++;
}
}
Step 5: now add the this "SpeakOperationCompleted" method to your code
#region Speak Operation Completed
public void SpeakOperationCompleted(object sender, SpeakCompletedEventArgs e)
{
WhateverTheNameOfYourTextBox.ReadOnly = false;
WhateverTheNameOfYourTextBox.SelectAll();
WhateverTheNameOfYourTextBox.SelectionColor = previousSelectionForeColor;
WhateverTheNameOfYourTextBox.SelectionBackColor = previousSelectionBackColor;
}
#endregion
Step 5: add this method to your code(this is the method that will be called to actually speak the text you specify):
public void SpeakSynthText(String TextToSpeak)
{
#region SpeechSynth
synth.SetOutputToDefaultAudioDevice();
synth.SelectVoiceByHints(VoiceGender.Female);
synth.SpeakAsync(TextToSpeak);
#endregion
}
Step 6: now add a button to the form, double click on it and paste the following code:
public void SpeakText_Click_1(object sender, EventArgs e)
{
TB.ReadOnly = true;
nWords = 0;
theWords.Clear();
theWordsLocations.Clear();
loc = 0;
theWords = TB.Text.Split(splitChar, StringSplitOptions.RemoveEmptyEntries).ToList();
nWords = theWords.Count;
foreach (var word in theWords)
{
theWordsLocations.Add(new Point(loc, word.Length));
loc += word.Length + 1;
}
currentWord = 0;
SpeakSynthText(WhateverTheNameOfYourTextBox.Text);
}
#endregion
and there you have it!
just type whatever you want to have the Speech Synth Say and click the button you added to the form and watch the text box as it will highlight the current word being spoken by the TTS engine.
if this doesn't work for you or you have any questions please state the problem or question in the comments of this post.
all credit for the text selection idea goes to BillWoodruff.
Thank you soo much Bill!
MasterCodeon