Hello and welcome.
In this lecture we are going to add the code to make the music player work.
All right.
First thing I want to do a sum generate an event handler for each of these buttons.
Basically an event handler is used to response to an event an event basically something an action that
is performed on an application.
When the application is running for example a click is an event.
So the correspondent corresponding event handler would be a click event handler.
So let's start with the brass button.
So double click on that.
And that would generate a stop of the code is top meaning it just generates an empty method or empty
function.
So we have to write the code in between these curly braces.
You can see them the cursor is flashing waiting for us to start writing the code.
What I am going to do here I'm going to use an if statement just to check or to check the dialog box
to see if the file has been open.
When you click on the browse button so would type in if we just to the intelligence skip not the guide.
So in the if statement we added an open file dialog box so I'm gonna pass that into the if statement.
So it's a lowercase o.
So this is the look.
This will now become the ope.
This is the open file a box we added earlier on to our form.
So we'll need to do here.
We need to attach a method to it that will use to display the box the open dialogue.
So if you press dot or period it gives you a list of methods you can use if you type in show.
It would show you this show dialog box.
So this show dialog is a method every method must be enclosed in parentheses.
So it tells you this this is a sure dialog box.
So what we need to do is complaining at the moment because I've not finished writing the code.
So what I need to do I need to set a need to way for the if statement to check something.
So to check or evaluate you do double equals two.
And I wanted to check the dialog there.
So this is dialogue results.
And I want to attach a value to it and you can attach and poke.
So if you're not sure what any parts of the code does just place your mouse over it and the idea will
illustrate or tell you give you a tool tip guide us to what that code does it tells you this is a method
a wrongs a common dialog box with a default owner.
OK.
That tells you that that is an open file open file dialog for form one form one is the is the form that
this dialogue is attached to and the.
Okay here the dialog box returns value is okay.
Okay tells you usually sent from a button labeled okay.
Okay in this instance is the button is not a hook not code okay by gives you a guide.
What that what is this method does not is I've got this red squiggly line here because it's complaining
that I have not finished the code telling me that this is an invalid expression.
So it's telling me you know it's expecting because if the code is finished I should put a semicolon
there but it's not finished.
Is an if statement when you are using the if statement you get to you have to specify the code that
will run for that if statement.
So to do that I need to specify blank curly braces just expand that in between the curly braces is where
I will write the code that will be activated in response to this if statement.
So what I'm going to do I'm going to reference the text box.
We added a text box earlier whether the text box 1.
So dot text I'm going to attach a text property to it.
All right.
And I'm gonna set that value to equals to the open dialogue.
I'm going to set it to equals to that and I'm going to attach a file name to that.
All right.
So if you put your mouse over there to tell you what that does is say it gets all sets the string containing
the file names selected in the file dialog box if you put your mouse over that tells you what that does
tells you what that does.
Okay.
So if you're not sure and just put your mouse over there and give you two tip guide the next thing you
want to do when you are using a music player you need to specify a path where the files will be picked
up from.
So I'm going to reference r music player.
Remember we declared it.
We called it.
I changed the name to this.
So you put your mouse over it I tells you that's the music player.
So I'm going to references and you need to specify a U.R.L. path that is a part for where the music
files is coming from.
So I'm going to set that to equals to the text box.
So that means it will get the location of the music and displayed in the text box area.
Okay.
Is gonna displayed as a text that's basically what that is saying.
So that's the code done for this button.
I just save that and go back in to our designer and click on the play button.
It would generate a blank again generated as top of the event handler.
So in between this curly braces I need to specify the code because this is a play it's going to want
when the button is clicked it will just play the music.
This is quite easy to do.
So again I reference the music player which is this fair ball here.
And how attach a control v just to you can do the control.
So there's a control called this just double click on it and then I'll attach a method to it a play
method.
So these are various metals you can attach to one on one for this is just the play method.
So that's a play method and yes to a semicolon and that's it.
So this single line of code is responsible for playing the music.
So I'll save that go back into a designer and do the same for the pause.
This is the event handler for the polls.
I'm gonna save time.
Just copy
paste it here and just change the method to post
code that's a lowercase
OK.
Go back to the control.
Save that.
Go back.
Double click on the stop button generates a stop of the event handler paste same thing
and just type in a stop notice as a stop method.
That's all I need to do for that.
Come here and grab the exit.
This is different.
I don't need to call the player.
All I need to do is attach a close.
There's a method called Close.
So I want when the exit button is clicked it will close the application.
So our code is basically finished.
So click save and let's test the application.
So to test we start debugging.
We click on Start and just wait for it to load.
All right.
It has loaded so click on the browser.
It should give us a dialog box.
Excellent.
So we need to go to music.
You can click any music files by the way.
I'm just gonna click on the default music sample music that Microsoft provided with the operating system.
I just try the first one.
I'm not sure if you can hear that this plane okay.
That's a play you can see the path is this plane the.
I can click on Pulse play then click on stop and then exit.
Excellent.
So we have successfully created a music player.
Thank you so much for your time.
Take care.
Bye for now.