0 Replies - 1671 Views - Last Post: 07 March 2009 - 09:23 PM Rate Topic: -----

#1 Tertitten  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 02-March 09

Treview Items to Listview Items with extracting paths and more informa

Post icon  Posted 07 March 2009 - 09:23 PM

I'm working on a simple MP3 player and are stucked on some Treview.Click and Listview.Items.Add problems which I cant seem to get past.

What I'm trying to do is first with a button launch a FolderDialog to get folder path, when this is done I add the directories and subdirectories to tvFolders1 (Treview)
For the whole procedure for this see classes LoadFolderTree(), LoadDir() and tvFolders1_AfterExpand.
This seems to work just fine and no problems as far as I can see

The we add a feature where user click on the Treeview (tvFolders1) and we are supposed to do the following:
On tvFolders1_AfterSelect (when the user actually select something)
I am the trying to add Items and subitems to the lvFiles control (Listview)

Then we go trough the whole procedure of adding:
- Items = File Names
-- SubItems X 4 = ID3 info which we get from
			Me.lvFiles.Items.Add(FileIO.FileSystem.GetName(a))
		Next
		Dim _N As Integer
		With Player
			For _N = 0 To .currentPlaylist.count - 1
				lvFiles.Items(_N).SubItems.Add((Player.currentPlaylist.Item(_N).getItemInfo("Title")))

See the Class
Private Sub tvFolders1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tvFolders1.AfterSelect

For all the details.

Now adding items and subbitems goes just fine if I:
1. Do not clear the ListView lvFiles in the Class tvFolders1_AfterSelect
But I need that, I want the listview to be cleared before adding new items/subitems or we end up with an enormous list
2. Only add more items to the ListView without doing anything more

What happens:
If I clear the ListView lvFiles in the class tvFolders1_AfterSelect the application will simply freeze and crash after clicking on any item in TreeView tvFolders1

So basically somewhere in my code there is something causing this and that's were I need help, I where hoping someone could figure out what I need to do
I have pasted the whole code for that form because I suspect the problems are not in the adding/clicking on items but somewhere else, I do not know though.

There is also a "bug/another big problem" in the adding of items/subitems to the lvFiles ListView and that is that the first mp3 file only gets added with file/path and not ID3.

I would really appreciate some help with this problem, Thank You

Whole Code
		
Public Class frmMain

#Region "Color Settings"
	Dim CurrentTrackColor As System.Drawing.Color = Color.Red
	Dim PausedTrackColor As System.Drawing.Color = Color.LightYellow
#End Region
	Dim WithEvents Player As New WMPLib.WindowsMediaPlayer
	Dim files As Collections.ObjectModel.ReadOnlyCollection(Of String)
	Dim folders As Collections.ObjectModel.ReadOnlyCollection(Of String)
	Dim titles As New List(Of String)
	Dim CurrentPlaying As Integer = 0
	Dim PreviouslyPlaying As Integer = 0


	Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
		' Dispose of player
		Player = Nothing
	End Sub

	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		' chSource.Width = lvFiles.Width - 20
		But_Pause.Enabled = False
		But_Stop.Enabled = False
		But_Previous.Enabled = False
		But_Next.Enabled = False
		files = FileIO.FileSystem.GetFiles(My.Computer.FileSystem.SpecialDirectories.MyMusic, FileIO.SearchOption.SearchAllSubDirectories, "*.mp3")
		Volume.Value = Player.settings.volume
		Me.Txt_TrackName.Text = Player.URL
		Player.settings.autoStart = False
		'Player.URL = files(0)
		Player.enableContextMenu = False
		'With Me.Timer1
		'.Interval = 500
		' .Start()
		'.Enabled = True
		'End With
	End Sub

	Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
		' Form is closing, so shutdown player
		Player.close()
	End Sub

	Private Sub ClickedOnPlayButton(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles But_Play.Click
		With Me.Timer1
			.Interval = 500
			.Start()
			.Enabled = True
		End With
		GUIMode("Play")
		updatePlayer()
		Player.controls.play()
	End Sub

	Private Sub ClickedonstopNutton(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles But_Stop.Click
		Timer1.Enabled = False
		Player.controls.stop()
		GUIMode("Stopped")
	End Sub

	Private Sub ClickedonpauseButton(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles But_Pause.Click
		If Player.playState = WMPLib.WMPPlayState.wmppsPaused Then
			GUIMode("Play")
		Else
			GUIMode("Paused")
		End If
	End Sub

	Private Sub GUIMode(ByRef Guimode As String)
		Select Case Guimode
			Case "Play"
				' Put GUI in playing mode guise
				Player.controls.play()
				But_Pause.BackColor = System.Drawing.SystemColors.Control
				But_Pause.Enabled = True
				But_Stop.Enabled = True
				But_Play.Enabled = True
			Case "Paused"
				' put gui in paused mode guise
				But_Pause.Enabled = True
				But_Stop.Enabled = False
				But_Play.Enabled = False
				But_Pause.BackColor = PausedTrackColor
				Player.controls.pause()
			Case "Stopped"
				But_Pause.Enabled = False
				But_Stop.Enabled = False

		End Select
	End Sub
#Region "Handles the playing of files, Volume ctrl, Progress & State changes"

	Private Sub ScrollingVolume(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Volume.Scroll
		' Change the player's volume
		Player.settings.volume = Volume.Value
	End Sub

	Private Sub Player_MediaError(ByVal pMediaObject As Object) Handles Player.MediaError
		MessageBox.Show("Unrecoverable Problem. Shutting Down", "MyMusic Player")
		Me.Close()
	End Sub

	Private Sub Player_PlayStateChange(ByVal NewState As Integer) Handles Player.PlayStateChange
		Static Dim PlayAllowed As Boolean = True
		Select Case CType(NewState, WMPLib.WMPPlayState)
			Case WMPLib.WMPPlayState.wmppsReady
				If PlayAllowed Then
					Player.controls.play()
				End If
			Case WMPLib.WMPPlayState.wmppsMediaEnded
				' Reach end of track move onto next, looping around
				PreviouslyPlaying = CurrentPlaying
				CurrentPlaying = (CurrentPlaying + 1) Mod files.Count
				' Start protection (without it next wouldn't play
				PlayAllowed = False
				' Play track
				Player.URL = files(CurrentPlaying)
				Player.controls.play()
				' End Protection
				PlayAllowed = True
				updatePlayer()
		End Select

	End Sub

	Private Sub updatePlayer()
		' Display track name
		Txt_TrackName.Text = Player.currentMedia.name
		' Update TrackPostion
		With TrackPosition
			.Minimum = 0
			.Maximum = CInt(Player.currentMedia.duration)
			.Value = CInt(Player.controls.currentPosition())
		End With
		' Display Current Time Position and Duration
		Txt_Progress.Text = Player.controls.currentPositionString & vbTab & Player.currentMedia.durationString
		' Set Volume slide to match current volume
		Volume.Value = Player.settings.volume
		' Is the CurrentPlaying Track No. is different to the Previous Track number.
		If CurrentPlaying <> PreviouslyPlaying Then
			' Yes, 
			' Set the forecolor of the corrisponding track, assiociated with the previous playing track, with the control color
			lvFiles.Items(PreviouslyPlaying).ForeColor = System.Drawing.SystemColors.ControlText
		End If
		' Set the forecolor of the corrisponding track, assiociated with the currently playing track, with the current track color
		lvFiles.Items(CurrentPlaying).ForeColor = CurrentTrackColor

	End Sub
#End Region

	Private Sub Tracks_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lvFiles.MouseDoubleClick
		GUIMode("Play")

		' A track in the tracklisting has been double clicked on
		PreviouslyPlaying = CurrentPlaying
		' Set CurrentPlaying to position of selected track.
		CurrentPlaying = lvFiles.SelectedIndices(0)
		' Play the track
		Player.URL = files(CurrentPlaying)
		updatePlayer()
		Player.controls.play()
	End Sub

	Private Sub ScrollingTrackPosition(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackPosition.Scroll
		' Seek the through track
		Player.controls.pause()
		Player.controls.currentPosition = TrackPosition.Value
		Player.controls.play()
		updatePlayer()
		' Allow the app to do some processing
		Application.DoEvents()
	End Sub

	Private Sub UpdatePlayerTimer(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
		updatePlayer()
	End Sub

	Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
		Dim id3folder As String
		folderDialog.ShowDialog()
		id3folder = folderDialog.SelectedPath()
		Player.newPlaylist("defualt", "c:\\")
		files = FileIO.FileSystem.GetFiles(id3folder, FileIO.SearchOption.SearchAllSubDirectories, "*.mp3")
		For Each a As String In files
			Dim _NewSong As WMPLib.IWMPMedia = Player.newMedia(FileIO.FileSystem.GetParentPath(a) + "\" + FileIO.FileSystem.GetName(a))
			Player.currentPlaylist.appendItem(_NewSong)
			Me.lvFiles.Items.Add(FileIO.FileSystem.GetName(a))
		Next
		Dim _N As Integer
		With Player
			For _N = 0 To .currentPlaylist.count - 1
				lvFiles.Items(_N).SubItems.Add((Player.currentPlaylist.Item(_N).getItemInfo("Title")))
				lvFiles.Items(_N).SubItems.Add((Player.currentPlaylist.Item(_N).getItemInfo("Artist")))
				lvFiles.Items(_N).SubItems.Add((Player.currentPlaylist.Item(_N).getItemInfo("Album")))
				lvFiles.Items(_N).SubItems.Add((Player.currentPlaylist.Item(_N).getItemInfo("Genre")))
				Application.DoEvents()
			Next
		End With
	End Sub


	Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
		Dim LibraryFolder As String
		folderDialog.ShowDialog()
		LibraryFolder = folderDialog.SelectedPath()
		LoadFolderTree(LibraryFolder)
	End Sub

#Region "Handling loading of Folders into trFolders, Handling loading of files from tvFolders to lvFiles Listview"
	Friend WithEvents tvFolders1 As System.Windows.Forms.TreeView
	Public Sub LoadFolderTree(ByVal path As String)
		Dim basenode As System.Windows.Forms.TreeNode
		If IO.Directory.Exists(path) Then
			If path.Length <= 3 Then
				basenode = tvFolders1.Nodes.Add(path)
			Else
				basenode = tvFolders1.Nodes.Add(path)
			End If
			basenode.Tag = path
			LoadDir(path, basenode)
		Else
			Throw New System.IO.DirectoryNotFoundException()
		End If
	End Sub

	Private Sub tvFolders1_AfterExpand(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tvFolders1.AfterExpand
		Dim n As System.Windows.Forms.TreeNode
		For Each n In e.Node.Nodes
			LoadDir(n.Tag, n)
		Next
	End Sub

	Public Sub LoadDir(ByVal DirPath As String, ByVal Node As Windows.Forms.TreeNode)
		On Error Resume Next
		Dim Dir As String
		Dim Index As Integer
		If Node.Nodes.Count = 0 Then
			For Each Dir In My.Computer.FileSystem.GetDirectories(DirPath)
				Index = Dir.LastIndexOf("\")
				Node.Nodes.Add(Dir.Substring(Index + 1, Dir.Length - Index - 1))
				Node.LastNode.Tag = Dir
				Node.LastNode.ImageIndex = 0
			Next
		End If
	End Sub


	Private Sub tvFolders1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tvFolders1.AfterSelect
		Dim id3folder As String
		lvFiles.Items.Clear()
		id3folder = e.Node.FullPath()
		Player.newPlaylist("Changed", "c:\")
		files = FileIO.FileSystem.GetFiles(id3folder, FileIO.SearchOption.SearchAllSubDirectories, "*.mp3")
		For Each a As String In files
			Dim _NewSong As WMPLib.IWMPMedia = Player.newMedia(FileIO.FileSystem.GetParentPath(a) + "\" + FileIO.FileSystem.GetName(a))
			Player.currentPlaylist.appendItem(_NewSong)
			Me.lvFiles.Items.Add(FileIO.FileSystem.GetName(a))
		Next
		Dim _N As Integer
		With Player
			For _N = 0 To .currentPlaylist.count - 1
				lvFiles.Items(_N).SubItems.Add((Player.currentPlaylist.Item(_N).getItemInfo("Title")))
				lvFiles.Items(_N).SubItems.Add((Player.currentPlaylist.Item(_N).getItemInfo("Artist")))
				lvFiles.Items(_N).SubItems.Add((Player.currentPlaylist.Item(_N).getItemInfo("Album")))
				lvFiles.Items(_N).SubItems.Add((Player.currentPlaylist.Item(_N).getItemInfo("Genre")))
				Application.DoEvents()
			Next
		End With
	End Sub

#End Region

This post has been edited by Tertitten: 07 March 2009 - 09:31 PM


Is This A Good Question/Topic? 0
  • +

Page 1 of 1