Tuesday, February 18, 2014

Powershell and GUI : Button element

 

 Buttons

18-02-2014 16-25-39

 

One of the main component of any GUI application are buttons. Without Buttons, no GUI applications seems completed.

In my GUI application I use button a lot.

In today’s post we are going to see the basic functionality of the Button element.

Today we are going to cover only the “Click” event of the button. These “Click” event triggers when user press the button.

Let’s get started.

 

Note: I am using “Sapien PowerShell Studio 2012”   for creating GUI’s, you can also download the trial version of it for use and testing. It doesn’t matter which IDE you have used, concept are same everywhere.

1.       Open PowerShell studio 2012.

2.       Click on “New” and click on “New Form”

3.       18-02-2014 16-17-18

4.       Choose Empty Form and “Click on Select”.

5.       18-02-2014 16-17-31

6.       Now from the “Toolbox Panel”, click on “Label” and drop it on the blank form.

7.       18-02-2014 16-18-13

8.       Click on “Label” properties and change the following.

a.       Label’s Text to = “This is  a label”

b.      Design to = “labelmy”

9.       18-02-2014 16-19-55

10.   Now drag “Button” from the control pane and drop it on a form.

11.   18-02-2014 16-19-55

12.   Now  double click on “Button” and it will open the script pane,

13.   And you can see the Button name and a click method

14.   18-02-2014 16-20-32

$button1_Click={

       #TODO: Place custom script here

      

      

      

15.               }

18-02-2014 16-42-21

16.   $button1 is the name of our button and Click is a click method.

17.   Now let’s make this button do something.

18.    I have added the below line in the button click method

a.       $labelmy.Text = 'Oh my God!! Button is pressesd.'

19.   18-02-2014 16-22-03

20.   So when we press the button, our label text “This is a Label” will be changed to “'Oh my God!! Button is pressesd.'

21.   Let’s see that.

22.   Go back to form and click on “Run File” or press “Ctrl + F5”

23.   18-02-2014 16-22-19

24.   And when the form run, click on the button. And you will see that text of label is changed.

25.   18-02-2014 16-22-54

26.   Now let’s add some more fun.

27.   I have added the one more line of code and change the text for the label.

 

 

28.   18-02-2014 16-23-58

$button1_Click={

       #TODO: Place custom script here

      

       $labelmy.Text = 'Notepad! will be opened.'

       Start-Process 'notepad.exe'

      

29.               }

30.   Now, when we click on our button, the text of our label will change and it will open a notepad too.

31.   Run the form again and click on “button”

32.   And you can see that text is changed and a new notepad window is opened.

33.   18-02-2014 16-24-41

 

That’s all for now.

See you in my next blog posts.

 

Regards

clip_image017 clip_image018 clip_image019 clip_image020  clip_image021Aman Dhally

 

1 comment:

  1. Hi Team Delhi,
    This is what I have been after for a week :) I am extremely new at Powershell. This was perfectly explained.

    Thank you

    ReplyDelete

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