Speech Calculator in WPF

NOTE

In this article we create a speech calculator in WPF. When the application runs and we say "one" then the focus goes to button "1" and in the TextBox the digit "1" is shown. The same process goes for all calculator operations in this application.

Step 1

In Visual Studio create a new project (in the file menu) and create a new WPF Project with the name "SpeechCalc".

Step 2

Now we must add references for the "Windows Speech Recognition". For that go to the Solution Explorer and click on "References" -> "Add references". Then we can show this window. Now we select "System.Speech" assemblies from the Framework Section.

System_Speech
 

STEP 3

We create the design of the application using the following XAML Code.

MAINWINDOW.XAML

<Window x:Class="SpeechCalc.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Speech Calculator" Height="500" Width="700" BorderBrush="#7EB900" BorderThickness="5" ShowInTaskbar="False" ResizeMode="NoResize">
    <Grid Background="#354c8c">
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>

            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />

        </Grid.ColumnDefinitions>
        <TextBox Margin="12,12,345,267" Name="textbox1" Background="{x:Null}" AutoWordSelection="True" TextWrapping="Wrap" IsHitTestVisible="False" TextChanged="textbox1_TextChanged_1" Grid.Row="0" Grid.ColumnSpan="6" HorizontalAlignment="Center" Visibility="Collapsed" />
        <TextBox Height="40" Width="450" HorizontalAlignment="Center" Grid.Row="1" Grid.ColumnSpan="6" TextAlignment="Right" VerticalAlignment="Top" Name="txtShow" Foreground="Black" FontFamily="Console" FontSize="20" FontWeight="Bold" BorderBrush="#7EB900" BorderThickness="5"/>
        <Button Name="btnOne" Grid.Row="2" Grid.Column="1" Content="1" GotFocus="btnOne_GotFocus_1"/>
        <Button Name="btnTwo" Grid.Row="2" Grid.Column="2" Content="2" GotFocus="btnTwo_GotFocus_1"/>
        <Button Name="btnThree" Grid.Row="2" Grid.Column="3" Content="3" GotFocus="btnThree_GotFocus_1"/>
        <Button Name="btnPlus" Grid.Row="2" Grid.Column="4" Content="+" GotFocus="btnPlus_GotFocus_1"/>
        <Button Name="btnFour" Grid.Row="3" Grid.Column="1" Content="4" GotFocus="btnFour_GotFocus_1"/>
        <Button Name="btnFive" Grid.Row="3" Grid.Column="2" Content="5" GotFocus="btnFive_GotFocus_1"/>
        <Button Name="btnSix" Grid.Row="3" Grid.Column="3" Content="6" GotFocus="btnSix_GotFocus_1"/>
        <Button Name="btnMinus" Grid.Row="3" Grid.Column="4" Content="-" GotFocus="btnMinus_GotFocus_1"/>

        <Button Name="btnSeven" Grid.Row="4" Grid.Column="1" Content="7" GotFocus="btnSeven_GotFocus_1"/>
        <Button Name="btnEight" Grid.Row="4" Grid.Column="2" Content="8" GotFocus="btnEight_GotFocus_1" />
        <Button Name="btnNine" Grid.Row="4" Grid.Column="3" Content="9"  GotFocus="btnNine_GotFocus_1"/>
        <Button Name="btnMul" Grid.Row="4" Grid.Column="4" Content="*" GotFocus="btnMul_GotFocus_1"/>

        <Button Name="btnDot" Grid.Row="5" Grid.Column="1" Content="." GotFocus="btnDot_GotFocus_1"/>
        <Button Name="btnZero" Grid.Row="5" Grid.Column="2" Content="0" GotFocus="btnZero_GotFocus_1"/>|
        <Button Name="btnEqual" Grid.Row="5" Grid.Column="3" Content="=" GotFocus="btnEqual_GotFocus_1"/>
        <Button Name="btnDiv" Grid.Row="5" Grid.Column="4" Content="/" GotFocus="btnDiv_GotFocus_1"/>

        <Button Name="btnClear" Grid.Row="6" Grid.Column="2" Content="Clear" GotFocus="btnClear_GotFocus_1"/>
        <Button Name="btnDelete" Grid.Row="6" Grid.Column="3" Content="Del" GotFocus="btnDelete_GotFocus_1"/>
        <Label Content="Made By" FontSize="15" Foreground="#007" FontWeight="Bold" Grid.Row="7" Grid.ColumnSpan="4" HorizontalAlignment="Center"/>
        <Label Content="PR@N@y" FontSize="15" Foreground="White" FontWeight="Bold" Grid.Row="7" Grid.ColumnSpan="6" HorizontalAlignment="Center" Margin="10,0,60,20"/>
    </Grid>
</
Window>

Step 4

We add some STYLE for the various controls to the APP.XAML File.

APP.XAML

<Application x:Class="SpeechCalc.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <Style TargetType="RowDefinition">
            <Setter Property="Height" Value="60" />
           
       
</Style>
        <Style TargetType="ColumnDefinition">
            <Setter Property="Width" Value="100"/>
        </Style>

        <Style TargetType="Button">

            <Setter Property="Background" Value="#0086dc"/>
            <Setter Property="Foreground" Value="#007" />
            <Setter Property="Width" Value="60" />
            <Setter Property="Height" Value="50" />
            <Setter Property="FontWeight" Value="Bold"/>
            <Setter Property="BorderBrush" Value="#007"/>
            <Setter Property="BorderThickness" Value="10"/>
            <Setter Property="FontSize" Value="20" />
            <Setter Property="Margin" Value="20,0,20,0" />
        </Style>
    </Application.Resources>
</
Application>

STEP 5

We write the following code in the MAINWINDOW.XAML.CS file. We add the namespace for Speech Recognigation. 

MAINWINDOW.XAML.CS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Speech;
using System.Speech.Recognition;
using System.Speech.Recognition.SrgsGrammar;
using System.Xml;

namespace SpeechCalc
{
   
/// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
       
protected SpeechRecognizer SPVC;
       
public string op;
       
public string value;
       
public string voice;
       
public void InitializeVC()
        {
            SPVC =
new SpeechRecognizer();
            SPVC.Enabled =
true;
            SPVC.SpeechRecognized +=
new EventHandler<SpeechRecognizedEventArgs>(SPVC_SpeechRecognized);
        }
       
public MainWindow()
        {
            InitializeComponent();
            InitializeVC();
            textbox1.Focus();
        }
       
void SPVC_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            textbox1.Text = e.Result.Text;
            voice = e.Result.Text;
        }
       
private void textbox1_TextChanged_1(object sender, TextChangedEventArgs e)
        {
           
if (textbox1.Text.Equals("One"))
            {
                btnOne.Focus();
            }
           
if (textbox1.Text.Equals("Clear") || textbox1.Text.Equals("C"))
            {
                btnClear.Focus();
            }
           
if(textbox1.Text.Equals("Space"))
            {
               btnDelete.Focus();
            }
        }
 
       
private void btnOne_GotFocus_1(object sender, RoutedEventArgs e)
        {
            txtShow.Text +=
"1";
        }

        private void btnTwo_GotFocus_1(object sender, RoutedEventArgs e)
        {
            txtShow.Text +=
"2";
        }

        private void btnThree_GotFocus_1(object sender, RoutedEventArgs e)
        {
            txtShow.Text +=
"3";
        }

        private void btnPlus_GotFocus_1(object sender, RoutedEventArgs e)
        {
            value = txtShow.Text;
            txtShow.Text =
"";
            op =
"+";
        }

        private void btnFour_GotFocus_1(object sender, RoutedEventArgs e)
        {
            txtShow.Text +=
"4";
        }
 
       
private void btnFive_GotFocus_1(object sender, RoutedEventArgs e)
        {
            txtShow.Text +=
"5";
        }

        private void btnSix_GotFocus_1(object sender, RoutedEventArgs e)
        {
            txtShow.Text +=
"6";
        }

        private void btnMinus_GotFocus_1(object sender, RoutedEventArgs e)
        {
            value = txtShow.Text;
            txtShow.Text =
"";
            op =
"-";
        }

        private void btnSeven_GotFocus_1(object sender, RoutedEventArgs e)
        {
            txtShow.Text +=
"7";
        }

        private void btnEight_GotFocus_1(object sender, RoutedEventArgs e)
        {
            txtShow.Text +=
"8";
        }

        private void btnNine_GotFocus_1(object sender, RoutedEventArgs e)
        {
            txtShow.Text +=
"9";
        }

        private void btnMul_GotFocus_1(object sender, RoutedEventArgs e)
        {
            value = txtShow.Text;
            txtShow.Text =
"";
            op =
"*";
        }

        private void btnDot_GotFocus_1(object sender, RoutedEventArgs e)
        {
            txtShow.Text +=
".";
        }

        private void btnZero_GotFocus_1(object sender, RoutedEventArgs e)
        {
            txtShow.Text +=
"0";
        }

        private void btnEqual_GotFocus_1(object sender, RoutedEventArgs e)
        {
           
switch (op)
            {
               
case "+":
                    txtShow.Text =
Convert.ToString(Convert.ToDecimal(value.ToString()) + Convert.ToDecimal(txtShow.Text));
                   
break;
               
case "-":
                    txtShow.Text =
Convert.ToString(Convert.ToDecimal(value.ToString()) - Convert.ToDecimal(txtShow.Text));
                   
break;
               
case "/":
                    txtShow.Text =
Convert.ToString(Convert.ToDecimal(value.ToString()) / Convert.ToDecimal(txtShow.Text));
                   
break;
               
case "*":
                    txtShow.Text =
Convert.ToString(Convert.ToDecimal(value.ToString()) * Convert.ToDecimal(txtShow.Text));
                   
break;
               
default:
                   
MessageBox.Show("Not a Valid Choice");
                   
break;
            }
        }

        private void btnDiv_GotFocus_1(object sender, RoutedEventArgs e)
        {
            value = txtShow.Text;
            txtShow.Text =
"";
            op =
"/";
        }

        private void btnClear_GotFocus_1(object sender, RoutedEventArgs e)
        {
            txtShow.Text =
"";
        }

        private void btnDelete_GotFocus_1(object sender, RoutedEventArgs e)
        {
           
Int32 l = Convert.ToInt32(txtShow.Text.Length) - 1;
           
if (txtShow.Text == "")
            {

            }
           
else        {
                txtShow.Text = txtShow.Text.Substring(0, l);
               
if (l < 1)
                {
                    txtShow.Text =
"";
                                 }
            }        }    }    }

OUTPUT

screen

Then we say for examle "one plus two" and then it gives the answer "three(3)" in the TextBox.

I hope this article help you.

Thank you
J :) :)