PageRenderTime 56ms CodeModel.GetById 37ms app.highlight 13ms RepoModel.GetById 2ms app.codeStats 0ms

/Source Code/PowerSong/Categories/frmCategory.vb

#
Visual Basic | 81 lines | 57 code | 19 blank | 5 comment | 1 complexity | ef78b0ee6331e806b3ed923ee39128df MD5 | raw file
 1Imports System.Windows.Forms
 2Imports PowerSong.SongDatabase
 3
 4Public Class frmCategory
 5
 6    Private FDatabase As Database = Nothing
 7
 8    Private FCategoryID As Guid = Nothing
 9
10    Private Enum EMode
11        Add
12        Edit
13    End Enum
14
15    Private FMode As EMode = EMode.Add
16
17    Public Sub New(ByVal database As Database)
18        InitializeComponent()
19        FDatabase = database
20        FMode = EMode.Add
21        Text = "Add Category"
22    End Sub
23
24    Public Sub New(ByVal database As Database, ByVal categoryID As Guid)
25        InitializeComponent()
26        FDatabase = database
27        FMode = EMode.Edit
28        Text = "Edit Category"
29        FCategoryID = categoryID
30        txtCategoryName.Text = database.Categories.GetCategoryName(categoryID)
31    End Sub
32
33    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
34
35        ' Ensure a name has been specified for the category
36        If txtCategoryName.Text = "" Then
37            MsgBox("Please enter a name for the category.", MsgBoxStyle.Exclamation)
38            Exit Sub
39        End If
40
41        ' Add or edit the category
42        Select Case FMode
43
44            Case EMode.Add
45
46                ' Add the category
47                If FDatabase.Categories.CategoryExists(txtCategoryName.Text) Then
48                    MsgBox("The category '" + txtCategoryName.Text + "' already exists. Please specify a different name.", MsgBoxStyle.Exclamation)
49                    Exit Sub
50                End If
51                FDatabase.Categories.AddCategory(Guid.NewGuid, txtCategoryName.Text.Trim)
52
53            Case EMode.Edit
54
55                ' Edit an existing category
56                Dim CategoryExists As Boolean = False
57                For Each Category As KeyValuePair(Of Guid, String) In FDatabase.Categories.GetAllCategories
58                    If Category.Value.ToUpper.Trim = txtCategoryName.Text.ToUpper.Trim AndAlso Category.Key <> FCategoryID Then
59                        CategoryExists = True
60                    End If
61                Next
62                If CategoryExists Then
63                    MsgBox("The category '" + txtCategoryName.Text + "' already exists. Please specify a different name.", MsgBoxStyle.Exclamation)
64                    Exit Sub
65                End If
66                FDatabase.Categories.ChangeCategoryName(FCategoryID, txtCategoryName.Text.Trim)
67
68        End Select
69
70        ' Close the form
71        Me.DialogResult = System.Windows.Forms.DialogResult.OK
72        Me.Close()
73
74    End Sub
75
76    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
77        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
78        Me.Close()
79    End Sub
80
81End Class