Results 1 to 14 of 14

Thread: [2008] I have to pass strings through a hidden textbox before adding to listview

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2008
    Posts
    27

    [2008] I have to pass strings through a hidden textbox before adding to listview

    OK so I have some code which gets the title and artist from the ID3 tag of an mp3 file.

    Now the problem is, in order for these to properly show up in my listview, I have to pass them through a textbox first.

    Heres my code:

    Code:
            For Each filePath As String In IO.Directory.GetFiles(DirListBox1.Path, "*.mp3", IO.SearchOption.AllDirectories)
    
                Dim title As String = ""
                Dim artist As String = ""
                Dim buffer(128) As Byte
                Dim mp3File As New FileInfo(filePath)
    
                If mp3File.Length > 128 Then
    
                    Dim i As Integer
                    Dim mp3Reader As Stream = mp3File.OpenRead()
    
                    mp3Reader.Seek(-128, SeekOrigin.End)
    
                    For i = 0 To 127
    
                        buffer(i) = mp3Reader.ReadByte
    
                    Next
    
                    mp3Reader.Close()
    
                End If
    
                If Encoding.Default.GetString(buffer, 0, 3).Equals("TAG") Then
    
                    title = Trim(Encoding.Default.GetString(buffer, 3, 30))
                    artist = Trim(Encoding.Default.GetString(buffer, 33, 30))
    
                End If
    
                TextBox1.Text = title
                TextBox2.Text = artist
    
                fMain.ListView1.Items.Add(TextBox2.Text + " - " + TextBox1.Text).SubItems.Add(filePath)
    
            Next
    If I do not pass the strings through the textbox, they do not show up properly in the listview. Why is this happening?

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: [2008] I have to pass strings through a hidden textbox before adding to listview

    try this:

    vb Code:
    1. title = Trim(System.Text.Encoding.Default.GetString(buffer, 3, 30))
    2. artist = Trim(System.Text.Encoding.Default.GetString(buffer, 33, 30).Substring(0, System.Text.Encoding.Default.GetString(buffer, 33, 30).IndexOf(Chr(0))))

    the artist string wasn't trimming correctly

  3. #3
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [2008] I have to pass strings through a hidden textbox before adding to listview

    Hey,

    I did a quick google, and I came up with this post, seems like a neater solution.

    Just a thought.

    Gary

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Oct 2008
    Posts
    27

    Re: [2008] I have to pass strings through a hidden textbox before adding to listview

    Quote Originally Posted by .paul.
    try this:

    vb Code:
    1. title = Trim(System.Text.Encoding.Default.GetString(buffer, 3, 30))
    2. artist = Trim(System.Text.Encoding.Default.GetString(buffer, 33, 30).Substring(0, System.Text.Encoding.Default.GetString(buffer, 33, 30).IndexOf(Chr(0))))

    the artist string wasn't trimming correctly
    nah it still isn't working properly. i will just stick with the textboxes for now and hide them behind the dirlistbox i have on the same form.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Oct 2008
    Posts
    27

    Re: [2008] I have to pass strings through a hidden textbox before adding to listview

    Quote Originally Posted by gep13
    Hey,

    I did a quick google, and I came up with this post, seems like a neater solution.

    Just a thought.

    Gary
    that looks like it does the exact same thing as what my current code does?

  6. #6
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [2008] I have to pass strings through a hidden textbox before adding to listview

    Well yes, the difference being that the code I linked to works.

    I adapted the code in that link, and incorporated it with the code the you posted, and I got this:

    Code:
    Imports System.IO
    Imports System.Text
    
    Public Class Form4
    
        Private _Tag As String ' (3)
        Private _Songname As String ' (30)
        Private _Artist As String ' (30)
        Private _Album As String ' (30)
        Private _Year As String ' (4)
        Private _Comment As String ' (30)
        Private _Genre As String ' (1)
        Private _DetailsRead As Boolean
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            For Each filePath As String In IO.Directory.GetFiles(DirListBox1.Text, "*.mp3", IO.SearchOption.AllDirectories)
                Dim buffer(128) As Byte
                Dim mp3File As New FileInfo(filePath)
                Dim fs As New FileStream(filePath, FileMode.Open, FileAccess.Read)
                Dim br As New BinaryReader(fs)
    
                If mp3File.Length > 128 Then
    
                    br.BaseStream.Seek(-128, SeekOrigin.End)
    
                    _Tag = br.ReadChars(3)
    
                    If _Tag = "TAG" Then
                        ' Read the rest of the data
                        _Songname = br.ReadChars(30)
                        _Artist = br.ReadChars(30)
                        _Album = br.ReadChars(30)
                        _Year = br.ReadChars(4)
                        _Comment = br.ReadChars(30)
                        _Genre = br.ReadChars(1)
    
                        _DetailsRead = True
                    Else
                        _DetailsRead = False
                    End If
    
                    ' Cleanup
                    br.Close()
                    fs.Close()
                    br = Nothing
                    fs = Nothing
                End If
    
                ListView1.Items.Add(_Songname + " - " + _Artist).SubItems.Add(filePath)
    
            Next
        End Sub
    End Class
    And it works, without the need to go through textboxes.

    Gary

  7. #7
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [2008] I have to pass strings through a hidden textbox before adding to listview

    Bear in mind, that I wasn't sure what kind of control you were using for DirListBox1, so I just used a textbox in my sample code.

    Gary

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: [2008] I have to pass strings through a hidden textbox before adding to listview

    Quote Originally Posted by gep13
    Hey,

    I did a quick google, and I came up with this post, seems like a neater solution.

    Just a thought.

    Gary
    yep. that was neat

  9. #9
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: [2008] I have to pass strings through a hidden textbox before adding to listview

    Quote Originally Posted by KJDion
    nah it still isn't working properly. i will just stick with the textboxes for now and hide them behind the dirlistbox i have on the same form.
    What does "still isn't working" mean anyways? What are you getting that you shouldn't.... or what are you not getting that you should? Or are you getting an error?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Oct 2008
    Posts
    27

    Re: [2008] I have to pass strings through a hidden textbox before adding to listview

    oh i get it, it uses a binary reader

    yeah i was looking into binary readers earlier too

  11. #11
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [2008] I have to pass strings through a hidden textbox before adding to listview

    So, does that mean that you got it working?

    If so, remember to mark your thread as resolved.

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Oct 2008
    Posts
    27

    Re: [2008] I have to pass strings through a hidden textbox before adding to listview

    ok well that code you posted doesnt work either, gep13

    i mean it is not displaying the songname at all in the list

    code that does not work:

    Code:
        Private Sub bAddFolder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bAddFolder.Click
    
            For Each filePath As String In IO.Directory.GetFiles(DirListBox1.Path, "*.mp3", IO.SearchOption.AllDirectories)
    
                Dim tag As String ' (3)
                Dim song As String = "" ' (30)
                Dim artist As String = "" ' (30)
                Dim buffer(128) As Byte
                Dim mp3File As New FileInfo(filePath)
                Dim fs As New FileStream(filePath, FileMode.Open, FileAccess.Read)
                Dim br As New BinaryReader(fs)
    
                If mp3File.Length > 128 Then
    
                    br.BaseStream.Seek(-128, SeekOrigin.End)
                    tag = br.ReadChars(3)
    
                    If tag = "TAG" Then
    
                        song = br.ReadChars(30)
                        artist = br.ReadChars(30)
    
                    End If
    
                    br.Close()
                    fs.Close()
                    br = Nothing
                    fs = Nothing
    
                End If
    
                If Not artist.Equals("") And Not song.Equals("") Then
    
                    fMain.ListView1.Items.Add(artist + " - " + song + "(yes)").SubItems.Add(filePath)
    
                Else
    
                    fMain.ListView1.Items.Add(Path.GetFileNameWithoutExtension(filePath)).SubItems.Add(filePath)
    
                End If
    
            Next
    
        End Sub
    code that does work:

    Code:
        Private Sub bAddFolder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bAddFolder.Click
    
            For Each filePath As String In IO.Directory.GetFiles(DirListBox1.Path, "*.mp3", IO.SearchOption.AllDirectories)
    
                Dim title As String = ""
                Dim artist As String = ""
                Dim buffer(128) As Byte
                Dim mp3File As New FileInfo(filePath)
    
                If mp3File.Length > 128 Then
    
                    Dim i As Integer
                    Dim mp3Reader As Stream = mp3File.OpenRead()
    
                    mp3Reader.Seek(-128, SeekOrigin.End)
    
                    For i = 0 To 127
    
                        buffer(i) = mp3Reader.ReadByte
    
                    Next
    
                    mp3Reader.Close()
    
                End If
    
                If Encoding.Default.GetString(buffer, 0, 3).Equals("TAG") Then
    
                    title = Trim(Encoding.Default.GetString(buffer, 3, 30))
                    artist = Trim(Encoding.Default.GetString(buffer, 33, 30))
    
                End If
    
                TextBox1.Text = title
                TextBox2.Text = artist
    
                If Not TextBox1.Text.Equals("") And Not TextBox2.Text.Equals("") Then
    
                    fMain.ListView1.Items.Add(TextBox2.Text + " - " + TextBox1.Text).SubItems.Add(filePath)
    
                Else
    
                    fMain.ListView1.Items.Add(Path.GetFileNameWithoutExtension(filePath)).SubItems.Add(filePath)
    
                End If
    
            Next
    
        End Sub

  13. #13
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: [2008] I have to pass strings through a hidden textbox before adding to listview

    try this. the problem is again as i suggested earlier.

    vb Code:
    1. If tag = "TAG" Then
    2.  
    3.    song = br.ReadChars(30)
    4.    artist = br.ReadChars(30)
    5.    If song.Contains(Chr(0)) Then song = song.Substring(0, song.IndexOf(Chr(0)))
    6.    If artist.Contains(Chr(0)) Then artist = artist.Substring(0, artist.IndexOf(Chr(0)))
    7.  
    8. End If

  14. #14
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [2008] I have to pass strings through a hidden textbox before adding to listview

    Man, that was driving me crazy for a second there, was trying everything I could think of to get it to work

    Here is a completed version that uses Pauls code above, as well as the code in the linked posted:

    Code:
    Imports System.IO
    Imports System.Text
    
    Public Class Form4
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            For Each filePath As String In IO.Directory.GetFiles(DirListBox1.Text, "*.mp3", IO.SearchOption.AllDirectories)
                Dim details As New MP3Details()
                If details.ReadMP3Details(filePath) = True Then
                    Dim entry As String = String.Format("{0} - {1}", details.Artist, details.Songname)
                    ListView1.Items.Add(entry).SubItems.Add(filePath)
                End If
            Next
        End Sub
    End Class
    
    Public Class MP3Details
    
    #Region "Attributes"
    
        Private _Tag As String ' (3)
        Private _Songname As String ' (30)
        Private _Artist As String ' (30)
        Private _Album As String ' (30)
        Private _Year As String ' (4)
        Private _Comment As String ' (30)
        Private _Genre As String ' (1)
    
        Private _DetailsRead As Boolean
    
    #End Region
    
        Public Sub New()
    
        End Sub
    
        Public Function ReadMP3Details(ByVal Filename As String) As Boolean
    
            ' Initialise
            _DetailsRead = False
    
            ' Does the file exist?
            If Not New IO.FileInfo(Filename).Exists Then
                _DetailsRead = False
                Return False
            End If
    
            ' Reads the MP3 tag details
            Dim fs As New IO.FileStream(Filename, IO.FileMode.Open, IO.FileAccess.Read)
            Dim br As New IO.BinaryReader(fs)
    
            ' Move to the end of the stream - 127 places from the end...
            br.BaseStream.Seek(-128, IO.SeekOrigin.End)
    
            ' Begin reading the data
            _Tag = br.ReadChars(3)
    
            ' Does a tag exist?
            If _Tag = "TAG" Then
                ' Read the rest of the data
                _Songname = br.ReadChars(30)
                If _Songname.Contains(Chr(0)) Then
                    _Songname = _Songname.Substring(0, _Songname.IndexOf(Chr(0)))
                End If
                _Artist = br.ReadChars(30)
                If _Artist.Contains(Chr(0)) Then
                    _Artist = _Artist.Substring(0, _Artist.IndexOf(Chr(0)))
                End If
                _Album = br.ReadChars(30)
                _Year = br.ReadChars(4)
                _Comment = br.ReadChars(30)
                _Genre = br.ReadChars(1)
    
                _DetailsRead = True
            Else
                _DetailsRead = False
            End If
    
            ' Cleanup
            br.Close()
            fs.Close()
            br = Nothing
            fs = Nothing
    
            ' Return value
            Return _DetailsRead
        End Function
    
    #Region "Properities"
    
        Public ReadOnly Property Songname() As String
            Get
                If _DetailsRead Then Return Me._Songname.ToString
            End Get
        End Property
    
        Public ReadOnly Property Artist() As String
            Get
                If _DetailsRead Then Return Me._Artist.ToString
            End Get
        End Property
    
        Public ReadOnly Property Album() As String
            Get
                If _DetailsRead Then Return Me._Album.ToString
            End Get
        End Property
    
        Public ReadOnly Property Year() As String
            Get
                If _DetailsRead Then Return Me._Year.ToString
            End Get
        End Property
    
        Public ReadOnly Property Comment() As String
            Get
                If _DetailsRead Then Return Me._Comment.ToString
            End Get
        End Property
    
        Public ReadOnly Property Genre() As String
            Get
                If _DetailsRead Then Return Me._Genre.ToString
            End Get
        End Property
    
    #End Region
    
    End Class
    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width