Programming guides for beginner...
Any comments are welcomed....
I hope it helps!!! Thanks for drop by...

Sunday, February 20, 2011

Getting all files in directory include sub directories in vb.net

 

     As from my previous tutorial on Simple Music Player, I had mentioned that the function that I wrote to getting all songs in the directory is not possible. Now, I had found out a method by using VB.Net Stack. VB.Net Stack is a collection that stores items that being push in. It uses the concept of Last In First Out (LIFO). Although this method list out all the files in directory, as for its’ LIFO system, it will list the files from the last. (Means the last to come will be the first to out.)

Here I includes the coding that need to be changes and also a function that used to get all files.

Private Sub ListView1_DragDrop(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DragEventArgs) _
Handles ListView1.DragDrop
Dim colFiles() As String = e.Data.GetData(DataFormats.FileDrop, True)
For i = 0 To colFiles.Count - 1
Dim list As List(Of String) = GetAllFiles.GetFileList(colFiles(i))
For Each path In list
ListView1.Items.Add(System.IO.Path.GetFileName(path))
ListView2.Items.Add(path)
Next
Next
End Sub

I use loop for data that drop into listview1 is to handle when there is more than one directory being drag into listview1.


 


And here, I includes the function that used to get all files in a directory which include all sub directories.

Public Class GetAllFiles

Public Shared Function GetFileList(ByVal Dirpath As String) As List(Of String)
Dim result As New List(Of String)
Dim store As New Stack(Of String)
store.Push(Dirpath)

Do While (store.Count > 0)
Dim dir As String = store.Pop
Try
result.AddRange(Directory.GetFiles(dir, "*.mp3*"))

'change here to load music file in other formats. Not all formats are supported.
Dim directoryName As String
For Each directoryName In Directory.GetDirectories(dir)
store.Push(directoryName)
Next
Catch ex As Exception
End Try
Loop
Return result
End Function

End Class

Credited to this site where I found this method. Thanks a lot.

Wednesday, February 9, 2011

Enhanced version of print screen program from previous post.

 

       When I am doing step-by-step tutorial on Simple Music Player, I used my previous print screen program. When using the program, I found out that there are still improvement that need to be done.

       I had added a new function which is editing the picture after the print screen with Microsoft Paint. This function can be activated by checking on the menu. Besides that, I have added features to auto save function. User will able to enter an integer for the saved pictures to be named.

For those who want to download and test the enhanced version, please visit http://programmingbeginner.zxq.net/download.php.

Tuesday, February 8, 2011

My first step-by-step tutorial on Simple Music Player (VS2008) Part III

 

     Continue from my previous post of Part I & Part II, this will be the last part of Simple Music Player. When you completed this part, you will be able to use the Music Player to play songs.

     The following coding in the “Check next songs” region is a function that used to check for next songs when current songs finish played. It also stops play when the playlist reached the end. The region for “timer start” is a function when a timer start ticking. The coding can also be approach by double click on timer1 that being added to the interface in Part I.

31

Your code should look like the above picture.

#Region "Check next songs"
    Private Sub Check_Next_Media()
        If int_currplayindex >= ListView1.Items.Count - 1 Then
            Timer1.Stop()
            objAudio.Stop()
            Button1.Text = "Play"
            b_musicEnding = True
            b_musicplaying = False
            Exit Sub
        End If
        b_musicEnding = False
        int_currplayindex += 1
        str_filepath = CStr(ListView2.Items.Item(int_currplayindex).Text)
        objAudio = New Audio(str_filepath, False)
        objAudio.Play()
    End Sub
#End Region

#Region "timer start"
    Private Sub Timer1_Tick(ByVal sender As System.Object, _
                            ByVal e As System.EventArgs) _
                            Handles Timer1.Tick
        ListView1.Items.Item(int_currplayindex).BackColor = Color.Blue
        'change the color here to show current playing music.
        If objAudio.CurrentPosition = objAudio.Duration Then
            b_musicEnding = True
        Else
            b_musicEnding = False
        End If
        If b_musicEnding = True Then
            ListView1.Items.Item(int_currplayindex).BackColor = clr_previous 'previous color
            Check_Next_Media()
        End If
    End Sub
#End Region

     After the the previous Part I & Part II, and also the above coding, you should be able to test your Player now. Press F5 to run the program. Now, you might faced a problem as shown in the picture below:


14



To solve this problem, you will need to change the exception.  Look for Debug | Exceptions. In Exceptions dialog, look for loaderlock in Managed Debugging Assistants and uncheck it at Thrown as shown in the picture below.


15


After that, you should have a working Music Player. If there is still problems, please feel free to leave a comment. I will try my best to help. Open-mouthed smile