Add System.Speech as a reference. Right click on references, choose add a reference. Then click on Net, then find System.Speech.
The program in this example is named CalcualtorDict
---------- Form1.Designer.cs ----------
namespace CalculatorDict
{
partial class Form1
{
///
/// Required designer variable.
///
private System.ComponentModel.IContainer components = null;
///
/// Clean up any resources being used.
///
/// true if managed resources should be disposed; otherwise, false.
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.num1Label = new System.Windows.Forms.Label();
this.signLabel = new System.Windows.Forms.Label();
this.num2Label = new System.Windows.Forms.Label();
this.panel1 = new System.Windows.Forms.Panel();
this.equalLabel = new System.Windows.Forms.Label();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// num1Label
//
this.num1Label.AutoSize = true;
this.num1Label.Font = new System.Drawing.Font("Microsoft Sans Serif", 20.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.num1Label.Location = new System.Drawing.Point(194, 95);
this.num1Label.Name = "num1Label";
this.num1Label.Size = new System.Drawing.Size(0, 31);
this.num1Label.TabIndex = 0;
//
// signLabel
//
this.signLabel.AutoSize = true;
this.signLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 20.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.signLabel.Location = new System.Drawing.Point(103, 133);
this.signLabel.Name = "signLabel";
this.signLabel.Size = new System.Drawing.Size(86, 31);
this.signLabel.TabIndex = 1;
this.signLabel.Text = "label1";
//
// num2Label
//
this.num2Label.AutoSize = true;
this.num2Label.Font = new System.Drawing.Font("Microsoft Sans Serif", 20.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.num2Label.Location = new System.Drawing.Point(197, 176);
this.num2Label.Name = "num2Label";
this.num2Label.Size = new System.Drawing.Size(0, 31);
this.num2Label.TabIndex = 2;
//
// panel1
//
this.panel1.BackColor = System.Drawing.Color.Black;
this.panel1.Location = new System.Drawing.Point(109, 211);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(228, 11);
this.panel1.TabIndex = 3;
//
// equalLabel
//
this.equalLabel.AutoSize = true;
this.equalLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.equalLabel.Location = new System.Drawing.Point(172, 242);
this.equalLabel.Name = "equalLabel";
this.equalLabel.Size = new System.Drawing.Size(0, 29);
this.equalLabel.TabIndex = 4;
//
// comboBox1
//
this.comboBox1.FormattingEnabled = true;
this.comboBox1.Location = new System.Drawing.Point(297, 13);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(160, 21);
this.comboBox1.TabIndex = 5;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.White;
this.ClientSize = new System.Drawing.Size(487, 302);
this.Controls.Add(this.comboBox1);
this.Controls.Add(this.equalLabel);
this.Controls.Add(this.panel1);
this.Controls.Add(this.num2Label);
this.Controls.Add(this.signLabel);
this.Controls.Add(this.num1Label);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "Form1";
this.Text = "CalculatorDict";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label num1Label;
private System.Windows.Forms.Label signLabel;
private System.Windows.Forms.Label num2Label;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Label equalLabel;
private System.Windows.Forms.ComboBox comboBox1;
}
}
-------------------- Form1.cs ---------------
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 System.Speech.Recognition;
using System.Speech.Synthesis;
namespace CalculatorDict
{
public partial class Form1 : Form
{
private SpeechRecognizer rec = new SpeechRecognizer();
private String recordedText = "";
private String num1, num2;
private SpeechSynthesizer reader = new SpeechSynthesizer();
public Form1()
{
InitializeComponent();
rec.SpeechRecognized += rec_SpeechRecognized;
}
private void goDefault()
{
num1 = null; num1Label.Text = null;
num2 = null; num2Label.Text = null;
signLabel.Text = null; equalLabel.Text = null; recordedText = "";
}
private void Form1_Load(object sender, EventArgs e)
{
goDefault();
var c = new Choices();
for (var i = 0; i <= 10; i++)
c.Add(i.ToString());
c.Add("plus"); c.Add("add");
c.Add("subtract"); c.Add("minus");
c.Add("divide");
c.Add("multiply"); c.Add("times");
c.Add("equal"); c.Add("result");
c.Add("start"); c.Add("again");
var gb = new GrammarBuilder(c);
var g = new Grammar(gb);
rec.LoadGrammar(g);
rec.Enabled = true;
}
private bool check(String value)
{
switch (value.ToLower())
{
case "plus":
case "add":
signLabel.Text = "+"; return true;
case "subtract":
case "minus":
signLabel.Text = "-";
return true;
case "divide":
signLabel.Text = ":";
return true;
case "multiply":
case "times":
signLabel.Text = "X";
return true;
default:
break;
}
return false;
}
private int calculate(int n, int nn)
{
switch (signLabel.Text.ToLower())
{
case "+":
return n + nn;
case "-":
return n - nn;
case "x":
return n * nn;
case ":":
return n / nn;
default:
break;
}
return -1;
}
void rec_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
String value=comboBox1.Text = e.Result.Text;
bool num = false;
for (var i = 0; i <= 10; i++)
{
if (value.Equals(i.ToString()))
{
recordedText += i; num = true; break;
}
}
if (!num && num1==null && recordedText.Trim().Length>0) {
if (check(value)) { num1Label.Text=num1 = recordedText; recordedText = ""; }
}
if ( (value.ToLower().Equals("equal") || value.ToLower().Equals("result") ) && num1!=null && signLabel.Text!=null && recordedText.Trim().Length>0)
{
num1 = null;
num2Label.Text=num2 = recordedText; recordedText = "";
int n = Convert.ToInt32(num1);
int nn = Convert.ToInt32(num2);
int result=calculate(n, nn);
equalLabel.Text = "" + result;
reader.SpeakAsync(equalLabel.Text);
}
if(value.ToLower().Equals("again") )
{
goDefault();
}
}
}
}
=================
It took time to write this app. To show support to the author and to keep this site free and running, please donate or do one of the following. Thank you.
- Visit irap123.com many times a day
- Join irap123.com
- Buy my app irap123 (rhyming dictionary) available for android, and iphone. On windows phone, it is called iLyrics. If you not you can get the desktop version at www.irap123.com/apps/webstore/
- Include irap123.com in your blogs, forums, facebook walls, email footers, comments
- send a donation at my paypal address at cbdsigned.vevo@gmail.com