2013-03-22 73 views
0

我遇到計數問題ToString();我想讓程序在每次用戶說出其中一個動物名稱後添加一個數字,如1,2和3,從而添加一個數字到將要顯示的lblCount標籤用戶內看到使用語音識別功能將數字添加到標籤

目前計數顯示(比分板)我有這樣的一切

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using SpeechLib; 
using System.IO; 
using System.Speech.Recognition; 
using System.Globalization; 

namespace SimpleSpeechRecognition 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private SpeechRecognitionEngine recognizer; 

     private void Form1_Load(object sender, EventArgs e) 
     { 

      //speechListBox1.Enabled = true; 
      panel1.Enabled = false; 
      panel2.Enabled = false; 

      pictureBox2.Image = Image.FromFile(@"C:\animals\" + "happy.jpg"); 
      lblTitle2.Text = "WELCOME"; 

      pictureBox1.Image = Image.FromFile(@"C:\animals\" + "zoo.jpg"); 
      lblTitle.Text = "WELCOME"; 

      speechListBox1.Items.Add("Dog"); 
      speechListBox1.Items.Add("Elephant"); 
      speechListBox1.Items.Add("Cat"); 
      speechListBox1.Items.Add("Bird"); 
      speechListBox1.Items.Add("Rabbit"); 

      recognizer = new SpeechRecognitionEngine(new CultureInfo("EN-US")); 
      recognizer.SetInputToDefaultAudioDevice(); 

      Choices choices = new Choices("Dog", "Elephant", "Cat", "Bird", "Rabbit"); 
      GrammarBuilder m_GrammarBuilder = new GrammarBuilder(choices); 
      Grammar m_Speech = new Grammar(m_GrammarBuilder); 
      recognizer.LoadGrammar(m_Speech); 

      recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized); 
      recognizer.RecognizeAsync(RecognizeMode.Multiple); 
     } 

     void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e) 
     { 
       // 

      foreach (RecognizedWordUnit word in e.Result.Words) 
      { 
       int count = 0; 
       count += 1; 

       switch (word.Text) 

       { 
        case "Dog": 
         pictureBox1.Image = Image.FromFile(@"C:\animals\" + "dog.jpg"); 
         this.pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage; 
         lblTitle.TextAlign = ContentAlignment.MiddleCenter; 
         lblTitle.Text = "DOG"; 
         lblCount.Text = count.ToString(); 
         break; 
        case "Elephant": 
         pictureBox1.Image = Image.FromFile(@"C:\animals\" + "elephant.jpg"); 
         this.pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage; 
         lblTitle.TextAlign = ContentAlignment.MiddleCenter; 
         lblTitle.Text = "ELEPHANT"; 
         lblCount.Text = count.ToString(); 
         break; 
        case "Cat": 
         pictureBox1.Image = Image.FromFile(@"C:\animals\" + "cat.jpg"); 
         this.pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage; 
         lblTitle.TextAlign = ContentAlignment.MiddleCenter; 
         lblTitle.Text = "CAT"; 
         lblCount.Text = count.ToString(); 
         break; 
        case "Bird": 
         pictureBox1.Image = Image.FromFile(@"C:\animals\" + "bird.jpg"); 
         this.pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage; 
         lblTitle.TextAlign = ContentAlignment.MiddleCenter; 
         lblTitle.Text = "BIRD"; 
         lblCount.Text = count.ToString(); 
         break; 
        case "Rabbit": 
         pictureBox1.Image = Image.FromFile(@"C:\animals\" + "rabbit.jpg"); 
         this.pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage; 
         lblTitle.TextAlign = ContentAlignment.MiddleCenter; 
         lblTitle.Text = "RABBIT"; 
         lblCount.Text = count.ToString(); 
         break; 
       } 
      } 
     } 

     private void SayPhrase(string PhraseToSay) 
     { 

      SpeechVoiceSpeakFlags SpFlags = new SpeechVoiceSpeakFlags(); 
      SpVoice Voice = new SpVoice(); 
      Voice.Speak(PhraseToSay, SpFlags); 

     } 

     private void speechListBox1_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      SayPhrase(speechListBox1.SelectedItems[0].ToString()); 
      pictureBox1.Refresh(); 
     } 

     private void btnExit_Click(object sender, EventArgs e) 
     { 
      Application.Exit(); 
     } 

     private void radioButton1_CheckedChanged(object sender, EventArgs e) 
     { 
      panel1.Enabled = true; 
      panel2.Enabled = false; 
      speechListBox1.Enabled = true; 
      pictureBox1.Enabled = true; 
     } 

     private void radioButton2_CheckedChanged(object sender, EventArgs e) 
     { 
      panel1.Enabled = false; 
      panel2.Enabled = true; 
      speechListBox1.Enabled = false; 
      pictureBox1.Enabled = false; 
     } 
    } 
} 

這是我工作的一部分這個標籤

void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e) 
     { 

      foreach (RecognizedWordUnit word in e.Result.Words) 
      { 
       int count = 0; 
       count += 1; 

       switch (word.Text) 

       { 
        case "Dog": 
         pictureBox1.Image = Image.FromFile(@"C:\animals\" + "dog.jpg"); 
         this.pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage; 
         lblTitle.TextAlign = ContentAlignment.MiddleCenter; 
         lblTitle.Text = "DOG"; 
         lblCount.Text = count.ToString(); 
         break; 
        case "Elephant": 
         pictureBox1.Image = Image.FromFile(@"C:\animals\" + "elephant.jpg"); 
         this.pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage; 
         lblTitle.TextAlign = ContentAlignment.MiddleCenter; 
         lblTitle.Text = "ELEPHANT"; 
         lblCount.Text = count.ToString(); 
         break; 
        case "Cat": 
         pictureBox1.Image = Image.FromFile(@"C:\animals\" + "cat.jpg"); 
         this.pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage; 
         lblTitle.TextAlign = ContentAlignment.MiddleCenter; 
         lblTitle.Text = "CAT"; 
         lblCount.Text = count.ToString(); 
         break; 
        case "Bird": 
         pictureBox1.Image = Image.FromFile(@"C:\animals\" + "bird.jpg"); 
         this.pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage; 
         lblTitle.TextAlign = ContentAlignment.MiddleCenter; 
         lblTitle.Text = "BIRD"; 
         lblCount.Text = count.ToString(); 
         break; 
        case "Rabbit": 
         pictureBox1.Image = Image.FromFile(@"C:\animals\" + "rabbit.jpg"); 
         this.pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage; 
         lblTitle.TextAlign = ContentAlignment.MiddleCenter; 
         lblTitle.Text = "RABBIT"; 
         lblCount.Text = count.ToString(); 
         break; 
       } 
      } 
     } 

回答

2

我不知道我理解你的問題完全是,但我看這似乎是一個相當blatent錯誤:

 foreach (RecognizedWordUnit word in e.Result.Words) 
     { 
      int count = 0; 
      count += 1; 

這每一次循環復位計數爲0,然後立即加1,所以你的標籤總是會說1.

只需在循環外部移動初始值設定項即可。

 int count = 0; 
     foreach (RecognizedWordUnit word in e.Result.Words) 
     { 
      count += 1; 
+0

是的,這是我遇到的問題,它始終保持爲1 – magi4000 2013-03-22 20:27:49

+1

非常感謝您這解決了我問題^ _^ – magi4000 2013-03-22 20:32:30

+0

很高興成爲第二套眼睛。 – 2013-03-22 20:34:59

0

如果您想在程序的整個生命週期中顯示總計數,則需要將計數變量移動到您的表格的成員。就像這樣:

public partial class Form1 : Form 
{ 
    private int count = 0; 
    // ... 
} 

然後改變你的事件處理程序是這樣的:

void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e) 
    { 

     foreach (RecognizedWordUnit word in e.Result.Words) 
     { 
      count += 1; 
+0

非常感謝您花時間回答我的問題,我現在已經明白了^ _ ^ – magi4000 2013-03-22 20:33:25