vbAccelerator - Contents of code file: frmTestMp3Tags.frm

This file is part of the download MP3 ID3 Tags Code, which is described in the article Reading and Writing MP3 ID3v1 and ID3v2 Tags.

VERSION 5.00
Begin VB.Form frmTestMp3Tags 
   Caption         =   "vbAccelerator MP3 ID3v1 and ID3v2 Tag Reader and Writer"
   ClientHeight    =   9555
   ClientLeft      =   4155
   ClientTop       =   2220
   ClientWidth     =   8160
   BeginProperty Font 
      Name            =   "Tahoma"
      Size            =   8.25
      Charset         =   0
      Weight          =   400
      Underline       =   0   'False
      Italic          =   0   'False
      Strikethrough   =   0   'False
   EndProperty
   Icon            =   "frmTestMp3Tags.frx":0000
   LinkTopic       =   "Form1"
   OLEDropMode     =   1  'Manual
   ScaleHeight     =   9555
   ScaleWidth      =   8160
   Begin VB.PictureBox picID3v2 
      BorderStyle     =   0  'None
      Height          =   5715
      Left            =   960
      ScaleHeight     =   5715
      ScaleWidth      =   6735
      TabIndex        =   17
      TabStop         =   0   'False
      Top             =   3240
      Width           =   6735
      Begin VB.ComboBox cboGenreV2 
         Height          =   315
         Left            =   900
         Sorted          =   -1  'True
         TabIndex        =   26
         Top             =   1380
         Width           =   1815
      End
      Begin VB.TextBox txtCommentV2 
         Height          =   1335
         Left            =   900
         MultiLine       =   -1  'True
         ScrollBars      =   2  'Vertical
         TabIndex        =   30
         Top             =   2100
         Width           =   5835
      End
      Begin VB.TextBox txtYearV2 
         Height          =   315
         Left            =   900
         MaxLength       =   4
         TabIndex        =   28
         Top             =   1740
         Width           =   1815
      End
      Begin VB.TextBox txtAlbumV2 
         Height          =   315
         Left            =   900
         TabIndex        =   24
         Top             =   1020
         Width           =   5835
      End
      Begin VB.TextBox txtTitleV2 
         Height          =   315
         Left            =   900
         TabIndex        =   22
         Top             =   660
         Width           =   5835
      End
      Begin VB.TextBox txtArtistV2 
         Height          =   315
         Left            =   900
         TabIndex        =   20
         Top             =   300
         Width           =   5835
      End
      Begin VB.TextBox txtTrack 
         Height          =   315
         Left            =   900
         MaxLength       =   4
         TabIndex        =   32
         Top             =   3480
         Width           =   1875
      End
      Begin VB.TextBox txtOriginalArtist 
         Height          =   315
         Left            =   900
         TabIndex        =   34
         Top             =   3840
         Width           =   5835
      End
      Begin VB.TextBox txtEncodedBy 
         Height          =   315
         Left            =   900
         TabIndex        =   40
         Top             =   4920
         Width           =   5835
      End
      Begin VB.TextBox txtLinkTo 
         Height          =   315
         Left            =   900
         TabIndex        =   42
         Top             =   5280
         Width           =   5835
      End
      Begin VB.TextBox txtComposer 
         Height          =   315
         Left            =   900
         TabIndex        =   36
         Top             =   4200
         Width           =   5835
      End
      Begin VB.TextBox txtCopyright 
         Height          =   315
         Left            =   900
         TabIndex        =   38
         Top             =   4560
         Width           =   5835
      End
      Begin VB.Label lblInfo 
         Caption         =   "Comment:"
         Height          =   255
         Index           =   6
         Left            =   60
         TabIndex        =   29
         Top             =   2160
         Width           =   795
      End
      Begin VB.Label lblInfo 
         Caption         =   "Year:"
         Height          =   255
         Index           =   7
         Left            =   60
         TabIndex        =   27
         Top             =   1800
         Width           =   795
      End
      Begin VB.Label lblInfo 
         Caption         =   "Genre:"
         Height          =   255
         Index           =   8
         Left            =   60
         TabIndex        =   25
         Top             =   1440
         Width           =   795
      End
      Begin VB.Label lblInfo 
         Caption         =   "Album:"
         Height          =   255
         Index           =   9
         Left            =   60
         TabIndex        =   23
         Top             =   1080
         Width           =   795
      End
      Begin VB.Label lblInfo 
         Caption         =   "Title:"
         Height          =   255
         Index           =   10
         Left            =   60
         TabIndex        =   21
         Top             =   720
         Width           =   855
      End
      Begin VB.Label lblInfo 
         Caption         =   "Artist:"
         Height          =   255
         Index           =   11
         Left            =   60
         TabIndex        =   19
         Top             =   360
         Width           =   795
      End
      Begin VB.Label lblInfo 
         Caption         =   "Track:"
         Height          =   255
         Index           =   12
         Left            =   60
         TabIndex        =   31
         Top             =   3540
         Width           =   795
      End
      Begin VB.Label lblInfo 
         Caption         =   "Org. Art.:"
         Height          =   255
         Index           =   13
         Left            =   60
         TabIndex        =   33
         Top             =   3900
         Width           =   795
      End
      Begin VB.Label lblInfo 
         Caption         =   "Encoded:"
         Height          =   255
         Index           =   14
         Left            =   60
         TabIndex        =   39
         Top             =   4980
         Width           =   795
      End
      Begin VB.Label lblInfo 
         Caption         =   "Link To:"
         Height          =   255
         Index           =   15
         Left            =   60
         TabIndex        =   41
         Top             =   5340
         Width           =   795
      End
      Begin VB.Label lblInfo 
         Caption         =   "Composer:"
         Height          =   195
         Index           =   16
         Left            =   60
         TabIndex        =   35
         Top             =   4260
         Width           =   795
      End
      Begin VB.Label lblInfo 
         Caption         =   "Copyright: (Yeah, Right)"
         Height          =   195
         Index           =   17
         Left            =   60
         TabIndex        =   37
         Top             =   4620
         Width           =   795
      End
      Begin VB.Label lblID3V2 
         BackColor       =   &H80000010&
         Caption         =   " ID3 v2 Tag Information"
         ForeColor       =   &H80000014&
         Height          =   255
         Left            =   0
         TabIndex        =   18
         Top             =   0
         Width           =   7515
      End
   End
   Begin VB.PictureBox picID3v1 
      BorderStyle     =   0  'None
      Height          =   2595
      Left            =   960
      ScaleHeight     =   2595
      ScaleWidth      =   6735
      TabIndex        =   3
      TabStop         =   0   'False
      Top             =   480
      Width           =   6735
      Begin VB.TextBox txtArtist 
         Height          =   315
         Left            =   900
         MaxLength       =   30
         TabIndex        =   6
         Top             =   300
         Width           =   5835
      End
      Begin VB.TextBox txtTitle 
         Height          =   315
         Left            =   900
         MaxLength       =   30
         TabIndex        =   8
         Top             =   660
         Width           =   5835
      End
      Begin VB.TextBox txtAlbum 
         Height          =   315
         Left            =   900
         MaxLength       =   30
         TabIndex        =   10
         Top             =   1020
         Width           =   5835
      End
      Begin VB.TextBox txtYear 
         Height          =   315
         Left            =   900
         MaxLength       =   4
         TabIndex        =   14
         Top             =   1740
         Width           =   1815
      End
      Begin VB.TextBox txtComment 
         Height          =   315
         Left            =   900
         MaxLength       =   30
         TabIndex        =   16
         Top             =   2100
         Width           =   5835
      End
      Begin VB.ComboBox cboGenre 
         Height          =   315
         Left            =   900
         Sorted          =   -1  'True
         Style           =   2  'Dropdown List
         TabIndex        =   12
         Top             =   1380
         Width           =   1815
      End
      Begin VB.Label lblInfo 
         Caption         =   "Artist:"
         Height          =   255
         Index           =   0
         Left            =   60
         TabIndex        =   5
         Top             =   360
         Width           =   795
      End
      Begin VB.Label lblInfo 
         Caption         =   "Title:"
         Height          =   255
         Index           =   1
         Left            =   60
         TabIndex        =   7
         Top             =   720
         Width           =   855
      End
      Begin VB.Label lblInfo 
         Caption         =   "Album:"
         Height          =   255
         Index           =   2
         Left            =   60
         TabIndex        =   9
         Top             =   1080
         Width           =   795
      End
      Begin VB.Label lblInfo 
         Caption         =   "Genre:"
         Height          =   255
         Index           =   3
         Left            =   60
         TabIndex        =   11
         Top             =   1440
         Width           =   795
      End
      Begin VB.Label lblInfo 
         Caption         =   "Year:"
         Height          =   255
         Index           =   4
         Left            =   60
         TabIndex        =   13
         Top             =   1800
         Width           =   795
      End
      Begin VB.Label lblInfo 
         Caption         =   "Comment:"
         Height          =   255
         Index           =   5
         Left            =   60
         TabIndex        =   15
         Top             =   2160
         Width           =   795
      End
      Begin VB.Label lblID3v1 
         BackColor       =   &H80000010&
         Caption         =   " ID3 v1 Tag Information"
         ForeColor       =   &H80000014&
         Height          =   255
         Left            =   0
         TabIndex        =   4
         Top             =   0
         Width           =   7515
      End
   End
   Begin VB.CommandButton cmdSet 
      Caption         =   "&Set"
      Enabled         =   0   'False
      Height          =   375
      Left            =   900
      TabIndex        =   43
      Top             =   9060
      Width           =   1155
   End
   Begin VB.CommandButton cmdPick 
      Caption         =   "..."
      Height          =   315
      Left            =   7740
      TabIndex        =   2
      Top             =   60
      Width           =   375
   End
   Begin VB.TextBox txtMp3File 
      BackColor       =   &H8000000F&
      Height          =   315
      Left            =   960
      Locked          =   -1  'True
      OLEDropMode     =   1  'Manual
      TabIndex        =   1
      Top             =   60
      Width           =   6735
   End
   Begin VB.Label lblMp3File 
      Caption         =   "MP3 File"
      Height          =   255
      Left            =   60
      TabIndex        =   0
      Top             =   120
      Width           =   855
   End
   Begin VB.Menu mnuFileTOP 
      Caption         =   "&File"
      Begin VB.Menu mnuFile 
         Caption         =   "&Open..."
         Index           =   0
         Shortcut        =   ^O
      End
      Begin VB.Menu mnuFile 
         Caption         =   "&Save"
         Index           =   1
         Shortcut        =   ^S
      End
      Begin VB.Menu mnuFile 
         Caption         =   "-"
         Index           =   2
      End
      Begin VB.Menu mnuFile 
         Caption         =   "E&xit"
         Index           =   3
      End
   End
End
Attribute VB_Name = "frmTestMp3Tags"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Private m_cID3v1 As New cMP3ID3v1
Private m_cID3v2 As New cMP3ID3v2

Private Sub ParseCommand(ByVal sCmd As String)
Dim sC As String
Dim bInQuote As Boolean
Dim bOk As Boolean
Dim sParsed As String
Dim i As Long

   For i = 1 To Len(sCmd)
      sC = Mid(sCmd, i, 1)
      If (sC = """") Then
         If (bInQuote) Then
            bInQuote = False
            bOk = True
            Exit For
         Else
            bInQuote = True
         End If
      ElseIf (sC = " ") Then
         If (bInQuote) Then
            sParsed = sParsed & sC
         Else
            bOk = True
            Exit For
         End If
      Else
         sParsed = sParsed & sC
      End If
   Next i
   
   If (bOk) Or Not (bInQuote) Then
      LoadFile sParsed
   End If
   
End Sub

Private Sub EnableControls(ByVal State As Boolean)
Dim ctl As Control
Dim txt As TextBox
Dim cbo As ComboBox
Dim lst As ComboBox
Dim oCol As OLE_COLOR
   oCol = IIf(State, vbWindowBackground, vbButtonFace)
   For Each ctl In Controls
      If (ctl Is cmdPick) Then
      ElseIf (ctl Is txtMp3File) Then
      ElseIf (TypeOf ctl Is Menu) Then
         If (ctl Is mnuFile(1)) Then
            ctl.Enabled = State
         End If
      ElseIf (TypeOf ctl Is Frame) Then
      ElseIf (TypeOf ctl Is Label) Then
      ElseIf (TypeOf ctl Is TextBox) Then
         Set txt = ctl
         txt.BackColor = oCol
         txt.Enabled = State
      ElseIf (TypeOf ctl Is ComboBox) Then
         Set cbo = ctl
         cbo.BackColor = oCol
         cbo.Enabled = State
      ElseIf (TypeOf ctl Is ListBox) Then
         Set lst = ctl
         lst.BackColor = oCol
         lst.Enabled = False
      Else
         ctl.Enabled = State
      End If
   Next
End Sub

Private Sub showMp3Tags()
   
   ' Get ID3v1 Tag Information:
   With m_cID3v1
      .MP3File = txtMp3File.Text
   
      txtArtist.Text = .Artist
      txtTitle.Text = .Title
      txtAlbum.Text = .Album
      If (.GenreName(m_cID3v1.Genre) = "") Then
         ' not sure why VB doesn't allow .Text to be set
         cboGenre.ListIndex = 0
      Else
         cboGenre.Text = .GenreName(m_cID3v1.Genre)
      End If
      txtYear.Text = .Year
      txtComment.Text = .Comment
   End With
   
   With m_cID3v2
      .MP3File = txtMp3File.Text
      
      txtArtistV2.Text = .Artist
      txtTitleV2.Text = .Title
      txtAlbumV2.Text = .Album
      cboGenreV2.Text = .GenreName(m_cID3v1.Genre)
      txtYearV2.Text = .Year
      txtCommentV2.Text = .Comment
      txtTrack.Text = .Track
      txtOriginalArtist.Text = .OriginalArtist
      txtComposer.Text = .Composer
      txtCopyright.Text = .Copyright
      txtEncodedBy.Text = .EncodedBy
      txtLinkTo.Text = .LinkTo
   End With
   
End Sub

Private Sub cmdPick_Click()
   PickFile
End Sub

Private Sub LoadFile(ByVal sFile As String)
Dim bOk As Boolean

On Error GoTo errorHandler
   txtMp3File.Text = sFile
   showMp3Tags
   bOk = True
   
errorHandler:
   EnableControls bOk
   If Not (Err.Number = 0) Then
      MsgBox "Could not load file '" & sFile & "'" & vbCrLf & vbCrLf &
       Err.Description, vbInformation
   End If
End Sub

Private Sub PickFile()
   Dim sFile As String
   If (VBGetOpenFileName( _
         Filename:=sFile, _
         Filter:="MP3 Files (*.MP3)|*.MP3|All Files (*.*)|*.*", _
         DefaultExt:="MP3", _
         Owner:=Me.hWnd)) Then
      LoadFile sFile
   End If
End Sub

Private Sub cmdSet_Click()
   SetTags
End Sub

Private Sub SetTags()
   
   Dim bFound As Boolean
   Dim i As Long
   
   ' set genre v1:
   If Len(cboGenre.Text) > 0 Then
      For i = 0 To cboGenre.ListCount - 1
         If InStr(cboGenre.List(i), cboGenre.Text) > 0 Then
            cboGenre.ListIndex = i
            bFound = True
            Exit For
         End If
      Next i
   End If
   If Not bFound Then
      cboGenre.ListIndex = 0
   End If
   
   ' set genre v2:
   If Len(cboGenreV2.Text) > 0 Then
      For i = 0 To cboGenreV2.ListCount - 1
         If StrComp(cboGenreV2.List(i), cboGenreV2.Text, vbTextCompare) = 0 Then
            cboGenreV2.ListIndex = i
            Exit For
         End If
      Next i
   End If
   
   
   ' Update ID3 v1 tag:
   With m_cID3v1
      .Artist = txtArtist.Text
      .Title = txtTitle.Text
      .Album = txtAlbum.Text
      .Year = txtYear.Text
      .Comment = txtComment.Text
      .Genre = cboGenre.ItemData(cboGenre.ListIndex)
      .Update
   End With
   
   ' Update ID3 v2 tag:
   With m_cID3v2
      .Artist = txtArtistV2.Text
      .Title = txtTitleV2.Text
      .Album = txtAlbumV2.Text
      .Year = txtYearV2.Text
      .Comment = txtCommentV2.Text
      If (cboGenreV2.ListIndex > -1) Then
         .Genre = cboGenreV2.ItemData(cboGenreV2.ListIndex)
      End If
      .OtherGenreName = cboGenreV2.Text
      .Track = txtTrack.Text
      .OriginalArtist = txtOriginalArtist.Text
      .Composer = txtComposer.Text
      .Copyright = txtCopyright.Text
      .EncodedBy = txtEncodedBy.Text
      .LinkTo = txtLinkTo.Text
      .Update
   End With
   
   
   
End Sub

Private Sub Form_Load()
   
   Set m_cID3v1 = New cMP3ID3v1
   Set m_cID3v2 = New cMP3ID3v2
   
   cboGenre.AddItem ""
   cboGenre.ItemData(cboGenre.NewIndex) = 255
   cboGenreV2.AddItem ""
   cboGenreV2.ItemData(cboGenreV2.NewIndex) = 255
   Dim i As Long
   For i = 0 To 255
      If Len(m_cID3v1.GenreName(i)) > 0 Then
         cboGenre.AddItem m_cID3v1.GenreName(i)
         cboGenre.ItemData(cboGenre.NewIndex) = i
         cboGenreV2.AddItem m_cID3v1.GenreName(i)
         cboGenreV2.ItemData(cboGenreV2.NewIndex) = i
      End If
   Next i
   
   EnableControls False
   
   Me.Show
   Me.Refresh
   
   Dim sCmd As String
   sCmd = Command
   If (Len(sCmd) > 0) Then
      ParseCommand sCmd
   End If
   
End Sub

Private Sub Form_OLEDragDrop(Data As DataObject, Effect As Long, Button As
 Integer, Shift As Integer, X As Single, Y As Single)
   On Error Resume Next
   If (Data.Files.Count > 0) Then
      If (Err.Number = 0) Then
         Effect = vbDropEffectCopy
         LoadFile Data.Files(1)
      End If
   End If
End Sub

Private Sub Form_OLEDragOver(Data As DataObject, Effect As Long, Button As
 Integer, Shift As Integer, X As Single, Y As Single, State As Integer)
   '
   On Error Resume Next
   If (Data.Files.Count > 0) Then
      If (Err.Number = 0) Then
         Effect = vbDropEffectCopy
         Exit Sub
      End If
   End If
   Effect = vbDropEffectNone
   '
End Sub

Private Sub mnuFile_Click(Index As Integer)
   Select Case Index
   Case 0
      PickFile
   Case 1
      SetTags
   Case 3
      Unload Me
   End Select
End Sub