3

I am trying to pick only a time (hour and minutes) using propertygrid and datetimepicker.
I have created my own editor.
The trouble is that it is not showing the value in the text and the value can also have calendars:

Here have code:

public class Tests
    {
        private DateTimePicker time = new DateTimePicker();

        [Editor(typeof(MyEditor), typeof(UITypeEditor))]
        public DateTimePicker Time
        {
            get { return time; }
            set { time = value; }

        }

        public Tests()
        {
            time.Format = DateTimePickerFormat.Time;
            time.CustomFormat = "hh:mm";
            time.Value = DateTime.Now;
            DateTime t = new DateTime();

        }

    }

    class MyEditor : UITypeEditor
    {

        IWindowsFormsEditorService editorService;
        DateTimePicker picker = new DateTimePicker();

        public MyEditor()
        {

            picker.Format = DateTimePickerFormat.Custom;
            picker.CustomFormat = "hh:mm";
            picker.ShowUpDown = true;
            picker.ValueChanged += new EventHandler(picker_ValueChanged);
        }

        void picker_ValueChanged(object sender, EventArgs e)
        {
            this.editorService.CloseDropDown();
        }

        public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
        {

            if (provider != null)
            {
                this.editorService = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
            }

            if (this.editorService != null)
            {

                DateTimePicker tmp = (DateTimePicker)value;
                tmp.CustomFormat = "hh:mm";
                tmp.Text = tmp.Value.ToString();
                picker = tmp;

                this.editorService.DropDownControl(picker);

            }

            return value;

        }

        public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.DropDown;
        }

    }
Unihedron
  • 10,902
  • 13
  • 62
  • 72
guyl
  • 2,158
  • 4
  • 32
  • 58

2 Answers2

4

I finally did it by using strings

public class Tests {
  private string time;

  [Editor(typeof(TimePickerEditor), typeof(UITypeEditor))]
  public string Time {
    get { return time; }
    set { time = value; }
  }

  internal class TimePickerEditor : UITypeEditor {
    IWindowsFormsEditorService editorService;
    DateTimePicker picker = new DateTimePicker();
    string time;

    public TimePickerEditor() {
      picker.Format = DateTimePickerFormat.Custom;
      picker.CustomFormat = "mm:HH";
      picker.ShowUpDown = true;
    }

    public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value) {
      if (provider != null) {
        this.editorService = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
      }
      if (this.editorService != null) {
        if (value == null) {
          time = DateTime.Now.ToString("HH:mm");
        }
        this.editorService.DropDownControl(picker);
        value = picker.Value.ToString("HH:mm");
      }
      return value;
    }

    public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context) {
      return UITypeEditorEditStyle.DropDown;
    }
  }
}
LarsTech
  • 80,625
  • 14
  • 153
  • 225
guyl
  • 2,158
  • 4
  • 32
  • 58
2

You need to implements a System.ComponentModel.TypeConverter that converts between your object and a string, rather than using string itself. PropertyGrid uses TypeConverter classes internally... although it's not very obvious :)

frogatto
  • 28,539
  • 11
  • 83
  • 129
MattDavey
  • 8,897
  • 3
  • 31
  • 54
  • I am familiar with typeConverters and created a lot of then , yet this way is a little more easy. thank for the inputs – guyl Dec 16 '10 at 14:40