Dit screen dump ligner VB6. Mit eksembel gør du følgende:
Add en Form, navn = Form1.
Add en Class, navn = cPlaylist.
I menuen Project > References vælger du:
"Microsoft Scripting Runtime"
På Form1:
Add en Combobox, navn = Combo1
Add en Textbox, navn = Text1
Add en Textbox, navn = Text2
Add en CommandButton, navn = cmdAdd
Add en CommandButton, navn = cmdDel
Add en CommandButton, navn = cmdSave
P.S. Der er ingen error handling what so ever.
Kode til klassen cPlaylist:
Public File As String
Public Title As String
Kode til Form1:
Option Explicit
Dim Col As New Collection
Private Function LoadPlaylist(ByVal FileName As String) As String
Dim FObj As New Scripting.FileSystemObject
Dim TS As TextStream
Dim Tmp As Variant
Dim Playlist As cPlaylist
Set TS = FObj.OpenTextFile(FileName, ForReading)
While TS.AtEndOfStream = False
Tmp = Split(TS.ReadLine, "=")
If InStr(1, Tmp(0), "file", vbTextCompare) = 1 Then
Set Playlist = New cPlaylist
Playlist.File = Tmp(1)
Tmp = Split(TS.ReadLine, "=")
Playlist.Title = Tmp(1)
Col.Add Playlist
End If
Wend
TS.Close
Set FObj = Nothing
End Function
Private Sub SavePlaylist(ByVal FileName As String)
Dim FObj As New Scripting.FileSystemObject
Dim TS As TextStream
Dim Playlist As New cPlaylist
Dim X1 As Integer
Set TS = FObj.OpenTextFile(FileName, ForWriting, True)
TS.WriteLine "[playlist]"
For X1 = 1 To Col.Count
Set Playlist = Col.Item(X1)
TS.WriteLine "file" & X1 & "=" & Playlist.File
TS.WriteLine "title" & X1 & "=" & Playlist.Title
TS.WriteLine "Length=-1"
Next X1
TS.WriteLine "NumberOfEntries = " & Col.Count
TS.WriteLine "Version = 2"
TS.Close
Set FObj = Nothing
End Sub
Sub Delete(Index As Variant)
Dim Playlist As New cPlaylist
Dim X1 As Integer
If IsNumeric(Index) Then
Col.Remove (Index)
Else
For X1 = 1 To Col.Count
Set Playlist = Col.Item(X1)
If Playlist.File = Index Then
Col.Remove (X1)
Exit For
End If
Next X1
End If
End Sub
Sub Add(File As String, Title As String)
Dim Playlist As New cPlaylist
Playlist.File = File
Playlist.Title = Title
Col.Add Playlist
End Sub
Sub InitChannelSelector()
Dim X1 As Integer
Combo1.Clear
For X1 = 1 To Col.Count
Combo1.AddItem "Channel " & X1
Next X1
Combo1.ListIndex = 0
End Sub
Private Sub cmdAdd_Click()
Add Text2.Text, Text1.Text
InitChannelSelector
End Sub
Private Sub cmdDel_Click()
Delete Combo1.ListIndex + 1
InitChannelSelector
End Sub
Private Sub cmdSave_Click()
SavePlaylist "C:\\Temp\\Playlist.txt"
End Sub
Private Sub Combo1_Click()
Dim Playlist As New cPlaylist
Set Playlist = Col.Item(Combo1.ListIndex + 1)
Text1.Text = Playlist.Title
Text2.Text = Playlist.File
End Sub
Private Sub Form_Load()
LoadPlaylist "C:\\Temp\\Playlist.txt"
InitChannelSelector
End Sub