简体   繁体   中英

how to add speech for dynamically generated textbox in winforms c#

I have created one winform app in which, on click of button, textbox will be generated. Now I have to add speech for these textboxes. For example if textbox's text is 'hello' there will be speech saying 'hello'.

I have tried this so far. :

private void btnAdd_Click(object sender, EventArgs e)
{
    TextBox textbox = new TextBox();
    int count = panel1.Controls.OfType<TextBox>().ToList().Count;
    textbox.Location = new System.Drawing.Point(60, 25 * count);
    textbox.Size = new System.Drawing.Size(220, 20);
    textbox.Multiline = true;
    textbox.Name = "textbox_" + (count + 1);
    //textbox.TextChanged += new System.EventHandler(this.TextBox_Changed);
    panel1.Controls.Add(textbox);
}

private void Form1_Load(object sender, EventArgs e)
{
    SpVoice vc = new SpVoice();
    vc.Speak(textbox1.Text, SpeechVoiceSpeakFlags.SVSFDefault);
}

I am using speechLib library for speech. Working fine in simple cases.

But when textbox is being created dynamically, I cannot access its ID or Name.

If the textbox is created in the button click event, then in the form_load event it doesn't exist, so you cannot use it as input for your voice library.

However, when you create the TextBox you could add a value for the property Tag.
For example

private void btnAdd_Click(object sender, EventArgs e)
{
    TextBox textbox = new TextBox();
    .....
    textbox.Name = "textbox_" + (count + 1);
    textbox.Tag = "SPEECH";
    ....
    panel1.Controls.Add(textbox);
}

Now, when you need to retrieve this dynamically created textbox and use your voice library you could write this. But for the example I use an event handler for a different button

private void AnotherButton_Click(object sender, EventArgs e)
{
    TextBox txt = panel1.Controls
                        .OfType<TextBox>()
                        .FirstOrDefault(x =x.Tag.ToString() == "SPEECH");

    if (txt != null)
    {
        SpVoice vc = new SpVoice();
        vc.Speak(txt.Text, SpeechVoiceSpeakFlags.SVSFDefault);
    }
    else
    {
        MessageBox.Show("Press the create button first!");
    }
}

If you want, you could also use the same button event handler. Check if the control with the specified Tag exists and if not create it, if yes execute the voice Speak call.

private void btnAdd_Click(object sender, EventArgs e)
{
    TextBox txt = panel1.Controls
                        .OfType<TextBox>()
                        .FirstOrDefault(x =x.Tag.ToString() == "SPEECH");

    if (txt != null)
    {
        SpVoice vc = new SpVoice();
        vc.Speak(txt.Text, SpeechVoiceSpeakFlags.SVSFDefault);
    }
    else
    {
        TextBox textbox = new TextBox();
        .....
        textbox.Name = "textbox_" + (count + 1);
        textbox.Tag = "SPEECH";
        ....
        panel1.Controls.Add(textbox);
    }
}

EDIT This assumes there is only one textbox with the SPEECH tag set. If you want more than one textbox then you should be able to tell which one you want to 'talk' (adding a little button on the right side of the textbox with a speaker image?) or loop over all the textboxes with the SPEECH tag and call Speak for each one

Something like this

var speechBoxes = panel1.Controls
                  .OfType<TextBox>()
                  .Where(x =x.Tag.ToString() == "SPEECH");

if (speechBoxes != null)
{
    SpVoice vc = new SpVoice();

    foreach(TextBox txt in speechBoxes)
    {
        vc.Speak(txt.Text, SpeechVoiceSpeakFlags.SVSFDefault);

        // Not sure if you need also to add this call ....
        vc.WaitUntilDone(10000);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM