articles

Home / DeveloperSection / Articles / RadioButton Control in Windows 7 Phone Development

RadioButton Control in Windows 7 Phone Development

Anonymous User 17989 11-Apr-2012

If you want to give few options to users out of which only one of this can be selected, radio buttons are used. Once you define set of radio buttons, you can bind them together using ‘GroupName’ property.

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

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 radio button control from toolbox which is at top left corner.

RadioButton Control in Windows 7 Phone Development

Write down the following code to build the 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="RadioButton 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" />

            <RadioButton Content="Australia" Height="72" Margin="58,74,205,0" Name="rbAustralia" VerticalAlignment="Top" GroupName="gbtnFavourit" BorderBrush="#BF22DE4D" />

            <RadioButton Content="England" Height="72" HorizontalAlignment="Left" Margin="58,152,0,0" Name="rbEngland" VerticalAlignment="Top" GroupName="gbtnFavourit" />

            <RadioButton Content="India" Height="72" HorizontalAlignment="Left" Margin="58,230,0,0" Name="rbIndia" VerticalAlignment="Top" GroupName="gbtnFavourit" />

        </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)

        {

            if (Convert.ToBoolean(rbAustralia.IsChecked))

            {

                MessageBox.Show("You have selected team Australia!");

            }

            else if (Convert.ToBoolean(rbEngland.IsChecked))

            {

                MessageBox.Show("You have selected team England!");

            }

            else if (Convert.ToBoolean(rbIndia.IsChecked))

            {

                MessageBox.Show("You have selected team India!");

            }

            else

            {

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

            }

           

        }     

      

    }

}

Now execute your program and you will see following output.


RadioButton Control in Windows 7 Phone Development

Now select any option from above list and click on button ‘Submit’.

RadioButton Control in Windows 7 Phone Development



Updated 21-May-2020
I am a content writter !

Leave Comment

Comments

Liked By