Click here to Skip to main content
15,886,963 members
Home / Discussions / Visual Basic
   

Visual Basic

 
QuestionVisual basic Pin
Member 160253288-Jun-23 18:45
Member 160253288-Jun-23 18:45 
AnswerRe: Visual basic Pin
Richard MacCutchan9-Jun-23 0:34
mveRichard MacCutchan9-Jun-23 0:34 
GeneralRe: Visual basic Pin
Member 160253289-Jun-23 18:18
Member 160253289-Jun-23 18:18 
GeneralRe: Visual basic Pin
Richard MacCutchan9-Jun-23 22:28
mveRichard MacCutchan9-Jun-23 22:28 
GeneralRe: Visual basic Pin
Member 1602532810-Jun-23 0:13
Member 1602532810-Jun-23 0:13 
GeneralRe: Visual basic Pin
Richard MacCutchan10-Jun-23 1:18
mveRichard MacCutchan10-Jun-23 1:18 
AnswerRe: Visual basic Pin
Gerry Schmitz9-Jun-23 2:51
mveGerry Schmitz9-Jun-23 2:51 
QuestionCrash on converting string to byte Pin
Member Alienoiz8-Jun-23 3:49
Member Alienoiz8-Jun-23 3:49 
Hello all...im new and im not even a coder. I just do some apps once in a while to keep the head busy and exercise my mind.
im actually trying to make an mp3 tagger. All is good but i cannot tag new files..only pre-tagged files...it says it cannot convert string to byte in the "Track" and "Genre" fields.

This is the class im using
VB.NET
 <pre>Imports System.IO

Public Class MP3ID3v1

    ' Constructor
    Public Sub New(Optional ByVal Filename As String = "")
        MyBase.New()
        If (Filename <> "") Then Me.Filename = Filename
    End Sub

#Region " Declarations "
     ' Genres
    Public Enum Genres As Byte
        Blues = 0
        ClassicRock = 1
        Country = 2
        Dance = 3
        Disco = 4
        Funk = 5
        Grunge = 6
        HipHop = 7
        Jazz = 8
        Metal = 9
        NewAge = 10
        Oldies = 11
        Other = 12
        Pop = 13
        RnB = 14
        Rap = 15
        Reggae = 16
        Rock = 17
        Techno = 18
        Industrial = 19
        Alternative = 20
        Ska = 21
        DeathMetal = 22
        Pranks = 23
        Soundtrack = 24
        EuroTechno = 25
        Ambient = 26
        TripHop = 27
        Vocal = 28
        JazzFunk = 29
        Fusion = 30
        Trance = 31
        Classical = 32
        Instrumental = 33
        Acid = 34
        House = 35
        Game = 36
        SoundClip = 37
        Gospel = 38
        Noise = 39
        AlternRock = 40
        Bass = 41
        Soul = 42
        Punk = 43
        Space = 44
        Meditative = 45
        InstrumentalPop = 46
        InstrumentalRock = 47
        Ethnic = 48
        Gothic = 49
        Darkwave = 50
        TechnoIndustrial = 51
        Electronic = 52
        PopFolk = 53
        Eurodance = 54
        Dream = 55
        SouthernRock = 56
        Comedy = 57
        Cult = 58
        Gangsta = 59
        Top40 = 60
        ChristianRap = 61
        PopFunk = 62
        Jungle = 63
        NativeAmerican = 64
        Cabaret = 65
        NewWave = 66
        Psychadelic = 67
        Rave = 68
        Showtunes = 69
        Trailer = 70
        LoFi = 71
        Tribal = 72
        AcidPunk = 73
        AcidJazz = 74
        Polka = 75
        Retro = 76
        Musical = 77
        RocknRoll = 78
        HardRock = 79
        None = 255
    End Enum

    ' Frame types
    Public Enum FrameTypes As Byte
        Title = 0
        Artist = 1
        Album = 2
        Year = 3
        Track = 4
        Comment = 5
        Genre = 6
    End Enum

#End Region
#Region " Properties "
     ' Filename
    Private mstrFilename As String
    Public Property Filename() As String
        Get
            Return mstrFilename
        End Get
        Set(ByVal Value As String)
            Dim objFile As File
            If (objFile.Exists(Value)) Then
                mstrFilename = Value
                Refresh()
            Else
                Throw New System.IO.FileLoadException("The specified file does not exist", Value)
            End If
        End Set
    End Property

    ' TagExists
    Private mblnTagExists As Boolean
    Public ReadOnly Property TagExists() As Boolean
        Get
            Return mblnTagExists
        End Get
    End Property

    ' Frame
    Private mobjFrame(7) As Object
    Public Property Frame(ByVal FrameType As FrameTypes)
        Get
            Return mobjFrame(FrameType)
        End Get
        Set(ByVal Value)
            mobjFrame(FrameType) = Value
        End Set
    End Property

#End Region
#Region " Methods "
     ' Refresh (gets all tags from the specified file)
    Public Sub Refresh()

        ' Declarations
        Dim strTag As New String(" ", 3)
        Dim strTitle As New String(" ", 30)
        Dim strArtist As New String(" ", 30)
        Dim strAlbum As New String(" ", 30)
        Dim strYear As New String(" ", 4)
        Dim strComment As New String(" ", 28)
        Dim bytDummy As Byte
        Dim bytTrack As Byte
        Dim bytGenre As Byte

        ' Open the file
        Dim intFile As Integer = FreeFile()
        FileOpen(intFile, mstrFilename, OpenMode.Binary, OpenAccess.Read, OpenShare.LockWrite)

        ' Gets length of file
        Dim lngLOF As Long = LOF(intFile)
        If (lngLOF > 128) Then

            ' Check for the ID3v1 tag
            FileGet(intFile, strTag, lngLOF - 127, True)
            If (strTag.ToUpper <> "TAG") Then

                ' No ID3v1 tag found
                mblnTagExists = False
                mobjFrame(0) = ""
                mobjFrame(1) = ""
                mobjFrame(2) = ""
                mobjFrame(3) = ""
                mobjFrame(4) = ""
                mobjFrame(5) = ""
                mobjFrame(6) = ""

            Else

                ' ID3v1 tag found
                mblnTagExists = True

                ' Read all frames from the file
                FileGet(intFile, strTitle)
                FileGet(intFile, strArtist)
                FileGet(intFile, strAlbum)
                FileGet(intFile, strYear)
                FileGet(intFile, strComment)
                FileGet(intFile, bytDummy)
                FileGet(intFile, bytTrack)
                FileGet(intFile, bytGenre)

                ' Assign the frame content to the properties
                mobjFrame(0) = strTitle
                mobjFrame(1) = strArtist
                mobjFrame(2) = strAlbum
                mobjFrame(3) = strYear
                mobjFrame(4) = bytTrack
                mobjFrame(5) = strComment
                mobjFrame(6) = bytGenre

            End If
        End If

        ' Close the file
        FileClose(intFile)

    End Sub

    ' Update
    Public Sub Update()

        ' Declarations
        Dim strTag As New String(" ", 3)
        Dim strTitle As New String(" ", 30)
        Dim strArtist As New String(" ", 30)
        Dim strAlbum As New String(" ", 30)
        Dim strYear As New String(" ", 4)
        Dim strComment As New String(" ", 28)
        Dim bytDummy As Byte
        Dim bytTrack As Byte
        Dim bytGenre As Byte

        ' Open the file
        Dim intFile As Integer = FreeFile()
        FileOpen(intFile, mstrFilename, OpenMode.Binary, OpenAccess.ReadWrite, OpenShare.LockWrite)

        ' Gets length of file
        Dim lngLOF As Long = LOF(intFile)
        If (lngLOF > 0) Then
            If (lngLOF > 128) Then

                ' Check for an existing ID3v1 tag
                FileGet(intFile, strTag, lngLOF - 127)
                If (strTag.ToUpper <> "TAG") Then

                    ' No ID3v1 tag found, so just add one
                    Seek(intFile, lngLOF)
                    strTag = "TAG"
                    FilePut(intFile, strTag)

                End If

                ' Fix the length of the frames
                strTitle = LSet(mobjFrame(0), Len(strTitle))
                strArtist = LSet(mobjFrame(1), Len(strArtist))
                strAlbum = LSet(mobjFrame(2), Len(strAlbum))
                strYear = LSet(mobjFrame(3), Len(strYear))
                bytTrack = mobjFrame(4)
                strComment = LSet(mobjFrame(5), Len(strComment))
                bytGenre = mobjFrame(6)

                ' Write the frames to the file
                FilePut(intFile, strTitle)
                FilePut(intFile, strArtist)
                FilePut(intFile, strAlbum)
                FilePut(intFile, strYear)
                FilePut(intFile, strComment)
                FilePut(intFile, bytDummy)
                FilePut(intFile, bytTrack)
                FilePut(intFile, bytGenre)

            End If
        End If

        ' Close the file
        FileClose(intFile)

    End Sub

#End Region

End Class


and this is my code:
VB.NET
<pre>Imports System.IO
Imports System.Windows.Forms.VisualStyles.VisualStyleElement
Imports System.Resources.ResXFileRef
Imports System.Text

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then

            ListBox1.Items.Clear()
            Dim fileNames = My.Computer.FileSystem.GetFiles(FolderBrowserDialog1.SelectedPath, FileIO.SearchOption.SearchTopLevelOnly, "*.mp3")

            For Each fileName As String In fileNames
                Dim result As String = Path.GetFileName(fileName)
                ListBox1.Items.Add(result)
            Next


        End If
        ComboBox1.DataSource = System.Enum.GetValues(GetType(MP3ID3v1.Genres))
        Label1.Text = "Title :"
        Label2.Text = "Artist :"
        Label3.Text = "Album :"
        Label4.Text = "Year :"
        Label5.Text = "Track :"
        Label6.Text = "Comment :"
        Label7.Text = "Genre : "

    End Sub



    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        ListBox1.Items.Clear()
        If FolderBrowserDialog1.SelectedPath = "" Then
            Dim r8 As DialogResult = MessageBox.Show(Me, "You need to load a folder.",
                                   "Load Files", MessageBoxButtons.OK)
        Else
            Dim fileNames = My.Computer.FileSystem.GetFiles(FolderBrowserDialog1.SelectedPath, FileIO.SearchOption.SearchTopLevelOnly, "*.mp3")

            For Each fileName As String In fileNames
                Dim result As String = Path.GetFileName(fileName)
                ListBox1.Items.Add(result)
            Next

        End If

        ComboBox1.DataSource = System.Enum.GetValues(GetType(MP3ID3v1.Genres))
        Label1.Text = "Title :"
        Label2.Text = "Artist :"
        Label3.Text = "Album :"
        Label4.Text = "Year :"
        Label5.Text = "Track :"
        Label6.Text = "Comment :"
        Label7.Text = "Genre : "
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ComboBox1.DataSource = System.Enum.GetValues(GetType(MP3ID3v1.Genres))
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged

        Dim objMP3V1 As New MP3ID3v1(FolderBrowserDialog1.SelectedPath & "\" & ListBox1.SelectedItem)



        If (objMP3V1.TagExists) Then

            Label1.Text = "Title : " & (objMP3V1.Frame(MP3ID3v1.FrameTypes.Title))
            Label2.Text = "Artist : " & (objMP3V1.Frame(MP3ID3v1.FrameTypes.Artist))
            Label3.Text = "Album : " & (objMP3V1.Frame(MP3ID3v1.FrameTypes.Album))
            Label4.Text = "Year : " & (objMP3V1.Frame(MP3ID3v1.FrameTypes.Year))
            Dim TR = (objMP3V1.Frame(MP3ID3v1.FrameTypes.Track))
            Label5.Text = "Track : " & (TR.ToString)
            Label6.Text = "Comment : " & (objMP3V1.Frame(MP3ID3v1.FrameTypes.Comment))
            Dim GR As MP3ID3v1.Genres = (objMP3V1.Frame(MP3ID3v1.FrameTypes.Genre))
            Label7.Text = "Genre : " & (GR.ToString)

        Else
            Label1.Text = "Title :"
            Label2.Text = "Artist :"
            Label3.Text = "Album :"
            Label4.Text = "Year :"
            Label5.Text = "Track :"
            Label6.Text = "Comment :"
            Label7.Text = "Genre : "
            ComboBox1.DataSource = System.Enum.GetValues(GetType(MP3ID3v1.Genres))
        End If
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        If ListBox1.Text = "" Then
            Dim r8 As DialogResult = MessageBox.Show(Me, "No File Selected!",
                                   "Select Files", MessageBoxButtons.OK)


        Else

            Dim objMP3V1 As New MP3ID3v1(FolderBrowserDialog1.SelectedPath & "\" & ListBox1.SelectedItem)

            If CheckBox1.Checked = True Then
                objMP3V1.Frame(MP3ID3v1.FrameTypes.Title) = TextBox1.Text
                objMP3V1.Update()
            End If

            If CheckBox2.Checked = True Then
                objMP3V1.Frame(MP3ID3v1.FrameTypes.Artist) = TextBox2.Text
                objMP3V1.Update()
            End If

            If CheckBox3.Checked = True Then
                objMP3V1.Frame(MP3ID3v1.FrameTypes.Album) = TextBox3.Text
                objMP3V1.Update()
            End If

            If CheckBox4.Checked = True Then
                objMP3V1.Frame(MP3ID3v1.FrameTypes.Year) = TextBox4.Text
                objMP3V1.Update()
            End If

            If CheckBox5.Checked = True Then
                objMP3V1.Frame(MP3ID3v1.FrameTypes.Track) = TextBox5.Text
                objMP3V1.Update()
            End If
            If CheckBox6.Checked = True Then
                objMP3V1.Frame(MP3ID3v1.FrameTypes.Comment) = TextBox6.Text
                objMP3V1.Update()
            End If

            If CheckBox7.Checked = True Then
                objMP3V1.Frame(MP3ID3v1.FrameTypes.Genre) = ComboBox1.SelectedValue
                objMP3V1.Update()
            End If
            Dim r9 As DialogResult = MessageBox.Show(Me, "Operation Complete!",
                          "Tagged Files!", MessageBoxButtons.OK)
        End If


    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        ListBox1.Items.Clear()
        ComboBox1.DataSource = System.Enum.GetValues(GetType(MP3ID3v1.Genres))
    End Sub
End Class


What am i doing wrong?..Thanks!!
Duarte
AnswerRe: Crash on converting string to byte Pin
Richard MacCutchan8-Jun-23 4:12
mveRichard MacCutchan8-Jun-23 4:12 
GeneralRe: Crash on converting string to byte Pin
Member Alienoiz8-Jun-23 4:27
Member Alienoiz8-Jun-23 4:27 
GeneralRe: Crash on converting string to byte Pin
Richard MacCutchan8-Jun-23 4:50
mveRichard MacCutchan8-Jun-23 4:50 
GeneralRe: Crash on converting string to byte Pin
Member Alienoiz8-Jun-23 4:58
Member Alienoiz8-Jun-23 4:58 
GeneralRe: Crash on converting string to byte Pin
Richard MacCutchan8-Jun-23 5:05
mveRichard MacCutchan8-Jun-23 5:05 
GeneralRe: Crash on converting string to byte Pin
Member Alienoiz8-Jun-23 5:23
Member Alienoiz8-Jun-23 5:23 
GeneralRe: Crash on converting string to byte Pin
Richard MacCutchan8-Jun-23 5:27
mveRichard MacCutchan8-Jun-23 5:27 
GeneralRe: Crash on converting string to byte Pin
Member Alienoiz8-Jun-23 5:46
Member Alienoiz8-Jun-23 5:46 
GeneralRe: Crash on converting string to byte Pin
Richard MacCutchan8-Jun-23 6:19
mveRichard MacCutchan8-Jun-23 6:19 
GeneralRe: Crash on converting string to byte Pin
Member Alienoiz8-Jun-23 6:00
Member Alienoiz8-Jun-23 6:00 
GeneralRe: Crash on converting string to byte Pin
Richard MacCutchan8-Jun-23 6:19
mveRichard MacCutchan8-Jun-23 6:19 
GeneralRe: Crash on converting string to byte Pin
Member Alienoiz8-Jun-23 7:57
Member Alienoiz8-Jun-23 7:57 
GeneralRe: Crash on converting string to byte Pin
Richard MacCutchan8-Jun-23 8:04
mveRichard MacCutchan8-Jun-23 8:04 
GeneralRe: Crash on converting string to byte Pin
Member Alienoiz8-Jun-23 8:18
Member Alienoiz8-Jun-23 8:18 
GeneralRe: Crash on converting string to byte Pin
Member Alienoiz8-Jun-23 8:43
Member Alienoiz8-Jun-23 8:43 
GeneralRe: Crash on converting string to byte Pin
Richard MacCutchan8-Jun-23 23:12
mveRichard MacCutchan8-Jun-23 23:12 
GeneralRe: Crash on converting string to byte Pin
Member Alienoiz9-Jun-23 6:31
Member Alienoiz9-Jun-23 6:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.