首页 \ 问答 \ Datepicker - 仅限年份?(Datepicker - year only?)

Datepicker - 仅限年份?(Datepicker - year only?)

我想使用Datepicker控件,但只显示YEAR并显示给用户进行编辑。 是否可以删除月和日字段?


I want to use Datepicker control, but with only YEAR displayed and shown to user for edit. Is it possible to remove the month and day fields?


原文:https://stackoverflow.com/questions/9929816
更新时间:2023-07-12 14:07

最满意答案

RepositoryItemTextEdit类用于保存TextEdit类就地实例的属性。 技术上TextEdit类是保存TextBoxMaskBox类的实例的TextBoxMaskBoxTextBoxMaskBox类是从System.Windows.Forms.TextBox类继承的。
因此,您可以在自己的TextBoxMaskBox后代中创建自己的RepositoryItemTextEditTextEditTextBoxMaskBox后代并覆盖WndProc方法。
这里是一个例子:

[UserRepositoryItem("RegisterPasteHandlerEdit")]
public class RepositoryItemPasteHandlerEdit : RepositoryItemTextEdit
{
    static RepositoryItemPasteHandlerEdit()
    {
        RegisterPasteHandlerEdit();
    }

    static private readonly object EventPaste;

    public const string CustomEditName = "PasteHandlerEdit";

    public RepositoryItemPasteHandlerEdit() { }

    public override string EditorTypeName => CustomEditName;

    public static void RegisterPasteHandlerEdit()
    {
        Image img = null;
        EditorRegistrationInfo.Default.Editors.Add(new EditorClassInfo(CustomEditName, typeof(PasteHandlerEdit), typeof(RepositoryItemPasteHandlerEdit), typeof(TextEditViewInfo), new TextEditPainter(), true, img));
    }

    public override void Assign(RepositoryItem item)
    {
        BeginUpdate();
        try
        {
            base.Assign(item);
            var source = item as RepositoryItemPasteHandlerEdit;
            if (source == null) return;

            Events.AddHandler(EventPaste, source.Events[EventPaste]);
        }
        finally
        {
            EndUpdate();
        }
    }

    public event PasteEventHandler OnPaste
    {
        add
        {
            Events.AddHandler(EventPaste, value);
        }
        remove
        {
            Events.RemoveHandler(EventPaste, value);
        }
    }

    protected internal void RaiseOnPaste(PasteEventArgs e)
    {
        if (IsLockEvents)
            return;

        var handler = (PasteEventHandler)Events[EventPaste];
        handler?.Invoke(GetEventSender(), e);
    }
}

[ToolboxItem(true)]
public class PasteHandlerEdit : TextEdit
{
    static PasteHandlerEdit()
    {
        RepositoryItemPasteHandlerEdit.RegisterPasteHandlerEdit();
    }

    public PasteHandlerEdit() { }

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public new RepositoryItemPasteHandlerEdit Properties => base.Properties as RepositoryItemPasteHandlerEdit;

    public override string EditorTypeName => RepositoryItemPasteHandlerEdit.CustomEditName;

    protected override TextBoxMaskBox CreateMaskBoxInstance() => new PasteHandlerMaskBox(this);

    protected override void CreateMaskBox()
    {
        base.CreateMaskBox();

        var pasteHandlerMaskBox = MaskBox as PasteHandlerMaskBox;
        pasteHandlerMaskBox.OnPaste += PasteHandlerMaskBox_OnPaste;
    }

    private void PasteHandlerMaskBox_OnPaste(object sender, PasteEventArgs e) => Properties.RaiseOnPaste(e);
}

public class PasteHandlerMaskBox : TextBoxMaskBox
{
    private const int WM_PASTE = 0x0302;

    public PasteHandlerMaskBox(TextEdit ownerEdit) : base(ownerEdit) { }

    public event PasteEventHandler OnPaste;

    protected override void WndProc(ref Message msg)
    {
        if (msg.Msg == WM_PASTE)
        {
            var e = new PasteEventArgs();

            OnPaste?.Invoke(this, e);

            if (e.Cancel)
                return;
        }

        base.WndProc(ref msg);
    }
}

public class PasteEventArgs : CancelEventArgs
{
    public PasteEventArgs() { }
    public PasteEventArgs(bool cancel) : base(cancel) { }

    public string Text => Clipboard.GetText();
}

public delegate void PasteEventHandler(object sender, PasteEventArgs e);

在这个例子中, OnPaste事件被添加到RepositoryItemPasteHandlerEdit类中。 你可以使用这个:

private void repositoryItemPasteHandlerEdit1_OnPaste(object sender, PasteEventArgs e)
{
    MessageBox.Show(e.Text);

    if (...) // Perform some checks.
        e.Cancel = true;
}

RepositoryItemTextEdit class is used to hold the properties for in-place instance of TextEdit class. Technically TextEdit class is box which holds the instance of TextBoxMaskBox class. TextBoxMaskBox class is inherited from System.Windows.Forms.TextBox class.
So, you can create your own RepositoryItemTextEdit, TextEdit and TextBoxMaskBox descendants and override WndProc method in your own TextBoxMaskBox descendant.
Here is example:

[UserRepositoryItem("RegisterPasteHandlerEdit")]
public class RepositoryItemPasteHandlerEdit : RepositoryItemTextEdit
{
    static RepositoryItemPasteHandlerEdit()
    {
        RegisterPasteHandlerEdit();
    }

    static private readonly object EventPaste;

    public const string CustomEditName = "PasteHandlerEdit";

    public RepositoryItemPasteHandlerEdit() { }

    public override string EditorTypeName => CustomEditName;

    public static void RegisterPasteHandlerEdit()
    {
        Image img = null;
        EditorRegistrationInfo.Default.Editors.Add(new EditorClassInfo(CustomEditName, typeof(PasteHandlerEdit), typeof(RepositoryItemPasteHandlerEdit), typeof(TextEditViewInfo), new TextEditPainter(), true, img));
    }

    public override void Assign(RepositoryItem item)
    {
        BeginUpdate();
        try
        {
            base.Assign(item);
            var source = item as RepositoryItemPasteHandlerEdit;
            if (source == null) return;

            Events.AddHandler(EventPaste, source.Events[EventPaste]);
        }
        finally
        {
            EndUpdate();
        }
    }

    public event PasteEventHandler OnPaste
    {
        add
        {
            Events.AddHandler(EventPaste, value);
        }
        remove
        {
            Events.RemoveHandler(EventPaste, value);
        }
    }

    protected internal void RaiseOnPaste(PasteEventArgs e)
    {
        if (IsLockEvents)
            return;

        var handler = (PasteEventHandler)Events[EventPaste];
        handler?.Invoke(GetEventSender(), e);
    }
}

[ToolboxItem(true)]
public class PasteHandlerEdit : TextEdit
{
    static PasteHandlerEdit()
    {
        RepositoryItemPasteHandlerEdit.RegisterPasteHandlerEdit();
    }

    public PasteHandlerEdit() { }

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public new RepositoryItemPasteHandlerEdit Properties => base.Properties as RepositoryItemPasteHandlerEdit;

    public override string EditorTypeName => RepositoryItemPasteHandlerEdit.CustomEditName;

    protected override TextBoxMaskBox CreateMaskBoxInstance() => new PasteHandlerMaskBox(this);

    protected override void CreateMaskBox()
    {
        base.CreateMaskBox();

        var pasteHandlerMaskBox = MaskBox as PasteHandlerMaskBox;
        pasteHandlerMaskBox.OnPaste += PasteHandlerMaskBox_OnPaste;
    }

    private void PasteHandlerMaskBox_OnPaste(object sender, PasteEventArgs e) => Properties.RaiseOnPaste(e);
}

public class PasteHandlerMaskBox : TextBoxMaskBox
{
    private const int WM_PASTE = 0x0302;

    public PasteHandlerMaskBox(TextEdit ownerEdit) : base(ownerEdit) { }

    public event PasteEventHandler OnPaste;

    protected override void WndProc(ref Message msg)
    {
        if (msg.Msg == WM_PASTE)
        {
            var e = new PasteEventArgs();

            OnPaste?.Invoke(this, e);

            if (e.Cancel)
                return;
        }

        base.WndProc(ref msg);
    }
}

public class PasteEventArgs : CancelEventArgs
{
    public PasteEventArgs() { }
    public PasteEventArgs(bool cancel) : base(cancel) { }

    public string Text => Clipboard.GetText();
}

public delegate void PasteEventHandler(object sender, PasteEventArgs e);

In this example the OnPaste event is added to RepositoryItemPasteHandlerEdit class. You can use this:

private void repositoryItemPasteHandlerEdit1_OnPaste(object sender, PasteEventArgs e)
{
    MessageBox.Show(e.Text);

    if (...) // Perform some checks.
        e.Cancel = true;
}

相关问答

更多
  • 可以通过两种方式在工作线程的上下文中调用主线程组件的WndProc()方法: 工作线程直接调用组件的WindowProc属性或其Perform()方法。 工作线程通过不安全地使用TWinControl.Handle属性来窃取组件窗口的所有权。 Handle属性getter不是线程安全的。 如果工作线程在主线程重新创建组件窗口的同一时刻从Handle属性读取( TWinControl窗口不是持久的 - 各种运行时条件可以动态地重新创建它们而不影响大部分UI逻辑),那么存在一个竞争条件,可以允许工作线程在其自己 ...
  • 实际上,据我所知,WPF中使用HwndSource和HwndSourceHook这样的事情确实有可能。 在MSDN上查看此线程为例。 (相关代码如下) // 'this' is a Window HwndSource source = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle); source.AddHook(new HwndSourceHook(WndProc)); private static IntPtr WndProc(Int ...
  • RepositoryItemTextEdit类用于保存TextEdit类就地实例的属性。 技术上TextEdit类是保存TextBoxMaskBox类的实例的TextBoxMaskBox 。 TextBoxMaskBox类是从System.Windows.Forms.TextBox类继承的。 因此,您可以在自己的TextBoxMaskBox后代中创建自己的RepositoryItemTextEdit , TextEdit和TextBoxMaskBox后代并覆盖WndProc方法。 这里是一个例子: [Use ...
  • 你正在使用的方法是完成它的唯一方法,但你是对的,它超越了默认组合框的顶部,并且在使用组合框控件时无法解决这个问题。 要真正获得自定义绘制的组合框,您需要在对象树中向上移动一级,但最终必须重新创建组合框的几乎所有功能。 The method you are using is the only way to get it done, but you are right, it draws over the top of the default combobox, and there is no way arou ...
  • 我能够使用ContainerControl.ParentForm来获取对宿主表单的引用,但是在生产环境中,ParentForm返回null,我仍然需要找到解决方案。 我把NewWndProc处理程序放在try / catch中,以防它抛出任何异常,虽然我不确定它可以抛出什么(如果有的话)。 我可能只需要使用Win32函数而不使用Form.Focused和Form.Activate().NET方法。 无论如何,这是代码: public class FocusFormWrapper { #region ...
  • 正如RM的回答所述,你的消息处理方法可以调用inherited WndProc(Message)而不是inherited ,这样可以正常工作。 但是,通过引入与正在处理的消息具有相同名称的方法,您可以了解正在处理的特定消息的知识。 所以你可能会发现使用消息方法而不是重写WndProc更容易,例如: type TMyControl = class(...) private procedure WMWindowPosChanged(var Message: TMessage); message ...
  • 你也应该处理WM_SETCURSOR 。 此外,您应该使用WM_NCLBUTTONDBLCLK来防止双击时控件最大化: protected override void WndProc(ref Message m) { const int WM_NCHITTEST = 0x84; const int WM_SETCURSOR = 0x20; const int WM_NCLBUTTONDBLCLK = 0xA3; const int HTCAPTION = 0x2; i ...
  • 在WndProc方法完成后尝试使用BeginInvoke启动事件: protected override void WndProc(ref Message m) { //0x210 is WM_PARENTNOTIFY if (m.Msg == 0x210 && m.WParam.ToInt32() == 513) //513 is WM_LBUTTONCLICK { Console.WriteLine("## MouseClick on UserControl1 "); ...
  • 使用SetWindowLongPtr(GWLP_WNDPROC)子类化窗口过程时,它将返回正在替换的上一个窗口过程。 您的子类过程必须使用CallWindowProc()而不是DefWindowProc()将未处理的消息传递给上一个过程。 这在文档中说明了 : 使用GWLP_WNDPROC索引调用SetWindowLongPtr会创建用于创建窗口的窗口类的子类。 应用程序可以子类化系统类,但不应该为另一个进程创建的窗口类创建子类。 SetWindowLongPtr函数通过更改与特定窗口类关联的窗口过程来创建 ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。