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