articles

Home / DeveloperSection / Articles / CheckBox control in Windows Phone 7 Development

CheckBox control in Windows Phone 7 Development

Anonymous User 13200 11-Apr-2012

If you want to give some options to users and users can select one option or more than one option then we use, CheckBox control are used.

This article is going to explain how to use CheckBox control in windows 7 phone application using c# code.  Let’s see a brief demonstration on it.

Getting Started:

1.       Open visual studio and go to File -> New -> Project.

2.       Select Silverlight for Windows Phone option

3.       Enter the project name and choose the saving location

4.       Click on button ‘Ok’

Now drag and drop checkbox control from toolbox which is at top left corner.

CheckBox control in Windows Phone 7 Development

Write down the following code to build above layout.

Code of ‘MainPage.xaml’:

<!--LayoutRoot is the root grid where all page content is placed-->

    <Grid x:Name="LayoutRoot" Background="Transparent" Width="480">

        <Grid.RowDefinitions>

            <RowDefinition Height="Auto"/>

            <RowDefinition Height="*"/>

        </Grid.RowDefinitions>

 

        <!--TitlePanel contains the name of the application and page title-->

        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28" Background="#FF6C1643">

            <TextBlock x:Name="ApplicationTitle" Text="My Application" Style="{StaticResource PhoneTextNormalStyle}"/>

            <TextBlock x:Name="PageTitle" Text="CheckBox Demo" Margin="0,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}" Height="90" Width="424" FontSize="48" Foreground="#FFE8E8EB" AllowDrop="False" />

        </StackPanel>

 

        <!--ContentPanel - place additional content here-->

        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0" Background="#FF194533">

            <Button Content="Submit" Height="88" HorizontalAlignment="Left" Margin="140,350,0,0" Name="btnSubmit" VerticalAlignment="Top" Width="170" Background="#FF15A7E8" DataContext="{Binding}" AllowDrop="False" UseLayoutRounding="False" VerticalContentAlignment="Center" Click="btnSubmit_Click" />

           

            <CheckBox Content="Australia" Height="72" HorizontalAlignment="Left" Margin="40,80,0,0" Name="chkAustralia" VerticalAlignment="Top" />

            <TextBlock Height="46" HorizontalAlignment="Left" Margin="12,28,0,0" Name="textBlock1" Text="Which country having largest population?" VerticalAlignment="Top" Width="456" FontSize="24" Foreground="#FFEFBA0C" />

            <CheckBox Content="England" Height="72" HorizontalAlignment="Left" Margin="40,140,0,0" Name="chkEngland" VerticalAlignment="Top" />

            <CheckBox Content="South Africa" Height="72" HorizontalAlignment="Left" Margin="40,198,0,0" Name="chkSAfrica" VerticalAlignment="Top" />

            <CheckBox Content="India" Height="72" HorizontalAlignment="Left" Margin="40,254,0,0" Name="chkIndia" VerticalAlignment="Top" />

        </Grid> </Grid>

Code of MainPage.xaml.cs:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

using Microsoft.Phone.Controls;

 

namespace testPhoneApp

{

    public partial class MainPage : PhoneApplicationPage

    {

        // Constructor

        public MainPage()

        {

            InitializeComponent();

        }

 

        private void btnSubmit_Click(objectsender, RoutedEventArgse)

        {

            string checkedItem= null;

           

            if (Convert.ToBoolean(chkAustralia.IsChecked))

 

                checkedItem="Australia";

 

            if (Convert.ToBoolean(chkEngland.IsChecked))

 

                checkedItem+=", England";

 

            if (Convert.ToBoolean(chkSAfrica.IsChecked))

 

                checkedItem+=", South Africa";

 

            if (Convert.ToBoolean(chkIndia.IsChecked))

 

                checkedItem+=", India";

 

            if (checkedItem== null)

                MessageBox.Show("You have not selected any value!");

 

            else

            {

                if (checkedItem.Equals(", India"))

                    MessageBox.Show("Correct Answer!");

                else

                    MessageBox.Show("Wrong Answer!");

            }

 

        }

 

 

    }

}

 

Now execute your program and you will see following output.


CheckBox control in Windows Phone 7 Development

Now select any option and submit to see your result.

CheckBox control in Windows Phone 7 Development

This is the complete description on Windows 7 CheckBox Control. I hope you will be enjoying it and I’m very thankful to you for reading this article. If you’ve any suggestion related to this article, please write your feedback in comment section.



Updated 07-Sep-2019
I am a content writter !

Leave Comment

Comments

Liked By