Zenth
Zenth

Reputation: 811

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"

Upvotes: 0

Views: 170

Answers (2)

Ali Sepehri.Kh
Ali Sepehri.Kh

Reputation: 2508

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

Changing modifier

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

Upvotes: 0

Damir Arh
Damir Arh

Reputation: 17855

You need to make your static field or property ChatForm of type ChatForm instead of Form:

private ChatForm ChatForm;

Upvotes: 1

Related Questions