locked
Hi how can i make a windows form button work for on and off ? RRS feed

  • Question

  • Hi how can i make a windows form button work for on and off ? So when I click it the first time it turns my speech synthesis on and the next time i click it turns it off ? This is what i have tried but it doesn't work :(

    private void button1_Click(object sender, EventArgs e)
            {
                SpeechSynthesizer SS = new SpeechSynthesizer();
                bool onOff = true;
    
                if (onOff == true)
                {
                    SS.Speak(forReading.Text);
                }
                else if (onOff == false)
                {
                    SS.Dispose();
                }
                
            }

    forReading.Text is my textbox to read from.

    I thought maybe using a bool(onOff) may be the way but I have no experience so im only guessing


    HaKDMoDz

    Monday, August 10, 2015 9:34 AM

Answers

  • Hi,

    move the bool onOff outside the methods body, to make it a class level variable to make it keep the value you set:

    bool onOff = true;
    
    private void button1_Click(object sender, EventArgs e)
            {
                SpeechSynthesizer SS = new SpeechSynthesizer();
                
                if (onOff == true)
                {
                    SS.Speak(forReading.Text);
                }
                else if (onOff == false)
                {
                    SS.Dispose();
                }
                
               //maybe set/reset onOff here too
            }

    Regards,

      Thorsten

    • Proposed as answer by Blackwood Monday, August 10, 2015 3:15 PM
    • Marked as answer by Youjun Tang Tuesday, August 18, 2015 3:11 AM
    Monday, August 10, 2015 3:09 PM