I was looking around the System.Windows.Forms namespace and was interested by the TrackBar Class.

To familiarize myself with it’s use, I put together a script showing the class in action. I used the Beep method of the System.Console class for the event.

The beep has varied frequency and duration, which is perfect to control with the TrackBar. The frequency can be between 37 and 32767 hertz. I allowed durations between 1 and 2000 milliseconds, they could be most anything. The output is to the console speaker.

First I setup the main window and the basic definition of all the objects I would use.

# Main Form and Objects
$BeepForm = New-Object Windows.Forms.Form
$FrequencyTrackBar = New-Object Windows.Forms.TrackBar
$DurationTrackBar = New-Object Windows.Forms.TrackBar
$FrequencyLabel = New-Object System.Windows.Forms.Label
$DurationLabel = New-Object System.Windows.Forms.Label
$LineLabel = New-Object System.Windows.Forms.Label
$PlayButton = New-Object System.Windows.Forms.Button
$ExitButton = New-Object System.Windows.Forms.Button
$BeepForm.BackColor = [System.Drawing.Color]::White
$BeepForm.Font = “Comic Sans MS,8.25”
$BeepForm.Text = “WinForms TrackBar Demo”
$BeepForm.Size = “500,300”

The frequency trackbar is defined. Setup the position, orientation, height and width. The range matches the min and max values allowed or desired. You adjusted the tick frequency so that you can readily tell your relative position within the range, but not too many as to crowd it. Tick style is used to position the tick marks relative to the trackbar. TopLeft positions the ticks above the trackbar in a horizontal orientation. I set the inital values for the TrackBar value.

# Frequency TrackBar
$FrequencyTrackBar.Location = “70,20”
$FrequencyTrackBar.Orientation = “Horizontal”
$FrequencyTrackBar.Width = 350
$FrequencyTrackBar.Height = 40
$FrequencyTrackBar.TickFrequency = 1000
$FrequencyTrackBar.TickStyle = “TopLeft”
$FrequencyTrackBar.SetRange(37, 32767)
$FreqTrackBarValue = 2000
$FrequencyTrackBar.Value = 2000

The frequency trackbar event handler uses add_ValueChanged get values as the trackbar is moved. When there is a change, I get the value then load it into the frequency label for display on the form and set a global variable for use when the beep is played.

#Frequency TrackBar Event Handler
$FrequencyTrackBar.add_ValueChanged({
$FreqTrackBarValue = $FrequencyTrackBar.Value
$FrequencyLabel.Text = “Frequency ($FreqTrackBarValue)”
$global:FrequencyValue = $FreqTrackBarValue
})

The trackbar is then added to the form.

 $BeepForm.Controls.add($FrequencyTrackBar)

The frequency label is setup with position and dimensions. The text is set to an initial value as it will be updated as the TrackBar moves. It is then added to the form.

   # Frequency Label
    $FrequencyLabel.Location = “200,75”
    $FrequencyLabel.Size = “120,30”
    $FrequencyLabel.Text = “Frequency ($FreqTrackBarValue)”
    $BeepForm.Controls.Add($FrequencyLabel)

The play button is defined like the other objects. The add_Click calls a function to play the beep. The function call requires brackets.

  # Play Button 
   $PlayButton.Location = “120,220”
    $PlayButton.Size = “75,23”
    $PlayButton.Text = “Play”
    $PlayButton.add_Click({PlayIt})
    $BeepForm.Controls.Add($PlayButton)

The PlayIt function calls the beep passing the global variables accumulated from the TrackBar movement.

    # Play the Tone
    Function PlayIt {
        [System.Console]::Beep($global:FrequencyValue,$global:DurationValue)
    }

The rest of the objects are setup similarly, as you can see in the code. The TrackBar is a pretty cool control.   It is a bit limited in where you could use it but very nice in the right circumstance. As for the beep, I found that the entire frequency range is not playable, at least on my laptop. Below 50 hertz and above 13,000 you just get a click or nothing. The speaker doesn’t support the whole range. Now if only this script did something of more value than just annoying your cubby mates…

TrackBarDemo

TrackBarDemo.ps1

Advertisements