Learn Universal Windows Programming Via Modern C++ (Check Box)

Before reading this article, I highly recommend reading the previous parts of the series.

Check Box

Check Box control is use to select or deselect the items based on the user choice 

ex - Installing Visual Studio software , user can select the software component to be installed or not

 

This control has three selection processes & each one has its own event,

  1. Selected - Items are selected, value returns as true
  2. Unselected - Items are unselected, value returns as false
  3. Indeterminate - Items has been both selected and Unselected, value returns as null state.

1. Selected 2. Unselected 3. Indeterminate, Selected and Unselected property handle in the IsChecked property and Indeterminate property handle in the IsThreeState property, If IsChecked property contains whether  itis true or false , IsThreeState is set as null


Each property it has its own event handler , Checked, Unchecked and Indeterminate events, event has implemented in the same way as button control event

Checked event will fire , when the checkbox Item has been selected , uncheked event will fire , when the checkbox item has been unselected, if item has been selected or unselected , IsChecked property automatically sets as true or false.

Indeterminate events will fire , when the items in the ThreeState (select and unselect mode). 
 
Sample code 
  1. CheckBox chkBox;  
  2. chkBox.Checked(&ChkBoxCheckedClick);  
  3.   
  4. void App::ChkBoxCheckedClick(IInspectable const & sender, const RoutedEventArgs &args)  
  5. {  
  6.   
  7. }  
The below sample application explains about how to create checkBox control and the event handler  
  1. #include "pch.h"  
  2.   
  3. using namespace winrt;  
  4. using namespace Windows::ApplicationModel;  
  5. using namespace Windows::ApplicationModel::Activation;  
  6. using namespace Windows::Foundation;  
  7. using namespace Windows::UI;  
  8. using namespace Windows::UI::Xaml;  
  9. using namespace Windows::UI::Xaml::Controls;  
  10. using namespace Windows::UI::Xaml::Controls::Primitives;  
  11. using namespace Windows::UI::Xaml::Interop;  
  12. using namespace Windows::UI::Xaml::Media;  
  13. using namespace Windows::UI::Xaml::Navigation;  
  14. using namespace Windows::UI::Popups;  
  15. using namespace Windows::Storage;  
  16.   
  17. struct App:ApplicationT<App>  
  18. {  
  19. public:  
  20.     virtual ~App() = default;  
  21.     static IInspectable MakeString(hstring);  
  22.     static CheckBox CreateCheckBox(hstring,bool,bool);  
  23.     static bool CheckBoxStatus(IInspectable const& sender);  
  24.     static void OnLaunched(LaunchActivatedEventArgs const&);  
  25.     static void ChkBoxCheckedClick(IInspectable const & sender, const RoutedEventArgs &args);  
  26.     static void ChkBoxUnCheckedClick(IInspectable const & sender, const RoutedEventArgs &args);  
  27.     static void ChkBoxIndeterminateClick(IInspectable const & sender, const RoutedEventArgs &args);  
  28.     static hstring GetChkBoxName(IInspectable const& sender);  
  29. };  
  30.   
  31. IInspectable App::MakeString(hstring captionText)  
  32. {  
  33.     return PropertyValue::CreateString(captionText);  
  34. }  
  35.   
  36. CheckBox App::CreateCheckBox(hstring captionText,bool threeState,bool isChecked)  
  37. {  
  38.     CheckBox chkBox;  
  39.     chkBox.Content(MakeString(captionText));  
  40.   
  41.     if (threeState)  
  42.         chkBox.IsThreeState(threeState);  
  43.     else  
  44.         chkBox.IsChecked(isChecked);  
  45.       
  46.       
  47.     chkBox.Checked(&ChkBoxCheckedClick);  
  48.     chkBox.Unchecked(&ChkBoxUnCheckedClick);  
  49.     chkBox.Indeterminate(&ChkBoxIndeterminateClick);  
  50.   
  51.     return chkBox;  
  52. }  
  53.   
  54. bool App::CheckBoxStatus(IInspectable const& sender)  
  55. {  
  56.     CheckBox chkBox = sender.as<CheckBox>();  
  57.     IReference<bool> value = chkBox.IsChecked();  
  58.     auto isSeleted = value.Value();  
  59.     return isSeleted;  
  60. }  
  61.   
  62. void App::ChkBoxUnCheckedClick(IInspectable const & sender, const RoutedEventArgs &args)  
  63. {  
  64.     auto ChkStatus = CheckBoxStatus(sender);  
  65.     auto chkName = GetChkBoxName(sender);  
  66.     MessageDialog msgDlg(L"UnChecked State (false)", chkName);  
  67.     msgDlg.ShowAsync();  
  68. }  
  69.   
  70. void App::ChkBoxIndeterminateClick(IInspectable const & sender, const RoutedEventArgs &args)  
  71. {  
  72.     CheckBox chkBox = sender.as<CheckBox>();  
  73.     auto value = chkBox.IsThreeState();  
  74.     if(value)  
  75.     {  
  76.         auto chkName = GetChkBoxName(sender);  
  77.         MessageDialog msgDlg(L"Indeterminate State", chkName);  
  78.         msgDlg.ShowAsync();  
  79.     }     
  80. }  
  81.   
  82. hstring App::GetChkBoxName(IInspectable const& sender)  
  83. {  
  84.     CheckBox chkBox = sender.as<CheckBox>();  
  85.     IPropertyValue IValue = chkBox.Content().as<IPropertyValue>();  
  86.     return IValue.GetString();  
  87. }  
  88.   
  89. void App::ChkBoxCheckedClick(IInspectable const & sender, const RoutedEventArgs &args)  
  90. {  
  91.     auto ChkStatus = CheckBoxStatus(sender);  
  92.     auto chkName = GetChkBoxName(sender);  
  93.     MessageDialog msgDlg(L"Checked State (True)", chkName);  
  94.     msgDlg.ShowAsync();  
  95. }  
  96.   
  97.   
  98. void App::OnLaunched(LaunchActivatedEventArgs const&)  
  99. {  
  100.   
  101.     auto chkBox1 = CreateCheckBox(L"C++",false,false);  
  102.     auto chkBox2 = CreateCheckBox(L"C#"true,false);  
  103.     auto chkBox3 = CreateCheckBox(L"VB"falsetrue);  
  104.       
  105.     Thickness margin;  
  106.     margin.Bottom = 0;  
  107.     margin.Left = 20;  
  108.     margin.Right = 0;  
  109.     margin.Top = 5;  
  110.   
  111.     StackPanel sPanel;  
  112.     sPanel.Margin(margin);  
  113.     sPanel.Children().Append(chkBox1);  
  114.     sPanel.Children().Append(chkBox2);  
  115.     sPanel.Children().Append(chkBox3);  
  116.   
  117.     Window window = Window::Current();  
  118.     window.Content(sPanel);  
  119.   
  120.     window.Activate();  
  121. }  
  122.   
  123.   
  124. int __stdcall wWinMain(HINSTANCE,HINSTANCE,PWSTR,int)  
  125. {  
  126.     Application::Start([](auto &&) {make<App>(); });  
  127.     return 0;  
  128. }  
CheckedClick Event output 
 
 
 
UnCheckedClick Event output
 
 
 
Indeterminate Event
 
 
 
 
I hope you understood how to use the checkbox control.
 
In the above example "as" , IPropertyValue and auto keywords are used , Let's see what is the use of that

Operator and IPropertyValue

“as” operator
In the checked event handler code , first argment is "IInspectable is a sender object." This object is used to find the control . IInspectable interface means we have to convert to the control object. How do we convert it? The answer is : "as" operator , using the “as” operator, we can convert IInspectable into source of the control object like whether it's checkbox or button control. 
  1. hstring App::GetChkBoxName(IInspectable const& sender)  
  2. {  
  3.     CheckBox chkBox = sender.as<CheckBox>();  
  4. }  

IPropertyValue
As you can remember in the button control , we are using the IInspectable to set the content of the text ( check box also works the same way). Now , we will see  how to get the value from the IInspectable interface. We use IPropertyValue. Using the as operator , we can convert to IPropertyValue , from  IPropertyValue , we can get the string 

  1. hstring App::GetChkBoxName(IInspectable const& sender)  
  2. {  
  3.     CheckBox chkBox = sender.as<CheckBox>();  
  4.     IPropertyValue IValue = chkBox.Content().as<IPropertyValue>();  
  5.     return IValue.GetString();  
  6. }  
auto
auto keyword - auto keyword is used to automatically deduce the data type. It's like var keyword in C#

ex - see in the below code IsThreeState() value is assgined to the "value" property in the left side and declares the auto complie time. Auto property replaces the bool datatype.
  1. CheckBox chkBox = sender.as<CheckBox>();  
  2. auto value = chkBox.IsThreeState();  
Conclusion

I hope you understood operator , IPropertyValue and Auto keywords usage. 

Up Next
    Ebook Download
    View all
    Learn
    View all