Saturday, February 22, 2014

PowerShell and GUI : Checkbox element



Checkbox

clip_image001
Checkbox are very useful element in GUI’s.
When you have to provide options to the user, like, if we want to enable “Active Directory” user account, or disable it, or option of creating email addresses while creating accounts etc.
Checkbox has two major properties, either they can be checked/ticked, or not checked/ticked.
If checkbox is checked / ticked that means it is True ($true).
If checkbox is not checked / ticked that meant it is False ($false).
We can write our logic based on these two properties. We can write that , if checkbox’s checked value is true then do this action and if the checkbox’s checked value is false do this.

Let’s see it in action


Open your favourite PowerShell GUI creator, in my case “PowerShell Studio 2012”.
Create a new form and add these three elements to the form.
1.       Label          clip_image002
2.       Button       clip_image003
3.       Checkbox  clip_image004

You form should be look like this.
clip_image006

Now we are going to implement the following logic.
 If we ticked the checkbox and click on button, our label text will be changed to “Check box is checked.”, and when we un-tick the checkbox and press the button, our text of the label will say “Checkbox is not checked”.
Now double click on our button and it will create a “Mouse Click” event {which we have covered in my past blog post } and add the  below code there.
$checkbox1 is our Checkbox
$button1 is our Button
$label1 is our label

       if ( $checkbox1.Checked -eq $true )
              {
                     $label1.Text = "Checkbox is checked."
                     $label1.ForeColor = 'Green'
             
              }
      
       else
              {
             
                     $label1.Text = "Checkbox is not checked!"
                     $label1.ForeColor = 'Red'
              }
             

clip_image008

Now run the form, in PowerShell studio press Ctrl + 5
You form look like below first.
clip_image009
Now just press on “Button” without ticking on the checkbox. And you will see that our label is changed to the “Checkbox is not checked”.
clip_image010

Now tick on the check and press the button again.
You can see that text of our label is changed to “Checkbox is checked”.
clip_image011

Video
Video on using checkbox

That’s all of now.

See you in my next blog post.

Thanks
Regards
Aman Dhally
clip_image017 clip_image018 clip_image019 clip_image020  clip_image021






1 comment:

  1. It is better to use wpf controls,that is what i think

    ReplyDelete

Note: Only a member of this blog may post a comment.