Application to speak the text in the textbox using C#.Net

Design

Design

Design the form as shown above with one TextBox and three Buttons, Set the 'textBox1' Properties as follows:

  • Dock: Top,
  • Multiline: True.

Now go to the 'Project' Menu -> Select 'AddReference'-> Click on the 'COM' tab.

Select 'Microsoft Speech Object Library' COM component -> OK.

Microsoft Speech Object

Now go to the code window and include the 'using SpeechLib' namespace

Code

using System;
using System.Windows.Forms;
using SpeechLib; // include this namespace

namespace TextSpeaker
{
    public partial class TextSpeakerForm : Form
    {
        public TextSpeakerForm()
        {
            InitializeComponent();
        }

        private void btnSpeak_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Trim().Length > 0)
            {
                SpVoice obj = new SpVoice();
                obj.Speak(textBox1.Text, SpeechVoiceSpeakFlags.SVSFDefault);
            }
            else
            {
                MessageBox.Show("Please write some text in the TextBox", "Info.", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

Output

Write some text in the textbox and press the 'speak' button.

Textbox


Similar Articles