Automate, Accelerate, Accurate

Main menu

Tag Archives: Twitter

Pulled together some scripts to make a cmdlet which returns version information from one or more SQL servers. Remember to set the SQLServer parameter value correctly. It will be different depending on whether you are using a named instance. You can find a copy of the source code at my powershell.amsterdam gitlab repository.

I’ve used Invoke-Command in the script so that the function can also be run remotely against other systems.

Default Winforms on their own can sometimes look a bit bland, but there are some things we we can do, which do not require much coding, that can really enrich the GUI experience.

The MouseEnter and MouseLeave events can be easily overlooked when writing a forms application. A MouseEnter event is raised when the mouse pointer moves over an area, or to be more correct, a specific controls region. The MouseLeave event is the opposite. It is raised when the pointer is moved outside of the region of a control.

You can take advantage of these events in many ways. In this post, we’ll use them to highlight the control over which the pointer is, and display descriptive text associated with that control. The style is meant to be not unsimilar to some of the Microsoft installation dialogs. 🙂

NB If you wish, you can skip the steps below by downloading the source files from my GitHub repository.

Create the Forms Project

First of all, create a new forms project. Let’s call it MouseEnter. Now add the following controls, setting their properties as mentioned below.

Position labelDescription so that it occupies the left half of the form, underneath the blue panel.

Position these last three labels on the right hand side of the form, with the Options label at the top, followed by the other two directly under it.

Your form layout should now look something like this :

With the labelDescription control positioned as we want it, clear its the text property, so that it is blank.

Now we need to add code for five events :

form loading

mouse enters overview label region

mouse leaves overview label region

mouse enters mouse enter application region

mouse leaves mouse leave application region

Writing the Event Code

We’ll register event handlers for the above actions by the process below, and then paste in the event code that is further down this page.

Select the properties panel, and the events (lightening) button

Double click on the form

Go back to the form view, select the overview label, double click on MouseEnter.

Do this also for the MouseLeave event

Repeat the above for the mouseenterapplication label.

Once you have completed, this your form should show a set of entries for the events we have selected. These will be empty. Select the text, and clear it.

Select the text below, copy it into the clipboard, and then paste it into the code window.

PowerShell

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

$labelOverview_MouseEnter={

$labelOverview.ForeColor='Blue'

$labelDescription.Text=$overviewText

}

$labelOverview_MouseLeave={

$labelOverview.ForeColor='Black'

$labelDescription.Text=$defaultDescriptionText

}

$labelMouseEnterApplication_MouseEnter={

$labelMouseEnterApplication.ForeColor='Blue'

$labelDescription.Text=$applicationText

}

$labelMouseEnterApplication_MouseLeave={

$labelMouseEnterApplication.ForeColor='Black'

$labelDescription.Text=$defaultDescriptionText

}

$frmMouseEnterDemo_Load={

$script:defaultDescriptionText='Welcome to the MouseEnter example! This example demonstrates how we can use this.'

$script:overviewText='When the mouse pointer enters an area on which a control resides, a MouseEnter is raised. After the mouse departs from that specific controls area, a MouseLeave event occurs'

$script:applicationText='This application provides an example of how we can use the MouseEnter and MouseLeave events to enrich the GUI experience on a form'

$labelDescription.Text=$script:defaultDescriptionText

}

This sets the text that to be shown when a control’s region is entered or left by the mouse pointer, and also other actions to be performed in these circumstances. When the pointer enters a control’s region, we set its text is set blue, and the label on the left hand of the screen displays some descriptive text. On startup, or when a pointer leaves a controls region, this description is reset back to the default welcome text.

Rearrange your form as you wish.

Conclusion

Now let’s run our project to see the results.

Overview Mouse Enter

Overview MouseLeave

When the pointer is positioned over one of the options, the font changes to blue, and the descriptive text on the left hand side also changes. When the pointer moves outside the controls region, the default text is displayed, and the font set back to black.

And a video which highlights what’s happening a bit better. Apologies for the poor quality, I’m still wrestling with uploads to YouTube at the moment :

Love it or loathe it, URL shortening has been with us a while now and can certainly be handy. TinyURL are one such company to offer this service. Nicely for us, we do not need to register in order to use their API, and yet nicer still is that we can use it simply by entering a standard format of URL.

Before we see how we can use PowerShell to automate this process, let’s take a look at the format of URL that we need to use with TinyURL.

http://tinyurl.com/api-create.php?url=targetaddress

Where targetaddress refers to the URL that you wish to shorten.

And that’s it.

Let’s say we wanted share a link containing information about this years PowerShell Summit Europe event in Stockholm. The full length URL for this is :

For making this happen via PowerShell, Invoke-WebRequest is our friend. All we need to do is provide the required address via the Uri parameter, and the Content property of the returned HtmlWebResponseObject will contain its shortened equivalent.

So for the case of the above we’d be using a command (note the pipeline symbol) of the type :

I’ve put together a cmdlet called Get-TinyURL for doing this. At its simplest, you can run it with the Uri parameter, and it will return a PSObject containing the original full address and its shortened equivalent.

It’s also been bulked out a bit to give some extra functionality, such as being able to read from and write to the clipboard if we want. With both options enabled, we can copy a full address into the clipboard, run the cmdlet, and automatically have the shortened URL available for pasting wherever we want it next.

Navigate to desired URL and copy it to the clipboard

PowerShell

1

Get-TinyURL-ReadClipboard-WriteClipboard

Run the required command

Paste where required

The code used is listed below, and will also be posted on GitHub in due course.