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
最满意答案
RepositoryItemTextEdit
类用于保存TextEdit
类就地实例的属性。 技术上TextEdit
类是保存TextBoxMaskBox
类的实例的TextBoxMaskBox
。TextBoxMaskBox
类是从System.Windows.Forms.TextBox
类继承的。
因此,您可以在自己的TextBoxMaskBox
后代中创建自己的RepositoryItemTextEdit
,TextEdit
和TextBoxMaskBox
后代并覆盖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 ofTextEdit
class. TechnicallyTextEdit
class is box which holds the instance ofTextBoxMaskBox
class.TextBoxMaskBox
class is inherited fromSystem.Windows.Forms.TextBox
class.
So, you can create your ownRepositoryItemTextEdit
,TextEdit
andTextBoxMaskBox
descendants and overrideWndProc
method in your ownTextBoxMaskBox
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 toRepositoryItemPasteHandlerEdit
class. You can use this:private void repositoryItemPasteHandlerEdit1_OnPaste(object sender, PasteEventArgs e) { MessageBox.Show(e.Text); if (...) // Perform some checks. e.Cancel = true; }
相关问答
更多-
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
可以通过两种方式在工作线程的上下文中调用主线程组件的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 ...
-
正确覆盖WndProc(Properly overriding WndProc)[2022-12-09]
正如RM的回答所述,你的消息处理方法可以调用inherited WndProc(Message)而不是inherited ,这样可以正常工作。 但是,通过引入与正在处理的消息具有相同名称的方法,您可以了解正在处理的特定消息的知识。 所以你可能会发现使用消息方法而不是重写WndProc更容易,例如: type TMyControl = class(...) private procedure WMWindowPosChanged(var Message: TMessage); message ... -
当我们需要将光标保持为设置时,如何使用WndProc移动控件?(Set SizeAll cursor while moving control by handling NC_HITTEST)[2023-11-07]
你也应该处理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函数通过更改与特定窗口类关联的窗口过程来创建 ...