Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Option Explicit On
- Option Strict On
- Public Class frmMainWindow
- Dim albumArray(1000) As String
- Dim albumCounter As Integer
- Private Sub btnQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuit.Click
- Me.Close()
- End Sub
- Private Sub btnLoadList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadList.Click
- Dim listFile As IO.StreamReader
- Dim wishListFileName As String
- Dim fmtStr As String = "{0, -7} {1, -30}"
- wishListFileName = InputBox("Enter the file path and name.", "Name of File", "WishList.txt")
- If wishListFileName = "" Then
- MessageBox.Show("You must enter a file name. Please press the Load List button again.", "File Name Missing")
- Else
- If IO.File.Exists(wishListFileName) Then
- listFile = IO.File.OpenText(wishListFileName)
- Do While listFile.Peek <> -1
- albumCounter += 1
- albumArray(albumCounter) = listFile.ReadLine
- Loop
- listFile.Close()
- DisplayCDList()
- MessageBox.Show(albumCounter & " album(s) loaded from the list.", "Load Complete")
- btnDeleteCD.Enabled = True
- btnSortList.Enabled = True
- btnSaveList.Enabled = True
- btnLoadList.Enabled = False
- btnRandomCD.Enabled = True
- Else
- MessageBox.Show("There is no file with that path and filename. Please press the Load List button again.", "File Not Found")
- End If
- End If
- End Sub
- Private Sub btnAddCD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddCD.Click
- Dim fmtStr As String = "{0, -7} {1, -30}"
- albumCounter += 1
- albumArray(albumCounter) = txtArtistName.Text & " - " & txtAlbumName.Text
- DisplayCDList()
- If albumCounter > 998 Then
- btnAddCD.Enabled = False
- MessageBox.Show("You have reached the limit. If you want to add another album, you have to delete one first.", "Limit Reached")
- btnDeleteCD.Focus()
- End If
- btnDeleteCD.Enabled = True
- btnSortList.Enabled = True
- btnSaveList.Enabled = True
- btnRandomCD.Enabled = True
- txtArtistName.Clear()
- txtAlbumName.Clear()
- txtArtistName.Focus()
- ' This line will put set the focus to the last added cd.
- lstListOutput.SelectedIndex = albumCounter + 1
- End Sub
- Private Sub btnSaveList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveList.Click
- Dim destinationFile As IO.StreamWriter
- Dim wishListFileName As String
- Dim stepLoop As Integer
- wishListFileName = InputBox("Enter the file path and name.", "Name of File", "WishList.txt")
- If wishListFileName = "" Then
- MessageBox.Show("You must enter a file name. Please press the Load List button again.", "File Name Missing")
- ElseIf IO.File.Exists(wishListFileName) Then
- IO.File.Delete(wishListFileName)
- End If
- destinationFile = IO.File.AppendText(wishListFileName)
- For stepLoop = 1 To albumCounter
- destinationFile.WriteLine(albumArray(stepLoop))
- Next
- destinationFile.Close()
- If IO.File.Exists(wishListFileName) Then
- MessageBox.Show("Your data has been saved.", "File Saved")
- Else
- MessageBox.Show("The destination file does not exist. Something went wrong.", "File Save Not Successful")
- End If
- End Sub
- Private Sub btnDeleteCD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeleteCD.Click
- Dim stepLoop As Long
- Dim selectedSpot As Long
- selectedSpot = lstListOutput.SelectedIndex - 1
- lstListOutput.Items.RemoveAt(lstListOutput.SelectedIndex)
- For stepLoop = selectedSpot To UBound(albumArray) - 1
- albumArray(CInt(stepLoop)) = albumArray(CInt(stepLoop + 1))
- Next
- ReDim Preserve albumArray(UBound(albumArray) - 1)
- albumCounter -= 1
- End Sub
- Private Sub btnSortList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSortList.Click
- Dim sortGap As Integer
- Dim doneFlag As Boolean
- Dim tempHolder As String
- Dim stepLoop As Integer
- Dim fmtStr As String = "{0, -7} {1, -30}"
- sortGap = CInt(Int(albumCounter / 2))
- Do While sortGap >= 1
- Do
- doneFlag = True
- For stepLoop = 1 To albumCounter - sortGap
- If albumArray(stepLoop) > albumArray(stepLoop + sortGap) Then
- tempHolder = albumArray(stepLoop)
- albumArray(stepLoop) = albumArray(stepLoop + sortGap)
- albumArray(stepLoop + sortGap) = tempHolder
- doneFlag = False
- End If
- Next
- Loop Until doneFlag = True
- sortGap = CInt(Int(sortGap / 2))
- Loop
- DisplayCDList()
- txtArtistName.Focus()
- End Sub
- Private Sub frmMainWindow_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
- btnLoadList.Focus()
- End Sub
- Private Sub btnRandomCD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRandomCD.Click
- Dim randomNum As New Random
- Dim randomAlbum As Integer
- randomAlbum = randomNum.Next(1, albumCounter + 1)
- MessageBox.Show("Your randomly selected cd is " & albumArray(randomAlbum) & ".", "Random CD")
- End Sub
- Sub DisplayCDList()
- Dim stepLoop As Integer
- Dim fmtStr As String = "{0, -7} {1, -30}"
- lstListOutput.Items.Clear()
- lstListOutput.Items.Add(String.Format(fmtStr, "Album #", "Artist - Album Name"))
- lstListOutput.Items.Add(String.Format(fmtStr, "-------", "-------------------"))
- For stepLoop = 1 To albumCounter
- lstListOutput.Items.Add(String.Format(fmtStr, stepLoop, albumArray(stepLoop)))
- Next
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement