Tuesday, August 5, 2008

Play .wav/.mp3(Songs)/Vedio(Movies)

Hello Friends in this article we will see three things

1. How to play “.wav” Files?
2. How to play “.mp3” Files? (i.e. How to play Songs?)
3. How to play Video Files? (i.e. How to play Movies?)

First we will see to play wave files (i.e. “.wav” files)(U guys can found some of the .wav files on your system as well they are mostly in “C:\Windows\Media” in this folder U can found all the sound made by your Windows Operating system while doing diff. things.)

Create a new Project by clicking on FILE --> NEW PROJECT and selecting a new Windows Project.

A new Form will come in front of U. Now we need to add few Controls to it,

1. Two Button control. (btnPlay, btnOpen)
2. One Label Control. (Label1)
3. One Text Box Control. (txtFileNm)
4. One OpenFileDialog Control. (OpenFileDialog1)

Make Your Form Look like mine



Make the Filter Property of OpenFileDialoge to “WAV Files|*.wav”

Now just paste the code on your code window.
Imports System.Media

Public Class Form1

Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnopen.Click

If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
txtFileNm.Text = OpenFileDialog1.FileName
End If

End Sub

Private Sub btnPlay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlay.Click

If txtFileNm.Text <> String.Empty Then
Dim wavPlayer As SoundPlayer = New SoundPlayer()
wavPlayer.SoundLocation = txtFileNm.Text
AddHandler wavPlayer.LoadCompleted, AddressOf wavPlayer_LoadCompleted
wavPlayer.LoadAsync()
End If
End Sub

Private Sub wavPlayer_LoadCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
CType(sender, System.Media.SoundPlayer).Play()
End Sub
End Class


Now click on to browse button u will see the similar browsing dialog box which we normal see when we browse some file in windows.
Now just select any .wav file u have or u can found some in the folder I have already told U .Select the file and click Ok button and then hit the play button to here the sound.


____________________________________________________________________________

Now here is the thing for which most of U guys are bearing for this long i.e how to Play a song using your application or how to play “.mp3” file.

This one is simpler; create a new project as told earlier

To a new Form we add few Controls,

1. One Button control. (btnOpen/Play)
2. One OpenFileDialog Control. (OpenFileDialog1)
3. One WindowsMediaPlayer Control.

Make Your Form Look like mine




Note : This windowsMediaPlayer u can not find in your tool box for that you have to do the following

1. Right click on to the tool Box and click on to the choose items (As shown below.)




Now after a weight of few seconds new window (as shown below) will pop up.



On to this select the COM Component (Windows Media Player) (as shown above) and hit OK.

Then a new windows media player control is ready to be added to your Project . Now just double click on to the control and it will be added to your project.

Now just paste the code in your project and see(I mean hear) the magic.


Imports System.Media
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Const DATA_FILE_EXTENSION As String = ".mp3"
Dim dlgFileDialog As New OpenFileDialog
With dlgFileDialog
.Filter = DATA_FILE_EXTENSION & _
" files (*" & DATA_FILE_EXTENSION & "|*" & DATA_FILE_EXTENSION
.FilterIndex = 1
.RestoreDirectory = True
If .ShowDialog() = Windows.Forms.DialogResult.OK Then
'Play the sound file
Me.AxWindowsMediaPlayer1.URL = dlgFileDialog.FileName
End If
End With

End Sub


End Class

_____________________________________________________________________________-

In this portion we will see how to play a movie using vb.net

Just create a new project as told earlier

This will be the simplest .

On to the form add a single button hence I am not giving u the visual this time.
Now the new thing to this will be that u need to add the reference

Don’t panic every thing will be told here only have patience for some more time,just follow the steps

1. Click on the Project Menu --> Add Reference
2. New window will be open and now in .net Reference U can found “Microsoft.DirectX.AudioVedioPlayback” (As shown below)





Click OK to add the Reference to your project.

Now that’s it just pass the code and pass the path of your video file where required


Imports Microsoft.DirectX.AudioVideoPlayback
Public Class Form1
Dim WithEvents Vid As New Microsoft.DirectX.AudioVideoPlayback.Video("Your video file Path")


Public Sub Play()

If Vid Is Nothing Then Return
Vid.Play()

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Call Play()
End Sub
End Class

---------------------------------------------------------------------------------------------------------------------
Thanx for your patience ,in the end I would like to say that all these three projects can be done in one project but for the sake of simplicity I make it in three projects

With some more code and and some more user control one can have your very own media player.well this is not easy as I am saying but it can be done and in fact I am working on it as soon as it is done it will be on my Blog .

Till then HAPPY CODING and God Bless.

Friday, July 25, 2008

How to create user control

Hi Friends

Today we will see How to create your very own User Control ,

Just follow these few steps and in the end U will end up with your very own User Control With Your defined Properties and events

1.Create a new project by clicking on to

FILE --> NEW PROJECT

Now wait a second and read carefully here we are not going to create new Windows application we are here to create USER CONTROLS for your windows Application, hence select the option named WINDOWS CONTROL LIBRARY as shown below








2. As soon as you Click Ok you will see the Design Window will Look like this








This is the control on which we are going to put some controls to create a new Control

//*In this Example We will create Menu Control which will Contain buttons such as Add, Modify and more, But you can use any combination of controls u like.*//

3. Now put 6 Buttons on your Control and make your Control Look Like this




Note: I have put some Pictures on the Buttons, You can also have simple text

4. Now Dbl click on to your user control to go to Code window

Guys a bit of theory ,to your surprise at this moment when u haven’t written a single word in your code if you build your project (Before that U have to save it ) and register your component (Which we will see at the end of article) and when you use the component in your new product you will found that your control already have too many events to handle this is because this control inherits all the events and properties of the controls we used on it ,But since this is our Control So we want to have full control over it and that can be done by having our own properties and events .

a. How to have your own properties?

Properties can be given by simply writing that GET SET code a small example is given below

Public Property sanText() As String

Get

End Get

Set(ByVal Value As String)

End Set

End Property

Public Property sanLbl() As String

Get

End Get

Set(ByVal Value As String)

End Set

End Property



Note : I am not using any properties in this example ,this is just a example to show u that this is simple.

b. How to have your own Event?

First, the simplest way to create an event is to simply declare it in your class (in this case, your custom control class), like in the following code snippet

Public Class MenuControl

Public Event addclick()

End Class



As you can see, I have declared an event using the Event keyword-


Public Event addclick()


After adding a user control or modifying one, you have to rebuild your project in order to see the changes appear in the designer.


Adding Event Handlers


After rebuilding, you should see your user control appear in the "My User Controls" tab of the Toolbox. You can then drag an instance of this control to a form. Once you've done this, open the form in code view. In the upper left corner of the code screen, select the name of your control (in my case, it's MenuControl ) from the list. Then you'll be able to view all the events associated with that control in the drop-down list on the right.If you created an event called AddClick in your control, you'll see it in the list- you'll also see one called Click, which is a standard event associated with all Windows controls.


NOTE: You should not name your event "Click" or it will cause a conflict with the base class'

Click event.


If you want to create a handler for your custom AddClick event, you should select the name of the control from the code window, then the event, which will cause the IDE to automatically generate an event handler for you. You could also manually enter the event handler code, which would look like this:


Private Sub MenuControl_addclick() Handles MenuControl1.addclick

msgBox “Add click event is being Called.”

End Sub



The key portion to this is the "Handles" keyword. It tells VB.NET that this method will be invoked automatically by the runtime when your control fires its AddClick event. The method can have any name you like- it does not have to be "Menucontrol_Addclick ", although this is the usual convention. However, the method's "signature" must be the same as the event that it is handling- meaning that the number of arguments and types of arguments must be identical. If they are not, the IDE will tell you that the method signature does not match.


Raising (Firing) Events

At this point, we've created a User Control, added an event to that control, added that control to a form, and have implemented an event handler to handle our custom event. However, one thing is missing-currently , the event won't ever be fired! If you tried to test this,you may have noticed that the MessageBox call we placed in the method above is never invoked.In order for a control (or any class) to raise an event, it must use the "RaiseEvent" statement. Since we want this event to be raised when the button is clicked, we must add an event handler to the control which will handle the button's Click event and raise our event in turn.

The following code snippet demonstrates this:


Private Sub btnAdd_Click(ByVal sender

As System.Object, ByVal e As System.EventArgs)

Handles btnAdd.Click

RaiseEvent addclick()

End Sub


Remember that this method is added to the control, not the form! Note the Handles clause for this method- it's going to get the Click event for the button on the User Control and perform some operations if need be, then will fire (raise) our custom event. Don't forget to rebuild your project for the changes to appear to the forms using the control.


Declaring Events with Arguments


You can also pass arguments with events, just like with any method or function call. In order to do this, you would add the arguments you wanted to the Event declaration:

-----------------------

Public Event AddClickWithArgs(ByVal ClickTime as Date)

--------------------------

In order to raise this event, you would have to do something like this:

-----------------

RaiseEvent AddClickWithArgs(Now)

-----------------


5. How to register your Control so that u can use it on to your diff projects by creating instances

Writ Click on your Toolbox you will see the option Named CHOOSE ITEMS click on to it as shown below





















A form named CHOOSE TOOL BOX will appear on to the screen as shown below



















Click on to browse button to locate your directory where your Project is stored , in your Project

Just follow the link (I:\vbExampels\MenuControl\MenuControl\bin\Release)and you will found the dll file U created When you blid Your Project if it is not there it means it is you have not build your Component which can be done by clicking on to the Build Menu and selecting Build Component.Select this dll file and press ok the component come in the Chose toolbox item with it’s check box clicked.Now press Ok of this form And Your Control will be seen on to the tool box and is very much ready to use in diff projects as many times as required.



/*Hopefully this article will help u out in your journey to learn VB.NET*/