Reader Level:
ARTICLE

Create a Simple Keypad in Windows Phone 7

Posted by Mahak Gupta Articles | Windows Phone October 10, 2012
In this example we create a keypad in Windows Phone 7 with the help of Button Controls.
  • 0
  • 0
  • 3281
Download Files:
 

In this example we create a keypad in Windows Phone 7 with the help of Button Controls. For this use the following instructions.

Step 1

First we click on File -> New Project. And then the following window will be shown, here we select the following:

Windows-phone-Game.jpg

Step 2

Now we create a TextBox (which is used to show the message which will be typed by the keypad) and multiple Button Controls (to type the text in the TextBox).

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="14,11,0,-11">

            <TextBox Height="72" HorizontalAlignment="Right" Margin="0,26,35,0" Name="txtMessage" Text="" VerticalAlignment="Top" Width="421" HorizontalContentAlignment="Right" TextAlignment="Right" />

            <Button Content="q" Height="72" HorizontalAlignment="Left" Margin="-2,121,0,0" Name="btnq" VerticalAlignment="Top" Width="77" Click="btnq_Click" />

            <Button Content="i" Height="72" HorizontalAlignment="Left" Margin="370,121,0,0" Name="btni" VerticalAlignment="Top" Width="84" Click="btni_Click" />

            <Button Content="u" Height="72" Margin="321,121,79,0" Name="btnu" VerticalAlignment="Top" Click="btnu_Click" />

            <Button Content="y" Height="72" HorizontalAlignment="Left" Margin="267,121,0,0" Name="btny" VerticalAlignment="Top" Width="72" Click="btny_Click" />

            <Button Content="t" Height="72" HorizontalAlignment="Left" Margin="217,121,0,0" Name="btnt" VerticalAlignment="Top" Width="64" Click="btnt_Click" />

            <Button Content="r" Height="72" HorizontalAlignment="Left" Margin="163,121,0,0" Name="btnr" VerticalAlignment="Top" Width="64" Click="btnr_Click" />

            <Button Content="e" Height="72" Margin="114,121,0,0" Name="btne" VerticalAlignment="Top" Click="btne_Click" HorizontalAlignment="Left" Width="64" />

            <Button Content="w" Height="72" Margin="53,121,0,0" Name="btnw" VerticalAlignment="Top" Click="btnw_Click" HorizontalAlignment="Left" Width="80" />

            <Button Height="72" HorizontalAlignment="Left" Margin="357,355,0,0" Name="btnClear" VerticalAlignment="Top" Width="79" Click="btnClear_Click">

                <Button.Background>

                    <ImageBrush ImageSource="/SMSInWP7;component/Images/btndelete.png" />

                </Button.Background>

            </Button>

            <Button Content="m" Height="72" HorizontalAlignment="Right" Margin="0,355,94,0" Name="btnm" VerticalAlignment="Top" Width="85" Click="btnm_Click" />

            <Button Content="n" Height="72" Margin="227,355,164,0" Name="btnn" VerticalAlignment="Top" Click="btnn_Click" />

            <Button Content="b" Height="72" Margin="158,355,232,0" Name="btnb" VerticalAlignment="Top" Click="btnb_Click" />

            <Button Content="v" Height="72" HorizontalAlignment="Left" Margin="90,355,0,0" Name="btnv" VerticalAlignment="Top" Width="70" Click="btnv_Click" />

<Button Height="72" HorizontalAlignment="Left" Margin="6,355,0,0" Name="btnCase" VerticalAlignment="Top" Width="76" Click="btnCase_Click">

                    <ImageBrush ImageSource="/SMSInWP7;component/Images/uparrow1.png" />

                </Button.Background>

            </Button>

            <Button Content="&amp;123" Height="72" HorizontalAlignment="Left" Margin="25,433,0,0" Name="btndigit" VerticalAlignment="Top" Width="107" Click="btndigit_Click" />

            <Button Content="c" Height="72" HorizontalAlignment="Left" Margin="392,277,0,0" Name="btnc" VerticalAlignment="Top" Width="64" Click="btnc_Click" />

            <Button Content="x" Height="72" HorizontalAlignment="Left" Margin="337,277,0,0" Name="btnx" VerticalAlignment="Top" Width="64" Click="btnx_Click" />

            <Button Content="z" Height="72" HorizontalAlignment="Left" Margin="275,277,0,0" Name="btnz" VerticalAlignment="Top" Width="64" Click="btnz_Click" />

            <Button Content="l" Height="72" HorizontalAlignment="Left" Margin="217,277,0,0" Name="btnl" VerticalAlignment="Top" Width="64" Click="btnl_Click" />

            <Button Content="k" Height="72" HorizontalAlignment="Left" Margin="161,277,0,0" Name="btnk" VerticalAlignment="Top" Width="64" Click="btnk_Click" />

            <Button Content="j" Height="72" HorizontalAlignment="Left" Margin="106,277,0,0" Name="btnj" VerticalAlignment="Top" Width="64" Click="btnj_Click" />

            <Button Content="h" Height="72" HorizontalAlignment="Left" Margin="54,277,0,0" Name="btnh" VerticalAlignment="Top" Width="64" Click="btnh_Click" />

            <Button Content="g" Height="72" HorizontalAlignment="Left" Margin="6,277,0,0" Name="btng" VerticalAlignment="Top" Width="64" Click="btng_Click" />

            <Button Content="." Height="72" HorizontalAlignment="Left" Margin="287,433,0,0" Name="btndot" VerticalAlignment="Top" Width="64" Click="btndot_Click" />

            <Button Content="d" Height="72" HorizontalAlignment="Left" Margin="286,199,0,0" Name="btnd" VerticalAlignment="Top" Width="68" Click="btnd_Click" />

            <Button Content="s" Height="72" HorizontalAlignment="Left" Margin="223,199,0,0" Name="btns" VerticalAlignment="Top" Width="79" Click="btns_Click" />

            <Button Content="a" Height="72" HorizontalAlignment="Left" Margin="161,199,0,0" Name="btna" VerticalAlignment="Top" Width="77" Click="btna_Click" />

            <Button Content="p" Height="72" HorizontalAlignment="Left" Margin="110,199,0,0" Name="btnp" VerticalAlignment="Top" Width="64" Click="btnp_Click" />

            <Button Content="o" Height="72" HorizontalAlignment="Left" Margin="52,199,0,0" Name="btno" VerticalAlignment="Top" Width="73" Click="btno_Click" />

            <Button Content="space" Height="72" HorizontalAlignment="Left" Margin="179,433,0,0" Name="btnspace" VerticalAlignment="Top" Width="117" Click="btnspace_Click" />

            <Button Content="," Height="72" HorizontalAlignment="Left" Margin="122,433,0,0" Name="btnComma" VerticalAlignment="Top" Width="64" Click="btnComma_Click" />

            <Button Content="@" Height="72" HorizontalAlignment="Right" Margin="0,433,45,0" Name="btnatthareteof" VerticalAlignment="Top" Width="78" Click="btnatthareteof_Click" />

            <Button Content="f" Height="72" HorizontalAlignment="Right" Margin="0,199,46,0" Name="btnf" VerticalAlignment="Top" Width="83" Click="btnf_Click" />

        </Grid>

Here we use two Images, one for the ChangeCase Button and one for the Clear Text Button.

The output will be:

Output.jpg

Step 3

Now we will write the code for the Change Case Button; it is:

public int a = 0, b = 0;

 

private void btnCase_Click(object sender, RoutedEventArgs e)

{

    if (a == 0)

    {

        a = 1;

        b = 0;

        btnq.Content = "Q";

        btnw.Content = "W";

        btne.Content = "E";

        btnr.Content = "R";

        btnt.Content = "T";

        btny.Content = "Y";

        btnu.Content = "U";

        btni.Content = "I";

        btno.Content = "O";

        btnp.Content = "P";

        btna.Content = "A";

        btns.Content = "S";

        btnd.Content = "D";

        btnf.Content = "F";

        btng.Content = "G";

        btnh.Content = "J";

        btnj.Content = "J";

        btnk.Content = "K";

        btnl.Content = "L";

        btnz.Content = "Z";

        btnx.Content = "X";

        btnc.Content = "C";

        btnv.Content = "V";

        btnb.Content = "B";

        btnn.Content = "N";

        btnm.Content = "M";

    }

    else

    {

        a = 0;

        btnq.Content = "q";

        btnw.Content = "w";

        btne.Content = "e";

        btnr.Content = "r";

        btnt.Content = "t";

        btny.Content = "y";

        btnu.Content = "u";

        btni.Content = "i";

        btno.Content = "o";

        btnp.Content = "p";

        btna.Content = "a";

        btns.Content = "s";

        btnd.Content = "d";

        btnf.Content = "f";

        btng.Content = "g";

        btnh.Content = "h";

        btnj.Content = "j";

        btnk.Content = "k";

        btnl.Content = "l";

        btnz.Content = "z";

        btnx.Content = "x";

        btnc.Content = "c";

        btnv.Content = "v";

        btnb.Content = "b";

        btnn.Content = "n";

        btnm.Content = "m";

    }

}

After writing the code when we run the program and click on the Button the Keypad will look like this:

Output1.jpg

This keypad will be useful when we want to write the UpperCase Letter.

Step 4

Now we write the code for the Digit or special characters Button; see:

Output2.jpg
 

private void btndigit_Click(object sender, RoutedEventArgs e)

 {

     if (b == 0)

     {

         b = 1;

         a = 0;

         btnq.Content = "1";

         btnw.Content = "2";

         btne.Content = "3";

         btnr.Content = "4";

         btnt.Content = "5";

         btny.Content = "6";

         btnu.Content = "7";

         btni.Content = "8";

         btno.Content = "9";

         btnp.Content = "0";

         btna.Content = "!";

         btns.Content = "#";

         btnd.Content = "$";

         btnf.Content = "%";

         btng.Content = "^";

         btnh.Content = "&";

         btnj.Content = "(";

         btnk.Content = ")";

         btnl.Content = "-";

         btnz.Content = "*";

         btnx.Content = "`";

         btnc.Content = ":";

         btnv.Content = ";";

         btnb.Content = "?";

         btnn.Content = "~";

         btnm.Content = "/";

     }

     else

     {

         b = 0;

         btnq.Content = "q";

         btnw.Content = "w";

         btne.Content = "e";

         btnr.Content = "r";

         btnt.Content = "t";

         btny.Content = "y";

         btnu.Content = "u";

         btni.Content = "i";

         btno.Content = "o";

         btnp.Content = "p";

         btna.Content = "a";

         btns.Content = "s";

         btnd.Content = "d";

         btnf.Content = "f";

         btng.Content = "g";

         btnh.Content = "h";

         btnj.Content = "j";

         btnk.Content = "k";

         btnl.Content = "l";

         btnz.Content = "z";

         btnx.Content = "x";

         btnc.Content = "c";

        btnv.Content = "v";

        btnb.Content = "b";

        btnn.Content = "n";

        btnm.Content = "m";

    }

}


The output will be:

Output3.jpg

Step 5

Now we will write the code for typing the LowerCase Letter, UpperCase Letter and Digits and Special Characters in the Textbox. Here we have an example of this:

public partial class VisualWebPart1 {

 

    private void btnq_Click(object sender, RoutedEventArgs e)

    {

 

        if (a == 1)

        {

            txtMessage.Text = txtMessage.Text + "Q";

        }

        if (b == 1 && a == 0)

        {

            txtMessage.Text = txtMessage.Text + "1";

        }

        if (a == 0 && b == 0)

        {

            txtMessage.Text = txtMessage.Text + "q";

        }

    }


In this example first we check the value (a=1) which we will set on the Change Case Button. After that we will check the value of (b) which can also set by us in the Digit or Special Characters Button.

So the output will be:

Output4.jpg

Output5.jpg


Now we will write the code for the other Buttons; it is:

        private void btnw_Click(object sender, RoutedEventArgs e)

        {

            if (a == 1)

            {

                txtMessage.Text = txtMessage.Text + "W";

            }

            if (b == 1 && a == 0)

            {

                txtMessage.Text = txtMessage.Text + "2";

            }

            if (a == 0 && b == 0)

            {

                txtMessage.Text = txtMessage.Text + "w";

            }

        }

 

        private void btne_Click(object sender, RoutedEventArgs e)

        {

 

            if (a == 1)

            {

                txtMessage.Text = txtMessage.Text + "E";

            }

            if (b == 1 && a == 0)

            {

                txtMessage.Text = txtMessage.Text + "3";

            }

            if (a == 0 && b == 0)

            {

                txtMessage.Text = txtMessage.Text + "e";

            }

        }

 

        private void btnr_Click(object sender, RoutedEventArgs e)

        {

 

            if (a == 1)

            {

                txtMessage.Text = txtMessage.Text + "R";

            }

            if (b == 1 && a == 0)

            {

                txtMessage.Text = txtMessage.Text + "4";

            }

            if (a == 0 && b == 0)

            {

                txtMessage.Text = txtMessage.Text + "r";

            }

        }

 

        private void btnt_Click(object sender, RoutedEventArgs e)

        {

 

            if (a == 1)

            {

                txtMessage.Text = txtMessage.Text + "T";

            }

            if (b == 1 && a == 0)

            {

                txtMessage.Text = txtMessage.Text + "5";

            }

            if (a == 0 && b == 0)

            {

                txtMessage.Text = txtMessage.Text + "t";

            }

        }

 

        private void btny_Click(object sender, RoutedEventArgs e)

        {

 

            if (a == 1)

            {

                txtMessage.Text = txtMessage.Text + "Y";

            }

            if (b == 1 && a == 0)

            {

                txtMessage.Text = txtMessage.Text + "6";

            }

            if (a == 0 && b == 0)

            {

 

                txtMessage.Text = txtMessage.Text + "y";

            }

        }

 

        private void btnu_Click(object sender, RoutedEventArgs e)

        {

            if (a == 1)

            {

                txtMessage.Text = txtMessage.Text + "U";

            }

            if (b == 1 && a == 0)

            {

                txtMessage.Text = txtMessage.Text + "7";

            }

            if (a == 0 && b == 0)

            {

                txtMessage.Text = txtMessage.Text + "u";

            }

        }

 

        private void btni_Click(object sender, RoutedEventArgs e)

        {

            if (a == 1)

            {

                txtMessage.Text = txtMessage.Text + "I";

            }

            if (b == 1 && a == 0)

            {

                txtMessage.Text = txtMessage.Text + "8";

            }

            if (a == 0 && b == 0)

            {

                txtMessage.Text = txtMessage.Text + "i";

            }

        }

 

        private void btno_Click(object sender, RoutedEventArgs e)

        {

            if (a == 1)

            {

                txtMessage.Text = txtMessage.Text + "O";

            }

            if (b == 1 && a == 0)

            {

                txtMessage.Text = txtMessage.Text + "9";

            }

            if (a == 0 && b == 0)

            {

                txtMessage.Text = txtMessage.Text + "o";

            }

        }

 

        private void btnp_Click(object sender, RoutedEventArgs e)

        {

            if (a == 1)

            {

                txtMessage.Text = txtMessage.Text + "P";

            }

            if (b == 1 && a == 0)

            {

                txtMessage.Text = txtMessage.Text + "0";

            }

            if (a == 0 && b == 0)

            {

                txtMessage.Text = txtMessage.Text + "p";

            }

        }

 

        private void btna_Click(object sender, RoutedEventArgs e)

        {

            if (a == 1)

            {

                txtMessage.Text = txtMessage.Text + "A";

            }

            if (b == 1 && a == 0)

            {

                txtMessage.Text = txtMessage.Text + "!";

            }

            if (a == 0 && b == 0)

            {

                txtMessage.Text = txtMessage.Text + "a";

            }

        }

 

        private void btns_Click(object sender, RoutedEventArgs e)

        {

 

            if (a == 1)

            {

                txtMessage.Text = txtMessage.Text + "S";

            }

            if (b == 1 && a == 0)

            {

                txtMessage.Text = txtMessage.Text + "#";

            }

            if (a == 0 && b == 0)

            {

                txtMessage.Text = txtMessage.Text + "s";

            }

        }

 

        private void btnd_Click(object sender, RoutedEventArgs e)

        {

            if (a == 1)

            {

                txtMessage.Text = txtMessage.Text + "D";

            }

            if (b == 1 && a==0)

            {

                txtMessage.Text = txtMessage.Text + "$";

            }

            if (a == 0 && b == 0)

            {

                txtMessage.Text = txtMessage.Text + "d";

            }

        }

 

        private void btnf_Click(object sender, RoutedEventArgs e)

        {

            if (a == 1)

            {

                txtMessage.Text = txtMessage.Text + "F";

            }

            if (b == 1 && a == 0)

            {

                txtMessage.Text = txtMessage.Text + "%";

            }

            if (a == 0 && b == 0)

            {

                txtMessage.Text = txtMessage.Text + "f";

            }

        }

 

        private void btng_Click(object sender, RoutedEventArgs e)

        {

 

            if (a == 1)

            {

                txtMessage.Text = txtMessage.Text + "G";

            }

            if (b == 1 && a == 0)

            {

                txtMessage.Text = txtMessage.Text + "^";

            }

            if (a == 0 && b == 0)

            {

                txtMessage.Text = txtMessage.Text + "g";

            }

        }

 

        private void btnh_Click(object sender, RoutedEventArgs e)

        {

            if (a == 1)

            {

                txtMessage.Text = txtMessage.Text + "H";

            }

            if (b == 1 && a == 0)

            {

                txtMessage.Text = txtMessage.Text + "&";

            }

            if (a == 0 && b == 0)

            {

                txtMessage.Text = txtMessage.Text + "h";

            }

        }

 

        private void btnj_Click(object sender, RoutedEventArgs e)

        {

 

            if (a == 1)

            {

                txtMessage.Text = txtMessage.Text + "J";

            }

            if (b == 1 && a == 0)

            {

                txtMessage.Text = txtMessage.Text + "(";

            }

            if (a == 0 && b == 0)

            {

                txtMessage.Text = txtMessage.Text + "j";

            }

        }

 

        private void btnk_Click(object sender, RoutedEventArgs e)

        {

            if (a == 1)

            {

                txtMessage.Text = txtMessage.Text + "K";

            }

            if (b == 1 && a == 0)

            {

                txtMessage.Text = txtMessage.Text + ")";

            }

 

            if (a == 0 && b == 0)

            {

                txtMessage.Text = txtMessage.Text + "k";

            }

        }

 

        private void btnl_Click(object sender, RoutedEventArgs e)

        {

 

            if (a == 1)

            {

                txtMessage.Text = txtMessage.Text + "L";

            }

            if (b == 1 && a == 0)

            {

                txtMessage.Text = txtMessage.Text + "-";

            }

 

            if (a == 0 && b == 0)

            {

                txtMessage.Text = txtMessage.Text + "l";

            }

        }

 

        private void btnz_Click(object sender, RoutedEventArgs e)

        {

            if (a == 1)

            {

                txtMessage.Text = txtMessage.Text + "Z";

            }

            if (b == 1 && a==0)

            {

                txtMessage.Text = txtMessage.Text + "*";

            }

 

            if (a == 0 && b == 0)

            {

                txtMessage.Text = txtMessage.Text + "z";

            }

        }

 

        private void btnx_Click(object sender, RoutedEventArgs e)

        {

            if (a == 1)

            {

                txtMessage.Text = txtMessage.Text + "X";

            }

            if (b == 1 && a == 0)

            {

                txtMessage.Text = txtMessage.Text + "`";

            }

 

            if (a == 0 && b == 0)

            {

                txtMessage.Text = txtMessage.Text + "x";

            }

        }

 

        private void btnc_Click(object sender, RoutedEventArgs e)

        {

            if (a == 1)

            {

                txtMessage.Text = txtMessage.Text + "C";

            }

            if (b == 1 && a == 0)

            {

                txtMessage.Text = txtMessage.Text + ":";

            }

 

            if (a == 0 && b == 0)

            {

                txtMessage.Text = txtMessage.Text + "c";

            }

        }

        private void btnv_Click(object sender, RoutedEventArgs e)

        {

            if (a == 1)

            {

                txtMessage.Text = txtMessage.Text + "V";

            }

            if (b == 1 && a == 0)

            {

                txtMessage.Text = txtMessage.Text + ";";

            }

 

            if (a == 0 && b == 0)

            {

                txtMessage.Text = txtMessage.Text + "v";

            }

        }

 

        private void btnb_Click(object sender, RoutedEventArgs e)

        {

            if (a == 1)

            {

                txtMessage.Text = txtMessage.Text + "B";

            }

            if (b == 1 && a == 0)

            {

                txtMessage.Text = txtMessage.Text + "?";

            }

 

            if (a == 0 && b == 0)

            {

                txtMessage.Text = txtMessage.Text + "b";

            }

        }

 

        private void btnn_Click(object sender, RoutedEventArgs e)

        {

            if (a == 1)

            {

                txtMessage.Text = txtMessage.Text + "N";

            }

            if (b == 1 && a == 0)

            {

                txtMessage.Text = txtMessage.Text + "~";

            }

 

            if (a == 0 && b == 0)

            {

                txtMessage.Text = txtMessage.Text + "n";

            }

        }

 

        private void btnm_Click(object sender, RoutedEventArgs e)

        {

            if (a == 1)

            {

                txtMessage.Text = txtMessage.Text + "M";

            }

            if (b == 1 && a == 0)

            {

                txtMessage.Text = txtMessage.Text + "/";

            }

 

            if (a == 0 && b == 0)

            {

                txtMessage.Text = txtMessage.Text + "m";

            }

        }

 

        private void btnComma_Click(object sender, RoutedEventArgs e)

        {

            txtMessage.Text = txtMessage.Text + ",";

        }

 

        private void btndot_Click(object sender, RoutedEventArgs e)

        {

            txtMessage.Text = txtMessage.Text + ".";

        }

 

        private void btnatthareteof_Click(object sender, RoutedEventArgs e)

        {

            txtMessage.Text = txtMessage.Text + "@";

        }

 

        private void btnspace_Click(object sender, RoutedEventArgs e)

        {

            txtMessage.Text = txtMessage.Text + " ";

        }

 

private void btnClear_Click(object sender, RoutedEventArgs e)

        {

           

                int l = txtMessage.Text.Length - 1;

                txtMessage.Text = txtMessage.Text.Remove(l);

               

          

        }


The output will be:

Output6.jpg

COMMENT USING

Trending up