FlagsControl.cs 2.3 KB

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