CheckBoxEditor.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing.Design;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows.Forms;
  9. using System.Windows.Forms.Design;
  10. namespace FWindSoft.WinForm
  11. {
  12. public class CheckBoxEditor:UITypeEditor
  13. {
  14. public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
  15. {
  16. return UITypeEditorEditStyle.DropDown;
  17. }
  18. public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
  19. {
  20. try
  21. {
  22. if (provider == null)
  23. return value;
  24. IWindowsFormsEditorService edSvc =
  25. (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
  26. CheckBox check=new CheckBox();
  27. check.Checked = Convert.ToBoolean(context.PropertyDescriptor.GetValue(context.Instance));
  28. edSvc.DropDownControl(check);
  29. edSvc.CloseDropDown();
  30. return check.Checked;
  31. }
  32. catch (Exception)
  33. {
  34. }
  35. return base.EditValue(context, provider, value);
  36. }
  37. public override bool GetPaintValueSupported(ITypeDescriptorContext context)
  38. {
  39. return true;
  40. }
  41. public override void PaintValue(PaintValueEventArgs e)
  42. {
  43. var context = e.Context;
  44. bool check = Convert.ToBoolean(context.PropertyDescriptor.GetValue(context.Instance));
  45. ControlPaint.DrawCheckBox(e.Graphics, e.Bounds, check?ButtonState.Checked : ButtonState.Normal);
  46. }
  47. }
  48. }