Tuesday, December 2, 2008

Wed December 3: Something Old Something new

Greetings, chattering code monkeys!

Today we are going to do at least two things before you get after your personal projects. This posting is about the first one; there'll be a second posting in short order.

WHAT TO DO:

A) create a new project, and name it after yourself plus the phrase "JukeBox"

B) To this form, add a button and a combobox. Change the text on the button to "Play"

C) get into the combobox's properties sheet, select the "collections" thingamabob, then add these items to it:

Cars
Girl U Want
Musique Automatique
I Gotta Wear Shades

these are the names of some songs I have for you in the VB Depot folder. Please note that they are .wav files, as VB will not play mp3s, at least not that I've seen

D) OK, now to get some code monkeyin' done! get into your code, and just beneath
"Public Class Form1" add in a String variable named "DaFolder", another String variable called "MySong" and a String Array called Songs( ) that has as its elements the names of those songs you just put into the combobox. Make sure to add the .wav extension at the end of each filename

E) In your "Form Load" event, you will set the value of "DaFolder" to wherever you are going to store the .wav files. Make sure you set the full pathname!

F) OK, so that was all old stuff. There wasn't one thing on there that we haven't done before. Now for the new stuff! We are going to make use of a property of your combobox called "SelectedIndex". The items in a ComboBox are listed the same way as the elements in an array: they start at ZERO. So it's pretty easy to use this to select a certain element from an array, say an Array of song names. Here's how I did it in the button click event:

'make sure that a song has been chosen
If ComboBox1.SelectedIndex < 0 Then
MsgBox("Please choose a song!")
Else
'set up default VB wav player
Dim Sound As New System.Media.SoundPlayer( )
'establish where the sound folder is
'and then pick a sound from that folder by the array
MySong = Songs(ComboBox1.SelectedIndex)
Sound.SoundLocation = DaFolder & MySong
'load and then play the sound!
Sound.Load( )
Sound.Play( )

OK, now here's the tricky part: I couldn't upload the song files, because they are too big to fit on Box.net. I will try to put them in the H drive, but failing that, I have them on a couple of thumb drives

Cheers,
Mr. L

PS there will be second part of this lesson -- be looking for it!

No comments: