SystemSounds in F#

To play a system sound in Visual F#, use the SystemSounds object. For a simple example on using the SystemSounds object, try the following:


// Learn more about F# at http://fsharp.net
//use the f# standard library
open System
//use media classes
open System.Media
//specify the memory location of the classes used in drawing objects
//required to draw the listbox item text
open System.Drawing
//specify the location of the form class
open System.Windows.Forms
//creates a form and assign a "Play System Sounds" caption to it
let soundform=new Form(Text="Play System Sounds",StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)
//creates a label and set its Text to “Count”
let lbl=new Label(Text="System sounds:", Location=new System.Drawing.Point(20,10),AutoSize=true)
//makes a listbox
let soundlistbox=new ListBox(Sorted=true,Location=new System.Drawing.Point(20,30),FormattingEnabled=true)
//adds an item to the listbox when the form is loaded
soundform.Load.Add(fun items->
     //adds the items and ignore the passed index position values
                    soundlistbox.Items.Add("Asterisk")|>ignore
                    soundlistbox.Items.Add("Beep")|>ignore
                    soundlistbox.Items.Add("Exclaimation")|>ignore
                    soundlistbox.Items.Add("Hand")|>ignore
                    soundlistbox.Items.Add("Question")|>ignore)
soundlistbox.Click.Add(fun playsound->
                    if soundlistbox.SelectedIndex=0 then
                        SystemSounds.Asterisk.Play()
                        MessageBox.Show("Asterisk", "System Sounds", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)|>ignore
                    if soundlistbox.SelectedIndex=1 then
                        SystemSounds.Beep.Play()
                        MessageBox.Show("Beep", "System Sounds", MessageBoxButtons.OK)|>ignore
                    if soundlistbox.SelectedIndex=2 then
                        SystemSounds.Exclamation.Play()
                        MessageBox.Show("Exclaimation", "System Sounds", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)|>ignore
                    if soundlistbox.SelectedIndex=3 then
                        SystemSounds.Hand.Play()
                        MessageBox.Show("Hand", "System Sounds", MessageBoxButtons.OK, MessageBoxIcon.Hand)|>ignore
                    if soundlistbox.SelectedIndex=4 then
                       SystemSounds.Question.Play()
                        MessageBox.Show("Question", "System Sounds", MessageBoxButtons.OK, MessageBoxIcon.Question)|>ignore) 
                                           
//displays the label to our form
soundform.Controls.Add(lbl)      
//adds the listbox to our form    
soundform.Controls.Add(soundlistbox)           
soundform.Show()
Application.Run(soundform)

This will generate the following output:

Adding space to a concatenated String

There are several ways to add space to a contanated string in Visual F#:

1.Using the Tab Escape Character

Syntax:
“text1” + “\t” + “textn”

For instance:
Trace.WriteLine("Adding" +”\t”+ “Space”)

This will display “Adding Space” with 5-6 spaces in between in the output window. You can also use the unicode equivalent of tab which is \u0009. For example:
Trace.WriteLine("Adding" +”\u0009”+ “Space”)
2. Typing a space or “ “ between the concatenated strings
Syntax:
“text1” + “ ” + “textn”

For instance:
Trace.WriteLine("Adding" +” ”+ “Space”)
You can also use the unicode equivalent of space which is \u0032. For example:
Trace.WriteLine("Adding" +”\u0032”+ “Space”)
3. Using the Convert.ToChar() string manipulation function
Convert.ToChar converts a specified key code to character.
The key code for tab is 9 while Spacebar has 32.

Syntax:
Convert.ToChar(key code)
Examples:
//backspace key
Trace.WriteLine(Convert.ToString(Convert.ToChar(8)))
//enter key
Trace.WriteLine(Convert.ToString(Convert.ToChar(13)))
//adds space to a concatenated string
Trace.WriteLine(“Adding” + Convert.ToString(Convert.ToChar(32)) + “Space” )

For a simple example of application that adds space to a concatenated string:

1. Click Start>All Programs>Microsoft Visual Studio 2008>Microsoft Visual Studio 2008.

2. Click File>New>Project>Select Visual F# in the project types>Select F# application in the Visual Studio installed templates category.

3. Click the Project menu>Add reference>Click the .Net tab>Locate then double-click System.Windows.Forms. Do step 3 again and this time, select System.Drawing from the .Net tab.

4. Enter the following code after the line “// Learn more about F# at http://fsharp.net “:

// Learn more about F# at http://fsharp.net
//specifies the namespace memory location of the classes that
//will be used in our application
open System
open System.Diagnostics
open System.Windows.Forms
open System.Drawing
//creates our form
let myform= new Form(Text="Adding Space")
//creates our controls  
let label1=new Label(Text="Firstname:",Location=new System.Drawing.Point(0, 10),AutoSize=true)
let label2=new Label(Text="Lastname:",Location=new System.Drawing.Point(0, 50),AutoSize=true)
let fnametextbox=new TextBox(Location=new System.Drawing.Point(100,10),BorderStyle=BorderStyle.FixedSingle)
let lnametextbox=new TextBox(Location=new System.Drawing.Point(100,50),BorderStyle=BorderStyle.FixedSingle)
let okbutton=new Button(Text="Ok", Location=new System.Drawing.Point(120, 170))
let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 170))
myform.Controls.Add(label1)
myform.Controls.Add(label2
myform.Controls.Add(fnametextbox)
myform.Controls.Add(lnametextbox)
myform.Controls.Add(okbutton)
myform.Controls.Add(exitbutton)
myform.Click.Add(fun space->
//display our text in the ouput window
Trace.WriteLine("Adding Spaces")
Trace.WriteLine(label1.Text + "\u0009" + fnametextbox.Text)
Trace.WriteLine(label2.Text + "\u0009" + lnametextbox.Text))

//execute our application
myform.Show()
Application.Run(myform)