Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Explicit On
  2. Option Strict On
  3.  
  4. Public Class frmMainWindow
  5.  
  6.     Dim albumArray(1000) As String
  7.     Dim albumCounter As Integer
  8.  
  9.     Private Sub btnQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuit.Click
  10.  
  11.         Me.Close()
  12.  
  13.     End Sub
  14.  
  15.     Private Sub btnLoadList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadList.Click
  16.  
  17.         Dim listFile As IO.StreamReader
  18.         Dim wishListFileName As String
  19.         Dim fmtStr As String = "{0, -7}  {1, -30}"
  20.  
  21.         wishListFileName = InputBox("Enter the file path and name.", "Name of File", "WishList.txt")
  22.  
  23.         If wishListFileName = "" Then
  24.  
  25.             MessageBox.Show("You must enter a file name.  Please press the Load List button again.", "File Name Missing")
  26.  
  27.         Else
  28.  
  29.             If IO.File.Exists(wishListFileName) Then
  30.  
  31.                 listFile = IO.File.OpenText(wishListFileName)
  32.  
  33.                 Do While listFile.Peek <> -1
  34.  
  35.                     albumCounter += 1
  36.                     albumArray(albumCounter) = listFile.ReadLine
  37.  
  38.                 Loop
  39.  
  40.                 listFile.Close()
  41.                 DisplayCDList()
  42.  
  43.                 MessageBox.Show(albumCounter & " album(s) loaded from the list.", "Load Complete")
  44.                 btnDeleteCD.Enabled = True
  45.                 btnSortList.Enabled = True
  46.                 btnSaveList.Enabled = True
  47.                 btnLoadList.Enabled = False
  48.                 btnRandomCD.Enabled = True
  49.  
  50.             Else
  51.  
  52.                 MessageBox.Show("There is no file with that path and filename.  Please press the Load List button again.", "File Not Found")
  53.  
  54.             End If
  55.  
  56.         End If
  57.  
  58.     End Sub
  59.  
  60.     Private Sub btnAddCD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddCD.Click
  61.  
  62.         Dim fmtStr As String = "{0, -7}  {1, -30}"
  63.  
  64.         albumCounter += 1
  65.         albumArray(albumCounter) = txtArtistName.Text & " - " & txtAlbumName.Text
  66.         DisplayCDList()
  67.  
  68.         If albumCounter > 998 Then
  69.  
  70.             btnAddCD.Enabled = False
  71.             MessageBox.Show("You have reached the limit.  If you want to add another album, you have to delete one first.", "Limit Reached")
  72.             btnDeleteCD.Focus()
  73.  
  74.         End If
  75.  
  76.         btnDeleteCD.Enabled = True
  77.         btnSortList.Enabled = True
  78.         btnSaveList.Enabled = True
  79.         btnRandomCD.Enabled = True
  80.         txtArtistName.Clear()
  81.         txtAlbumName.Clear()
  82.         txtArtistName.Focus()
  83.  
  84.         ' This line will put set the focus to the last added cd.
  85.        lstListOutput.SelectedIndex = albumCounter + 1
  86.  
  87.     End Sub
  88.  
  89.     Private Sub btnSaveList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveList.Click
  90.  
  91.         Dim destinationFile As IO.StreamWriter
  92.         Dim wishListFileName As String
  93.         Dim stepLoop As Integer
  94.  
  95.         wishListFileName = InputBox("Enter the file path and name.", "Name of File", "WishList.txt")
  96.  
  97.         If wishListFileName = "" Then
  98.  
  99.             MessageBox.Show("You must enter a file name.  Please press the Load List button again.", "File Name Missing")
  100.  
  101.         ElseIf IO.File.Exists(wishListFileName) Then
  102.  
  103.             IO.File.Delete(wishListFileName)
  104.  
  105.         End If
  106.  
  107.         destinationFile = IO.File.AppendText(wishListFileName)
  108.  
  109.         For stepLoop = 1 To albumCounter
  110.  
  111.             destinationFile.WriteLine(albumArray(stepLoop))
  112.  
  113.         Next
  114.  
  115.         destinationFile.Close()
  116.  
  117.         If IO.File.Exists(wishListFileName) Then
  118.  
  119.             MessageBox.Show("Your data has been saved.", "File Saved")
  120.  
  121.         Else
  122.  
  123.             MessageBox.Show("The destination file does not exist.  Something went wrong.", "File Save Not Successful")
  124.  
  125.         End If
  126.  
  127.     End Sub
  128.  
  129.     Private Sub btnDeleteCD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeleteCD.Click
  130.  
  131.         Dim stepLoop As Long
  132.         Dim selectedSpot As Long
  133.  
  134.         selectedSpot = lstListOutput.SelectedIndex - 1
  135.         lstListOutput.Items.RemoveAt(lstListOutput.SelectedIndex)
  136.  
  137.         For stepLoop = selectedSpot To UBound(albumArray) - 1
  138.  
  139.             albumArray(CInt(stepLoop)) = albumArray(CInt(stepLoop + 1))
  140.  
  141.         Next
  142.  
  143.         ReDim Preserve albumArray(UBound(albumArray) - 1)
  144.         albumCounter -= 1
  145.  
  146.     End Sub
  147.  
  148.     Private Sub btnSortList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSortList.Click
  149.  
  150.         Dim sortGap As Integer
  151.         Dim doneFlag As Boolean
  152.         Dim tempHolder As String
  153.         Dim stepLoop As Integer
  154.         Dim fmtStr As String = "{0, -7}  {1, -30}"
  155.  
  156.         sortGap = CInt(Int(albumCounter / 2))
  157.  
  158.         Do While sortGap >= 1
  159.  
  160.             Do
  161.  
  162.                 doneFlag = True
  163.  
  164.                 For stepLoop = 1 To albumCounter - sortGap
  165.  
  166.                     If albumArray(stepLoop) > albumArray(stepLoop + sortGap) Then
  167.  
  168.                         tempHolder = albumArray(stepLoop)
  169.                         albumArray(stepLoop) = albumArray(stepLoop + sortGap)
  170.                         albumArray(stepLoop + sortGap) = tempHolder
  171.                         doneFlag = False
  172.  
  173.                     End If
  174.  
  175.                 Next
  176.  
  177.             Loop Until doneFlag = True
  178.  
  179.             sortGap = CInt(Int(sortGap / 2))
  180.  
  181.         Loop
  182.  
  183.         DisplayCDList()
  184.         txtArtistName.Focus()
  185.  
  186.     End Sub
  187.  
  188.     Private Sub frmMainWindow_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  189.  
  190.         btnLoadList.Focus()
  191.  
  192.     End Sub
  193.  
  194.     Private Sub btnRandomCD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRandomCD.Click
  195.  
  196.         Dim randomNum As New Random
  197.         Dim randomAlbum As Integer
  198.  
  199.         randomAlbum = randomNum.Next(1, albumCounter + 1)
  200.         MessageBox.Show("Your randomly selected cd is " & albumArray(randomAlbum) & ".", "Random CD")
  201.  
  202.     End Sub
  203.  
  204.     Sub DisplayCDList()
  205.  
  206.         Dim stepLoop As Integer
  207.         Dim fmtStr As String = "{0, -7}  {1, -30}"
  208.  
  209.         lstListOutput.Items.Clear()
  210.         lstListOutput.Items.Add(String.Format(fmtStr, "Album #", "Artist - Album Name"))
  211.         lstListOutput.Items.Add(String.Format(fmtStr, "-------", "-------------------"))
  212.  
  213.         For stepLoop = 1 To albumCounter
  214.  
  215.             lstListOutput.Items.Add(String.Format(fmtStr, stepLoop, albumArray(stepLoop)))
  216.  
  217.         Next
  218.  
  219.     End Sub
  220.  
  221. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement