Search code examples
c#text-to-speechspeech

how to read an highlighting and specific word in a textbox?


i have done my project which is Text to Speech from text file and PDF file but it read the whole file word by word continuously. my problem : if i want the program read a word which i have highlighted it by press double on it only, how can i make this in c# coding.

Can you help me , please ?

[

if (richTextBox1.Text != "")

        {   try

            {
                switch (comboBox1.SelectedItem.ToString())

                {
                    case "NotSet":
                        { voice.SelectVoiceByHints(VoiceGender.NotSet);
                        voice.SpeakAsync(richTextBox1.Text);
                        voice.Volume = trackBar1.Value;
                        voice.Rate = trackBar2.Value;
                        }
                        break;

                    case "Male":
                        {
                            voice.SelectVoiceByHints(VoiceGender.Male);
                            voice.SpeakAsync(richTextBox1.Text);
                            voice.Volume = trackBar1.Value;
                            voice.Rate = trackBar2.Value;
                        }
                        break;
                    case "Neutral":

                        {
                            voice.SelectVoiceByHints(VoiceGender.Neutral);
                            voice.SpeakAsync(richTextBox1.Text);
                            voice.Volume = trackBar1.Value;
                            voice.Rate = trackBar2.Value;
                        }
                        break;

                    case "Samar":
                        {
                           // voice.SelectVoiceByHints(VoiceGender.Female);
                            voice.Volume = trackBar1.Value;
                            voice.Rate = trackBar2.Value;
                            Pause.Enabled = true;
                            Stop.Enabled = true;
                            //SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=speakerpro;Integrated Security=True");

                            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=speakerpro;Integrated Security=True");
                            //انشاء قائمة اصوات جديدة
                            WMPLib.IWMPPlaylist firstPlaylist = axWindowsMediaPlayer4.playlistCollection.newPlaylist("MyPlayList");

                            //تقسيم النص
                            string[] stArray = richTextBox1.Text.Trim().Split(' ');
                            foreach (string stWords in stArray)
                            {

                                SqlDataAdapter adp = new SqlDataAdapter("select path from [en1] where word = '" + stWords + "'", con);
                                DataTable dt = new DataTable();
                                adp.Fill(dt);

                                if (dt.Rows.Count == 0)
                                {
                                    MessageBox.Show("error1");
                                }
                                else
                                {
                                    //نضيف المسارات الى قائمة اولا بدون تشغيلها

                                    var mediaItem = axWindowsMediaPlayer4.newMedia(dt.Rows[0]["path"].ToString());
                                    firstPlaylist.appendItem(mediaItem);
                                    //    richTextBox3.SelectionBackColor = Color.Yellow;
                                    //axWindowsMediaPlayer1.URL = ds.Tables["check"].Rows[0]["path"].ToString();
                                }
                            }

                            //تشغيل جميع المسارات
                            axWindowsMediaPlayer4.currentPlaylist = firstPlaylist;
                            //string txtstr = richTextBox3.Text.Trim() + " ";


                        }
                        break;
                }






            }
            catch (Exception)
            {
                MessageBox.Show("Speak error", "SAH", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

    else
        { MessageBox.Show("Enter your text, please !"); }
    }




    private void button3_Click(object sender, EventArgs e)
    {

        voice.Pause();
        Resume.Enabled = true;

    }

    private void button4_Click(object sender, EventArgs e)
    {
        voice.Resume();
    }

]


Solution

  • Use the SelectedText property of the RichTextBox:

    voice.SpeakAsync(richTextBox1.SelectedText);