如何将 DateTimePicker 设置为只读?
我有一个 DateTimePicker (可为空版本),我需要只读它。 如果显示被禁用,我对显示不满意,所以想知道是否有人有一个关于如何停止现场更新的好例子?
谢谢。
I have a DateTimePicker (nullable version) that I need to be read only. I'm not happy with the display if it is disabled, so wanted to know if anyone had a nifty example of how to stop updates on the field?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
我知道这已经很老了,但为了帮助其他人搜索这个(因为这是我通过谷歌找到的第一个)你可以使用:
使它的行为与文本框的行为相同
this.textbox1.ReadOnly =真
I know this is very old but to help anyone else searching for this (As it was the first one I found through google) You can use:
to make it act the same way as a a textbox would with
this.textbox1.ReadOnly = true
您可以挂钩 Changed 事件,并将值设置回您想要的值(如果不同)——这样您就可以覆盖任何更改原因(通过鼠标或键盘)。
您是否考虑过使用不同的控件,例如只读文本框甚至标签控件?
You could hook the Changed event, and set the value back to your desired value (if different) -- this way you'll cover any cause for change (via mouse, or keyboard)
Have you considered using a different control, such as a read only textbox or even a label control?
这个问题——六年后——似乎仍然引起了一些兴趣,所以我将投入我的 2 美分:对我有用的是 1) 创建一个 UserControl 并将基类更改为 DateTimePicker 2) 拍摄一个小位图快照每当值更改时控件 3) 拦截 WM_PAINT 消息,如果我们的控件被禁用,则绘制位图而不是控件。
(注意:designer.cs 中的 AutoScaleMode 属性会导致编译错误,因此只需删除)
This question--after six years--still seems to get some interest so I'll throw in my 2 cents: What works for me is 1) Make a UserControl and change the base class to DateTimePicker 2) Take a little bitmap snapshot of the control whenever the value changes 3) Intercept the WM_PAINT message and if our control is disabled draw the bitmap instead of the control.
(Note: AutoScaleMode property in designer.cs makes compile error so just remove)
我这样做的一种方法是将日期的允许范围简单地限制为单个值。
One way I've done this is to simply constrain the allowable range of dates to a single value.
另一个建议:使用文本框(或标签)并使其只读。 在其中显示您想要的值。
如果您必须有一个日期时间选择器,请创建一个日期时间选择器和一个文本框。 将它们叠放在一起。 当您需要日期时间选择器时,隐藏文本框。 ETC。
As another suggestion: Use a textbox (or a label) and make it read only. Display the value you want in it.
If you have to have a datetimepicker, create both a datetimepicker and a textbox. Lay them on top of each other. When you need the datetimepicker, hid the textbox. Etc.
按键时:e.SuppressKeyPress = true; 为我工作,仍然可以选择日期,但无法手动输入
on Keydown: e.SuppressKeyPress = true; worked for me, can still pick a date, but can't enter manually
为什么? 如果是因为禁用的文本框看起来很奇怪,您可以更改禁用的样式以使其看起来正常,或者以更漂亮的方式指示它仅通过日期选择器接受输入。 它可能没有边框,也就是说它不是一个真正的文本框。
Why? If it's because a disabled text box looks weird, you can just change the disabled style to make it look normal, or indicate in a prettier way that it accepts input only through the date picker. Possibly have no borders on it, to say it's not really a text box.
我知道它已经过时了很多年,但我讨厌发现仍然相关但没有解决方案的问题。 顺便说一句,您可能不想禁用它的原因是您无法轻松显示工具提示和自定义背景色(尽管这可以更轻松地解决)
无论如何,我找到了解决方案(感谢其他地方的其他人)我相信这个问题是这样问的。 我已经有一个 DateTimePicker 的自定义控件。
然后我添加了一个只读属性。 我的问题是在 VB.Net 项目中,
如果 Readonly = True,则覆盖 KeyDown 事件并标记为已处理。
这将处理箭头键和 F4 下拉键。
然后要禁用下拉列表,请覆盖 WndProc 方法:
为了使其在我的 OnPaint 覆盖中看起来为只读,我有以下内容:
I know it's years out of date but i hate finding problems that are still relevant with no solution. By the way, the reason you might not want to have it disabled is that you cannot easily display tooltips and custom backcolour (although this can be more easily worked around)
Anyway, I found the solution to this (thanks to others on here elsewhere) that i believe the question asks. I already had a custom control for DateTimePicker.
I then added a Readonly property. My problem was in a VB.Net proj
Override the KeyDown event and mark as handled if Readonly = True.
This takes care of the arrow keys and the F4 drop down key.
Then to disable the dropdown, override the WndProc method:
To make it look Readonly in my OnPaint override, i have the following:
处理 DateTimePicker.MouseClick 事件并将 event.handled 设置为 true
handle the DateTimePicker.MouseClick Event and set the event.handled to true
只获取 Changed 事件并设置 e.Cancel = true 怎么样?
How about just picking up the Changed event and setting e.Cancel = true?
这不是最好的方法,但这确实会阻止 keyUp 更新日期时间
Not the nicest way to do it but this does stop the update of datetime by keyUp
我们必须首先记住原始日期时间。
过程 TForm1.dtpDateClick(发件人: TObject);
开始
dtpDate.DateTime := oldDtpDate;
结束;
We have to remember original datetime firstly.
procedure TForm1.dtpDateClick(Sender: TObject);
begin
dtpDate.DateTime := oldDtpDate;
end;
这是简单而完美的解决方案之一。
It is one of the simple and perfect solutions.
继承自
DateTimePicker
,您可以在必要时重写WndProc
以将日期文本绘制为只读。通过以下实现,您可以绑定到
ReadOnly
,也可以选择绑定到HasValue
以显示(No date)
,在我看来,这是比内置CheckBox
的默认行为更好。Inheriting from
DateTimePicker
you can overrideWndProc
to draw the date text read-only when necessary.With the following implementation you can bind to
ReadOnly
, and optionally also bind toHasValue
to show(No date)
which, in my opinion, is better than the default behaviour with the built-inCheckBox
.dateTimePicker2.Enabled = false;
dateTimePicker2.Enabled = false;
尝试这个:
Try this: