Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The Error is:

'string.Trim(params char[])' is a 'method', which is not valid in the given context


And The Source Code is:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SpeechLib;
using System.Speech.Recognition;

namespace SpeechToTextTest
{
    public partial class Form1 : Form
    {
        private SpeechRecognitionEngine withEventsField_sre;

        public SpeechRecognitionEngine sre
        {
            get { return withEventsField_sre; }
            set
            {
                if (withEventsField_sre != null)
                {
                    withEventsField_sre.LoadGrammarCompleted -= sre_LoadGrammarCompleted;
                    withEventsField_sre.SpeechHypothesized -= sre_SpeechHypothesized;
                    withEventsField_sre.SpeechRecognitionRejected -= sre_SpeechRecognitionRejected;
                    withEventsField_sre.SpeechRecognized -= sre_SpeechRecognized;
                }
                withEventsField_sre = value;
                if (withEventsField_sre != null)
                {
                    withEventsField_sre.LoadGrammarCompleted += sre_LoadGrammarCompleted;
                    withEventsField_sre.SpeechHypothesized += sre_SpeechHypothesized;
                    withEventsField_sre.SpeechRecognitionRejected += sre_SpeechRecognitionRejected;
                    withEventsField_sre.SpeechRecognized += sre_SpeechRecognized;
                }
            }

        }



        public Form1()
        {
            Load += Form1_Load;
            InitializeComponent();
            sre = new SpeechRecognitionEngine();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string[] words = new string[] { "Kathy S" };
            Choices c = new Choices(words);
            GrammarBuilder grmb = new GrammarBuilder(c);
            Grammar grm = new Grammar(grmb);
            sre.LoadGrammar(grm);
        }


        private void btnLiterate_Click(object sender, EventArgs e)
        {
            if (TextBox1.Text.Trim.Length == 0)
                return;
            sre.SetInputToWaveFile(TextBox1.Text);
            RecognitionResult r = default(RecognitionResult);
            r = sre.Recognize();
            if (r == null)
            {
                TextBox2.Text = "Could not fetch result";
                return;
            }
            TextBox2.Text = r.Text;
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            TextBox1.Text = string.Empty;

            OpenFileDialog fileDlg = new OpenFileDialog();
            //wave As WaveFile()

            if (fileDlg.ShowDialog() == DialogResult.OK)
            {
                //wave = New WaveFile(fileDlg.FileName)

                TextBox1.Text = "Reading .WAV file...";

                //wave.Read()

                TextBox1.Text = OpenFileDialog1.FileName;
                //TextBox1.Text = "Finished Reading .WAV file..."

                //m_DrawWave = True

                Refresh();
            }

        }

        private void sre_LoadGrammarCompleted(object sender, System.Speech.Recognition.LoadGrammarCompletedEventArgs e)
        {
        }

        private void sre_SpeechHypothesized(object sender, System.Speech.Recognition.SpeechHypothesizedEventArgs e)
        {
            System.Diagnostics.Debug.Print(e.Result.Text);
        }

        private void sre_SpeechRecognitionRejected(object sender, System.Speech.Recognition.SpeechRecognitionRejectedEventArgs e)
        {
            System.Diagnostics.Debug.Print("Rejected: " + e.Result.Text);
        }

        private void sre_SpeechRecognized(object sender, System.Speech.Recognition.SpeechRecognizedEventArgs e)
        {
            System.Diagnostics.Debug.Print(e.Result.Text);
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {

        }

    }
}
Posted
Updated 19-Aug-10 7:55am
v2

Instead of the following line :

if (TextBox1.Text.Trim.Length == 0)


You should write the following line:

if (TextBox1.Text.Trim().Length == 0)


Because, Trim() is a method of the String class, rather than a property :)
 
Share this answer
 
Comments
Katkot 19-Aug-10 12:48pm    
Thank you sooooo much !! : )
well, Build succeeded
but i dunno .. i think there's a little problem too S:
in this line

sre.SetInputToWaveFile(TextBox1.Text);
Al-Farooque Shubho 19-Aug-10 12:56pm    
Well, first of all you should use

sre.SetInputToWaveFile(TextBox1.Text.Trim());

Then, you should debug the SetInputToWaveFile() method where ever it is (May be inside a class library)
Katkot 19-Aug-10 14:04pm    
Thank you so much for replaying and helping me :) i did what u said up there, it doesn't show errors or any warnings .

I'm setting here 8 hours to do a SPEECH to Text Code, and i don't know what's is happening with my exist code.
it's even can't read a wave file and type it in a textbox !

What can i do now
I added to the code, after the line of "Refresh(); "

DialogResult dr = default(DialogResult);
                dr = OpenFileDialog1.ShowDialog();
                if (dr == System.Windows.Forms.DialogResult.OK)
                {
                    if (!OpenFileDialog1.FileName.Contains("C:\\Documents and Settings\\Kathy\\Desktop\\SpeechToTextTest\\SpeechToTextTest\\OpenFileDialog1.wav"))
                    {
                        MessageBox.Show("Incorrect file");
                    }
                    else
                    {
                        TextBox1.Text = OpenFileDialog1.FileName;
                    }
                }




and still got error message in appearing the window "Incorrect File"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900