简体   繁体   中英

Access form controls from static events in static class

I using Speech Recognition in Form Proyect, I have a static class with the functions initialize the SR engine.

In this static class i declare public static form = new formX()

My problem is, when i detect the speechrecognition event, i need to update the text control in the formX, but the IDE say the text control dont exists in the form, i think is because the speechEngine uses separate thread.

    static General()
    {

        General.ChatForm = new ChatForm();

    }


    public static void startSpeechRecognition()
    {

        // Setup grammar rules:
        GrammarBuilder builder = new GrammarBuilder();
        builder.AppendDictation();

        grammar = new Grammar(builder);

        // Initiate Recognizer and Setup Events:
        recognizer = new SpeechRecognitionEngine(/*new CultureInfo("es-ES")*/);
        recognizer.LoadGrammar(grammar); // Poner otro try aqui, si falla, es que no tiene configurado el sistema de voz
        recognizer.SetInputToDefaultAudioDevice(); // Poner un try aqui, si falla es que no tiene microfono configurado.

        recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);

        // Initialize Recognizer thread:
        RecognizerState = false;
        RecThread = new Thread(new ThreadStart(RecThreadFunction));
        RecThread.Start();

    }

    static void RecThreadFunction()
    {
        // this function is on separate thread (RecThread). This will loop the recognizer receive call.
        while (true)
        {
            try
            {
                recognizer.Recognize();
            }
            catch
            {
                // handle Errors. Most errors are caused from the recognizer not recognizing speech.
            }

        }
    }



    static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {
        // Event raised when the speech recognizer recognizes speech.
        /*
        if (!RecognizerState)
        {
            return;
        }
         * */


        General.ChatForm.richTextBox1.Text += (" " + e.Result.Text.ToLower());


    }

My poblem is the line General.ChatForm.richTextBox1.Text += (" " + e.Result.Text.ToLower());, the IDE show "System.windows.Forms.Form does not contain a definition for 'richtextBox1' and no extension method 'richTextBox1' accepting a first argument of type System.Windows.Forms.Form could be found"

您需要将静态字段或属性ChatFormChatForm类型,而不是Form

private ChatForm ChatForm;

Change the modifier of the richtextBox1 to public in your form.

修改器

if General.ChatForm is type of Form , use (General.ChatForm as ChatForm).richtextBox1 or as Damir said change its type to ChatForm .

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