Voice Calculator in WPF

In this article, we will create a voice calculator in WPF. So when we say "One" the focus will move to the Button One and the digit "1" will be shown in the TextBox. This will help for the basic calculation of a calculator, using speech recognition. Now we create a Basic Voice calculator. For this use the following procedure.

Step 1

First we click on the New Project and add a WPF Application (Voice calculator).

Step 2

First we add a reference for the "Speech Recognition". To do that, first click on "Project" and then "Add Reference", the following window will be shown. Here we can select the "System.Speech" Component like this:

WPF1.jpg

Now we write the following NameSpaces in the .cs file:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

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;

Step 3

After that we write the following code:

protected SpeechRecognizer SPVC;

The Speechrecognizer Class is used to listen and fetch the Spoken Text from the system and convert it into text, or we can use these as Text Commands like we use in our program.

public string value;

        public string op;

        public string voice;

        protected SpeechRecognizer SPVC;

        public Window1()

        {

            InitializeComponent();

            InitializeVC();

            textBox1.Focus();

        }

 

        private void InitializeVC()

        {

            SPVC = new SpeechRecognizer();

            SPVC.Enabled = true;

            SPVC.SpeechRecognized +=

                new EventHandler<SpeechRecognizedEventArgs>(SPVC_SpeechRecognized);

        }

        void SPVC_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)

        {

            textBox1.Text = e.Result.Text;

            voice = e.Result.Text;

        }


Here we take a TextBox (textBox1) to fetch the spoken data. In this program, we set its visibility to collapsed like this:

<TextBox Margin="12,12,345,267" Name="textBox1" Background="{x:Null}" AutoWordSelection="True" TextWrapping="Wrap" IsHitTestVisible="False" TextChanged="textBox1_TextChanged" Grid.ColumnSpan="2" Visibility="Collapsed" />

Step 4

Now we create Buttons for digits and Operators and a TextBox to show the result like this:
 

        <TextBox Height="23" HorizontalAlignment="Left" Margin="21,38,0,0" Name="txtShow" VerticalAlignment="Top" Width="165" TextAlignment="Right" Grid.ColumnSpan="2" />

        <Button Content="1" Height="23" HorizontalAlignment="Left" Margin="20,86,0,0" Name="btnOne" VerticalAlignment="Top" Width="35" GotFocus="btnOne_GotFocus" />

        <Button Content="3" HorizontalAlignment="Left" Margin="50,86,0,180" Name="btnThree" Width="35" GotFocus="btnThree_GotFocus" Grid.Column="1" />

        <Button Content="+" Height="23" HorizontalAlignment="Right" Margin="0,144,253,0" Name="btnPlus" VerticalAlignment="Top" Width="35" GotFocus="btnPlus_GotFocus" Grid.Column="1" />

        <Button Content="=" Height="23" HorizontalAlignment="Left" Margin="52,173,0,0" Name="btnEqual" VerticalAlignment="Top" Width="35" GotFocus="btnEqual_GotFocus" Grid.Column="1" />

        <Button Content="2" Height="23" HorizontalAlignment="Left" Margin="4,86,0,0" Name="btnTwo" VerticalAlignment="Top" Width="35" GotFocus="btnTwo_GotFocus" Grid.Column="1" />

        <Button Content="4" HorizontalAlignment="Left" Margin="19,115,0,151" Name="btnFour" Width="35" GotFocus="btnFour_GotFocus" />

        <Button Content="5" Height="23" HorizontalAlignment="Left" Margin="4,115,0,0" Name="btnfive" VerticalAlignment="Top" Width="35" GotFocus="btnfive_GotFocus" Grid.Column="1" />

        <Button Content="6" Height="23" HorizontalAlignment="Left" Margin="50,115,0,0" Name="btnSix" VerticalAlignment="Top" Width="35" GotFocus="btnSix_GotFocus" Grid.Column="1" />

        <Button Content="7" Height="23" HorizontalAlignment="Left" Margin="21,144,0,0" Name="btnSeven" VerticalAlignment="Top" Width="35" GotFocus="btnSeven_GotFocus" />

        <Button Content="8" Height="23" HorizontalAlignment="Right" Margin="0,144,343,0" Name="btnEight" VerticalAlignment="Top" Width="35" GotFocus="btnEight_GotFocus" Grid.Column="1" />

        <Button Content="9" Height="23" HorizontalAlignment="Left" Margin="50,144,0,0" Name="btnNine" VerticalAlignment="Top" Width="35" GotFocus="btnNine_GotFocus" Grid.Column="1" />

        <Button Content="0" Height="23" HorizontalAlignment="Left" Margin="9,0,0,93" Name="btnZero" VerticalAlignment="Bottom" Width="35" GotFocus="btnZero_GotFocus" Grid.Column="1" />

        <Button Content="-" Height="23" HorizontalAlignment="Right" Margin="0,115,254,0" Name="btnMinus" VerticalAlignment="Top" Width="35" GotFocus="btnMinus_GotFocus" Grid.Column="1" />

        <Button Content="*" Height="23" HorizontalAlignment="Right" Margin="0,86,256,0" Name="btnMul" VerticalAlignment="Top" Width="35" GotFocus="btnMul_GotFocus" Grid.Column="1" />

        <Button Content="/" Height="23" HorizontalAlignment="Right" Margin="0,174,254,0" Name="btnDiv" VerticalAlignment="Top" Width="35" GotFocus="btnDiv_GotFocus" Grid.Column="1" />

        <Button Content="." Height="23" HorizontalAlignment="Left" Margin="22,173,0,0" Name="btnDot" VerticalAlignment="Top" Width="35" GotFocus="btnDot_GotFocus" />

        <Button Content="Clear" Height="23" HorizontalAlignment="Left" Margin="47,204,0,0" Name="btnClear" VerticalAlignment="Top" Width="36" GotFocus="btnClear_GotFocus" FontSize="12" FontWeight="Normal" HorizontalContentAlignment="Right" Grid.ColumnSpan="2" />

        <Button Content="delete" FontSize="12" FontWeight="Normal" Height="23" HorizontalAlignment="Left" HorizontalContentAlignment="Right" Margin="48,204,0,0" Name="btnDelete" VerticalAlignment="Top" Width="39" GotFocus="btnDelete_GotFocus" Grid.Column="1" />


The Output will be:

WPF2.jpg

Step 5

Now we write the following code in the .cs file:

     private void textBox1_TextChanged(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(object sender, RoutedEventArgs e)

        {

            txtShow.Text += "1";

        }

 

        private void btnPlus_GotFocus(object sender, RoutedEventArgs e)

        {

            value = txtShow.Text;

            txtShow.Text = "";

            op = "+";

        }

 

        private void btnThree_GotFocus(object sender, RoutedEventArgs e)

        {

            txtShow.Text += "3";

        }

 

        private void btnEqual_GotFocus(object sender, RoutedEventArgs e)

        {

            if (op == "+")

            {

                txtShow.Text = Convert.ToString(Convert.ToDecimal(value.ToString()) + Convert.ToDecimal(txtShow.Text));

            }

            if (op == "-")

            {

                txtShow.Text = Convert.ToString(Convert.ToDecimal(value.ToString()) - Convert.ToDecimal(txtShow.Text));

            }

            if (op == "*")

            {

                txtShow.Text = Convert.ToString(Convert.ToDecimal(value.ToString()) * Convert.ToDecimal(txtShow.Text));

            }

            if (op == "/")

            {

                txtShow.Text = Convert.ToString(Convert.ToDecimal(value.ToString()) / Convert.ToDecimal(txtShow.Text));

            }

        }

 

        private void btnTwo_GotFocus(object sender, RoutedEventArgs e)

        {

            txtShow.Text += "2";

        }

 

        private void btnFour_GotFocus(object sender, RoutedEventArgs e)

        {

            txtShow.Text += "4";

        }

 

        private void btnfive_GotFocus(object sender, RoutedEventArgs e)

        {

            txtShow.Text += "5";

        }

 

        private void btnSix_GotFocus(object sender, RoutedEventArgs e)

        {

            txtShow.Text += "6";

        }

 

        private void btnSeven_GotFocus(object sender, RoutedEventArgs e)

        {

            txtShow.Text += "7";

        }

 

        private void btnEight_GotFocus(object sender, RoutedEventArgs e)

        {

            txtShow.Text += "8";

        }

 

        private void btnNine_GotFocus(object sender, RoutedEventArgs e)

        {

            txtShow.Text += "9";

        }

 

        private void btnZero_GotFocus(object sender, RoutedEventArgs e)

        {

            txtShow.Text += "0";

        }

 

        private void btnMinus_GotFocus(object sender, RoutedEventArgs e)

        {

            value = txtShow.Text;

            txtShow.Text = "";

            op = "-";

        }

 

        private void btnMul_GotFocus(object sender, RoutedEventArgs e)

        {

            value = txtShow.Text;

            txtShow.Text = "";

            op = "*";

        }

 

        private void btnDiv_GotFocus(object sender, RoutedEventArgs e)

        {

            value = txtShow.Text;

            txtShow.Text = "";

            op = "/";

        }

 

        private void btnDot_GotFocus(object sender, RoutedEventArgs e)

        {

            txtShow.Text += ".";

        }

 

        private void btnClear_GotFocus(object sender, RoutedEventArgs e)

        {

            txtShow.Text = "";

        }

 

        private void btnDelete_GotFocus(object sender, RoutedEventArgs e)

        {

            //txtShow.Text = "";

            Int32 l = Convert.ToInt32(txtShow.Text.Length) - 1;

            if (txtShow.Text == "")

            {

            }

            else

            {

                txtShow.Text = txtShow.Text.Substring(0, l);

                if (l < 1)

                {

                    txtShow.Text = "";

                    txtShow.Text = "";

                }

            }

        }

So when we run the program:

WPF3.jpg

So when we say "one plus three equals" the TextBox will show 4.

Like when we say "four divided by two equals" the result is 2, which will be shown in the TextBox.