This is default featured slide 4 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 1 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 2 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 4 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

Me And My Respected Teacher Mr Kamal Sheel Mishra

Mr. K.S. Mishra is HOD of Computer Science from SMS Varanasi where I have completed my MCA

Me And My Respected Teacher Mr Udayan Maiti

Mr. Udayan Maiti is a senior .Net Expert and has guided many professionals of multi national Companies(MNC)

Me And My Best Friend Mr Ravinder Goel

Mr. Ravinder Goel is a senior Software Engineer and now he is working Wipro Technology

Saturday, 19 April 2014

How to use Checkbox in WPF

How to use Checkbox in WPF........

In computing, a checkbox is a graphical user interface element (widget) that permits the user to make a binary choice, i.e. a choice between one of more possible mutually exclusive options.
There are three property in Checkbox
  1. Checked
  2. Unchecked
  3. Trist ate Indeterminate

For Example:
Note: 1 In this program We will discuss how to set  three property in checkbox 

Code for Window4.xaml………………………….

<StackPanel Margin="10">
<Label FontWeight="Bold">Subject Options</Label>
<StackPanel Margin="10,5">
<CheckBox IsThreeState="True" Name="chkAll" Checked="chkAll_Checked"  >Enable all</CheckBox>
<StackPanel Margin="20,5">
<CheckBox Name="chk1" >.Net</CheckBox>
<CheckBox Name="chk2" >Java</CheckBox>
<!--Unchecked............-->
<CheckBox Name="chk3" IsChecked="True">Php</CheckBox>
<!--Trist ate Indeterminate-->
<CheckBox Name="chk4" IsChecked="{x:Null}">Android</CheckBox>
</StackPanel>
</StackPanel>
</StackPanel>


public partial class CheckBoxControl : Window
{
public CheckBoxControl()
{
InitializeComponent();
}

private void chkAll_Checked(object sender, RoutedEventArgs e)
{
bool t = (chkAll.IsChecked == true);
chk1.IsChecked = t;
chk2.IsChecked = t;
chk3.IsChecked = t;
chk4.IsChecked = t;
}
}
Result:

Note: 2 in this program we will discuss how to work with Enable All concept in checkbox 


<Window x:Class="WpfApplicationControls.CheckBoxControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="CheckBoxControl" Height="300" Width="300">
<Grid>
<StackPanel Margin="10">
<Label FontWeight="Bold">Subject Options</Label>
<StackPanel Margin="10,5">
<CheckBox IsThreeState="True" Name="chkAll" Checked="chkAll_Checked" Unchecked="chkAll_Unchecked_1"  >Enable all</CheckBox>
<StackPanel x:Name="stpanel1" Margin="20,5">
<CheckBox Name="chk1" >.Net</CheckBox>
<CheckBox Name="chk2" >Java</CheckBox>
<CheckBox Name="chk3" >Php</CheckBox>
<CheckBox Name="chk4" >Android</CheckBox>
 </StackPanel>
</StackPanel>
</StackPanel>
 </Grid>
</Window>

using System;
using System.Text;
using System.Windows;
using System.Windows.Controls;


namespace WpfApplicationControls
{
/// <summary>
/// Interaction logic for CheckBoxControl.xaml
/// </summary>
public partial class CheckBoxControl : Window
{
public CheckBoxControl()
{
InitializeComponent();
}
//When ChkAll is Checked .....
private void chkAll_Checked(object sender, RoutedEventArgs e)
{
if (chkAll.IsChecked == true)
{
foreach (UIElement ul in stpanel1.Children)
{
if (ul is CheckBox)
{
CheckBox chk = (CheckBox)ul;
chk.IsChecked = true;
}
}
}

}
//When ChkAll is UnChecked .....
private void chkAll_Unchecked_1(object sender, RoutedEventArgs e)
{
foreach (UIElement ul in stpanel1.Children)
{
if (ul is CheckBox)
{
CheckBox chk = (CheckBox)ul;
chk.IsChecked = false;
}
}
}
}
}

Result:2


Note: 3 in this program we will discuss how to get checked value of Checkbox in WPF

Code for Window4.xaml………………………….
<Window x:Class="WpfApplicationControls.Window4"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window4" Height="300" Width="300">
<Grid>

<Grid.RowDefinitions>
<RowDefinition Height="35"></RowDefinition>
<RowDefinition Height="35"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="35"></RowDefinition>
</Grid.RowDefinitions>

<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>

<TextBlock Grid.Row="0" Grid.ColumnSpan="2" Text="CheckBox in W P F" TextAlignment="Center" FontSize="20"  Foreground="Red"  Padding="3,0,25,35"></TextBlock>

<Label Grid.Row="1" Grid.Column="0" Content="Name" Padding="30,10,10,3"></Label>
<Label Grid.Row="2" Grid.Column="0" Content="Course" Padding="30,10,10,3"></Label>
<TextBox Grid.Row="1" Grid.Column="1"  Margin="10,5,6,6" Width="150"></TextBox>

<StackPanel Grid.Row="2" x:Name="spforcheckbox" Grid.Column="1" Width="150" Height="125" Background="Azure" >

<CheckBox Margin="4" Content="High School" IsChecked="True" />
<CheckBox Margin="4" Content="Intermidiate" />
<CheckBox Margin="4" Content="Graduation" />
<CheckBox Margin="4" Content="Post Graduation" />

</StackPanel>

<Button Grid.Row="3" Grid.Column="0" Content="GetValue" x:Name="btngetvalue" Margin="5,5,5,5" Click="btngetvalue_Click" ></Button>

<Label Grid.Row="3" Grid.Column="1" x:Name="lblmessage" Margin="5,5,5,5" ></Label>

</Grid>

</Window>

Code for Window4.xaml………………………….
using System;
using System.Text;
using System.Windows;
using System.Windows.Controls;

namespace WpfApplicationControls
{
/// <summary>
/// Interaction logic for Window4.xaml
/// </summary>
public partial class Window4 : Window
{
public Window4()
{
InitializeComponent();
}
string res;
//get checked value from CheckBox which is located in Stack Panel…………….
private void btngetvalue_Click(object sender, RoutedEventArgs e)
{
res = "";
foreach (UIElement ul  in spforcheckbox.Children)
{
if (ul is CheckBox)
{
CheckBox chk =(CheckBox)ul;
if (chk.IsChecked==true)
{
res += chk.Content + ",";
}
}
}
lblmessage.Content = res.Remove(res.Length - 1);
}
}
}

Result: