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:


  1. // Learn more about F# at http://fsharp.net  
  2. //use the f# standard library  
  3. open System  
  4. //use media classes  
  5. open System.Media  
  6. //specify the memory location of the classes used in drawing objects  
  7. //required to draw the listbox item text  
  8. open System.Drawing  
  9. //specify the location of the form class  
  10. open System.Windows.Forms  
  11. //creates a form and assign a "Play System Sounds" caption to it  
  12. let soundform=new Form(Text="Play System Sounds",StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)  
  13. //creates a label and set its Text to “Count”  
  14. let lbl=new Label(Text="System sounds:", Location=new System.Drawing.Point(20,10),AutoSize=true)  
  15. //makes a listbox  
  16. let soundlistbox=new ListBox(Sorted=true,Location=new System.Drawing.Point(20,30),FormattingEnabled=true)  
  17. //adds an item to the listbox when the form is loaded  
  18. soundform.Load.Add(fun items->  
  19.      //adds the items and ignore the passed index position values  
  20.                     soundlistbox.Items.Add("Asterisk")|>ignore  
  21.                     soundlistbox.Items.Add("Beep")|>ignore  
  22.                     soundlistbox.Items.Add("Exclaimation")|>ignore  
  23.                     soundlistbox.Items.Add("Hand")|>ignore  
  24.                     soundlistbox.Items.Add("Question")|>ignore)  
  25. soundlistbox.Click.Add(fun playsound->  
  26.                     if soundlistbox.SelectedIndex=0 then  
  27.                         SystemSounds.Asterisk.Play()  
  28.                         MessageBox.Show("Asterisk""System Sounds", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)|>ignore  
  29.                     if soundlistbox.SelectedIndex=1 then  
  30.                         SystemSounds.Beep.Play()  
  31.                         MessageBox.Show("Beep""System Sounds", MessageBoxButtons.OK)|>ignore  
  32.                     if soundlistbox.SelectedIndex=2 then  
  33.                         SystemSounds.Exclamation.Play()  
  34.                         MessageBox.Show("Exclaimation""System Sounds", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)|>ignore  
  35.                     if soundlistbox.SelectedIndex=3 then  
  36.                         SystemSounds.Hand.Play()  
  37.                         MessageBox.Show("Hand""System Sounds", MessageBoxButtons.OK, MessageBoxIcon.Hand)|>ignore  
  38.                     if soundlistbox.SelectedIndex=4 then  
  39.                        SystemSounds.Question.Play()  
  40.                         MessageBox.Show("Question""System Sounds", MessageBoxButtons.OK, MessageBoxIcon.Question)|>ignore)   
  41.                                              
  42. //displays the label to our form  
  43. soundform.Controls.Add(lbl)        
  44. //adds the listbox to our form      
  45. soundform.Controls.Add(soundlistbox)             
  46. soundform.Show()  
  47. Application.Run(soundform)  

This will generate the following output: