PageRenderTime 56ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/FlashCard/WindowsFormsApplication1/Tabs/Settings.cs

https://bitbucket.org/floAr/personal
C# | 83 lines | 72 code | 11 blank | 0 comment | 17 complexity | 5e3dc9830880538b67a2c28179c1f05d MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Collections;
  10. namespace FlashCard.Tabs
  11. {
  12. public partial class Settings : UserControl
  13. {
  14. private Deck<Word> wordDeck;
  15. private Deck<KanjiCharacter> kanjiDeck;
  16. public Settings()
  17. {
  18. InitializeComponent();
  19. }
  20. public void LoadDeck(Deck<Word> wordDeck, Deck<KanjiCharacter> kanjiDeck)
  21. {
  22. this.wordDeck = wordDeck;
  23. this.kanjiDeck = kanjiDeck;
  24. this.wordCategoryListBox.Items.AddRange(this.wordDeck.AllCards.Values.Select(w => w.Category).Distinct().ToArray());
  25. this.kanjiCategoryListBox.Items.AddRange(this.kanjiDeck.AllCards.Values.Select(w => w.Category).Distinct().ToArray());
  26. CheckCorrectBoxes(this.wordDeck.EnabledCategories, this.wordCategoryListBox);
  27. CheckCorrectBoxes(this.kanjiDeck.EnabledCategories, this.kanjiCategoryListBox);
  28. }
  29. private void CheckCorrectBoxes(IEnumerable<string> categories, CheckedListBox listBox)
  30. {
  31. int count = categories.Count();
  32. for (int i = 0; i < listBox.Items.Count; i++)
  33. {
  34. string item = listBox.Items[i] as string;
  35. listBox.SetItemChecked(i, count == 0 || categories.Contains(item));
  36. }
  37. }
  38. private void wordCategoryListBox_ItemCheck(object sender, ItemCheckEventArgs e)
  39. {
  40. string category = this.wordCategoryListBox.Items[e.Index] as string;
  41. if (e.NewValue == CheckState.Checked && !this.wordDeck.EnabledCategories.Contains(category))
  42. {
  43. this.wordDeck.EnabledCategories.Add(category);
  44. }
  45. else if (e.NewValue == CheckState.Unchecked && this.wordDeck.EnabledCategories.Contains(category))
  46. {
  47. this.wordDeck.EnabledCategories.Remove(category);
  48. }
  49. wordDeck.UpdateCategories();
  50. }
  51. private void kanjiCategoryListBox_ItemCheck(object sender, ItemCheckEventArgs e)
  52. {
  53. string category = this.kanjiCategoryListBox.Items[e.Index] as string;
  54. if (e.NewValue == CheckState.Checked && !this.kanjiDeck.EnabledCategories.Contains(category))
  55. {
  56. this.kanjiDeck.EnabledCategories.Add(category);
  57. }
  58. else if (e.NewValue == CheckState.Unchecked && this.kanjiDeck.EnabledCategories.Contains(category))
  59. {
  60. this.kanjiDeck.EnabledCategories.Remove(category);
  61. }
  62. kanjiDeck.UpdateCategories();
  63. }
  64. private void importBtn_Click(object sender, EventArgs e)
  65. {
  66. foreach (KanjiCharacter character in Importers.ImportKanji())
  67. {
  68. this.kanjiDeck.AddCard(character);
  69. }
  70. }
  71. }
  72. }