Search This Blog

Tuesday, September 28, 2010

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

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

Design:


                               
Design the form as shown above with one TextBox and three Buttons,
Set the ‘textBox1’ Properties as follows:
Dock: Top,
Multiline: True.

->Now  goto ‘Project’ Menu -> Select ‘AddReference’-> Click on ‘COM’ tab,
    Select ‘Microsoft Speech Object Library’ COM component -> OK

                    

->Now goto code window and include ‘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);
            }
            MessageBox.Show("Plz. 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 ‘speak’ button