FlagsControl.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System;
  2. using System.Windows.Forms;
  3. using System.ComponentModel;
  4. using FastReport.Utils;
  5. namespace FastReport.Controls
  6. {
  7. #if !DEBUG
  8. [DesignTimeVisible(false)]
  9. #endif
  10. internal class FlagsControl : CheckedListBox
  11. {
  12. private Type enumType;
  13. private bool allowMultipleFlags;
  14. public Enum Flags
  15. {
  16. get
  17. {
  18. string s = "";
  19. foreach (object o in CheckedItems)
  20. {
  21. s += (string)o + ", ";
  22. }
  23. if (s != "")
  24. {
  25. s = s.Remove(s.Length - 2);
  26. return (Enum)Enum.Parse(enumType, s);
  27. }
  28. return (Enum)Enum.ToObject(enumType, 0);
  29. }
  30. set
  31. {
  32. enumType = value.GetType();
  33. string[] names = Enum.GetNames(enumType);
  34. Array values = Enum.GetValues(enumType);
  35. int enumValue = (int)Enum.ToObject(enumType, value);
  36. float maxWidth = 0;
  37. for (int i = 0; i < names.Length; i++)
  38. {
  39. int val = (int)values.GetValue(i);
  40. if (val != 0 && (val & 3) != 3)
  41. {
  42. Items.Add(names[i]);
  43. SetItemChecked(Items.Count - 1, (enumValue & val) != 0);
  44. float itemWidth = DrawUtils.MeasureString(names[i]).Width;
  45. if (itemWidth > maxWidth)
  46. maxWidth = itemWidth;
  47. }
  48. }
  49. Width = (int)maxWidth + 20;
  50. Height = (Items.Count + 1) * (ItemHeight + 1);
  51. }
  52. }
  53. internal bool AllowMultipleFlags
  54. {
  55. get { return allowMultipleFlags; }
  56. set { allowMultipleFlags = value; }
  57. }
  58. protected override void OnItemCheck(ItemCheckEventArgs ice)
  59. {
  60. base.OnItemCheck(ice);
  61. if(!allowMultipleFlags && ice.NewValue == CheckState.Checked)
  62. {
  63. foreach (int i in CheckedIndices)
  64. SetItemChecked(i, false);
  65. }
  66. }
  67. public FlagsControl()
  68. {
  69. BorderStyle = BorderStyle.None;
  70. CheckOnClick = true;
  71. allowMultipleFlags = true;
  72. }
  73. }
  74. }