Create CheckBox in WPF using VB.NET

In this article you will learn about the CheckBox creation in WPF.
  • 1929

CheckBox: CheckBox is a graphical user interface element that allows the user to make multiple selections from a number of options. A CheckBox control is a ContentControl, which means it can contain content such as text, images, or panels. CheckBox controls inherit from the ToggleButton class, which in turn inherits from ButtonBase. Most important, ToggleButton adds an IsChecked property. IsChecked is a nullable Boolean, which means it can be set to true, false, or null. Obviously, true represents a checked box, while false represents an empty one. The null value is a little trickierâ€"it represents an indeterminate state, which is displayed as a shaded box.

The given below example shows CheckBox example based on a states that is key pressed states.

Example of the CheckBox
Xaml Code

<Window x:Class="WpfApplication1.Windows"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF" Height="170" Width="246">
    <StackPanel HorizontalAlignment="Center" Width="216">
        <UniformGrid Columns="2">
            <UniformGrid.Resources>
                <Style TargetType="{x:Type CheckBox}">
                    <Setter Property="IsHitTestVisible" Value="False" />
                    <Setter Property="Margin" Value="5" />
                </Style>
            </UniformGrid.Resources>
            <CheckBox Content="LeftShift" Name="chkLControl"/>
            <CheckBox Content="RightShift" Name="chkRControl" Width="92" /> 
        </UniformGrid>
        <Button Content="Check" Margin="10" Click="Button_Click"/>
    </StackPanel>
</
Window>

VBCode

Imports System
Imports System.Windows
Imports System.Windows.Input
Namespace WpfApplication1
    Partial Public Class Windows
        Inherits Window
        Public Sub New()
            InitializeComponent()
            CheckKeyboardState()
        End Sub
        Private Sub Button_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
            CheckKeyboardState()
        End Sub
        Private Sub CheckKeyboardState()
            chkLControl.IsChecked = Keyboard.IsKeyDown(Key.LeftShift)
            chkRControl.IsChecked = Keyboard.IsKeyDown(Key.RightShift)
        End Sub
    End Class
End Namespace

Output Window

 checkb1.gif

Conclusion

Hope this article helps you to understand the CheckBox in WPF.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.