This post is about how to implement text to speech on a Windows Form.

Ctrl + Shift + N > Select Visual C# > Select Windows Classic Desktop > Click Windows Forms App (.Net Framework) > click Ok.

Refer to the System.Speech.dll from the following location,

C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\YOUR .NET FRAMEWORK VERSION\System.Speech.dll

Example:

C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\System.Speech.dll

Add the following reference on top,

  1. using System.Speech.Synthesis;

In the design mode, double click on the button to generate the click event.

Paste the following code in the generated button click event.

  1. //Start
  2. if (textBox1.Text.Trim().Length <= 0) {
  3. MessageBox.Show("Please enter a text for the system to speak.");
  4. } else {
  5. string toSpeak = textBox1.Text;
  6. SpeechSynthesizer speechSynthesizer = new SpeechSynthesizer();
  7. speechSynthesizer.Speak(toSpeak);
  8. speechSynthesizer.Dispose();
  9. }
  10. //End

Run the code by pressing F5. Now, enter some text that you want the system to say.

Thank you