PageRenderTime 25ms CodeModel.GetById 12ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/Source Code/PowerSong/Item Management/frmSong.vb

#
Visual Basic | 157 lines | 105 code | 42 blank | 10 comment | 0 complexity | a8bb042f74a5c5ad995039ad2af62b67 MD5 | raw file
  1Imports PowerSong.SongDatabase
  2Imports PowerSong.SongDatabase.Items
  3
  4Public Class frmSong
  5
  6    Private FDatabase As Database = Nothing
  7
  8    Private FSong As New SongItem
  9
 10    Public ReadOnly Property Song() As SongItem
 11        Get
 12            UpdateSong()
 13            Return FSong
 14        End Get
 15    End Property
 16
 17    Private Enum EMode
 18        Add
 19        Edit
 20    End Enum
 21
 22    Private FMode As EMode
 23
 24    Public Sub New(ByVal database As Database)
 25
 26        InitializeComponent()
 27
 28        FDatabase = database
 29        FMode = EMode.Add
 30        Text = "Add Song"
 31        UpdateAvailableCategories()
 32        UpdateAvailableLicenses()
 33        Functionality.ConfigureForm(Me)
 34
 35    End Sub
 36
 37    Public Sub New(ByVal database As Database, ByVal existingSong As SongItem)
 38
 39        InitializeComponent()
 40
 41        FDatabase = database
 42        FMode = EMode.Edit
 43        FSong = existingSong
 44        txtTitle.Text = FSong.Title
 45        For Each Author As String In FSong.Authors
 46            txtAuthors.Text += Author
 47            txtAuthors.Text += Environment.NewLine
 48        Next
 49        txtCopyrightLine.Text = FSong.CopyrightLine
 50        txtParts.Text = FSong.CombineIntoString
 51        Text = "Edit Song"
 52        UpdateAvailableCategories()
 53        UpdateAvailableLicenses()
 54
 55        ' TODO: Fix: cboLicense.SelectedItem = database.Songs.GetSongLicense(existingSong.SongID)
 56
 57        Functionality.ConfigureForm(Me)
 58
 59    End Sub
 60
 61    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
 62
 63        ' Ensure at least one category is selected
 64        Dim CategorySelected As Boolean = False
 65        For ItemIndex As Integer = 0 To lstCategories.Items.Count - 1
 66            If lstCategories.GetItemChecked(ItemIndex) = True Then CategorySelected = True
 67        Next
 68        If Not CategorySelected Then
 69            MsgBox("You must assign the song to at least one category.", MsgBoxStyle.Exclamation)
 70            Exit Sub
 71        End If
 72
 73        ' Close the form
 74        Me.DialogResult = System.Windows.Forms.DialogResult.OK
 75        Me.Close()
 76
 77    End Sub
 78
 79    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
 80        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
 81        Me.Close()
 82    End Sub
 83
 84    Private Sub UpdateSong()
 85
 86        ' Update song details
 87        FSong.Title = txtTitle.Text.Trim
 88        FSong.CopyrightLine = txtCopyrightLine.Text.Trim
 89
 90        ' Update verses
 91        FSong.Parts = SongItem.SplitIntoParts(txtParts.Lines)
 92
 93        ' Update authors
 94        FSong.Authors.Clear()
 95        For Each Author As String In txtAuthors.Lines
 96            If Author.Trim <> "" Then FSong.Authors.Add(Author.Trim)
 97        Next
 98
 99        ' Update categories
100        FSong.Categories.Clear()
101        For ItemIndex As Integer = 0 To lstCategories.Items.Count - 1
102            If lstCategories.GetItemChecked(ItemIndex) Then
103                Dim Item As ListItem(Of Guid) = lstCategories.Items(ItemIndex)
104                FSong.Categories.Add(Item.Key)
105            End If
106        Next
107
108    End Sub
109
110    Private Sub UpdateAvailableCategories()
111
112        ' Clear the list
113        lstCategories.Items.Clear()
114
115        ' Add categories
116        For Each Category As KeyValuePair(Of Guid, String) In FDatabase.Categories.GetAllCategories
117            Dim Item As New ListItem(Of Guid)(Category.Value, Category.Key)
118            Dim ItemIndex As Integer = lstCategories.Items.Add(Item)
119
120            ' Determine if the currently added item should be selected
121            For Each SongCategory As Guid In FSong.Categories
122                If SongCategory = Category.Key Then
123                    lstCategories.SetItemChecked(ItemIndex, True)
124                End If
125            Next
126
127        Next
128
129    End Sub
130
131    Private Sub btnManageCategories_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnManageCategories.Click
132
133        Dim Form As New frmManageCategories(FDatabase)
134        If Form.ShowDialog = Windows.Forms.DialogResult.OK Then
135            UpdateAvailableCategories()
136        End If
137
138    End Sub
139
140    Private Sub btnInsertCopyrightSymbol_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsertCopyrightSymbol.Click
141        txtCopyrightLine.Text += "©"
142    End Sub
143
144    Private Sub frmSong_Shown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown
145        txtTitle.Focus()
146    End Sub
147
148    Private Sub UpdateAvailableLicenses()
149
150        cboLicense.Items.Clear()
151        For Each License As License In Database.Licenses.GetAllLicenses
152            cboLicense.Items.Add(License)
153        Next
154
155    End Sub
156
157End Class