在 propertygrid 中使用 datetimepicker 选择时间

发布于 2024-10-07 22:12:29 字数 2082 浏览 1 评论 0原文

我试图使用 propertygriddatetimepicker 仅选择一个时间(小时和分钟)。
我创建了自己的编辑器。
问题是它没有显示文本中的值,并且该值也可以有日历:

这里有代码:

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;
        }

    }

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;
        }

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绝不放开 2024-10-14 22:12:29

我终于用字符串做到了

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;
    }
  }
}

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;
    }
  }
}
稳稳的幸福 2024-10-14 22:12:29

您需要实现一个 System.ComponentModel.TypeConverter 来在对象和字符串之间进行转换,而不是使用字符串本身。 PropertyGrid 在内部使用 TypeConverter 类...虽然不是很明显:)

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文