C# 如何将 DateTimePicker 设置为只读?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/657983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you set a DateTimePicker to be read only?
提问by cjk
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?
我有一个我需要只读的 DateTimePicker(可为空版本)。如果它被禁用,我对显示不满意,所以想知道是否有人有一个很好的例子来说明如何停止现场更新?
Thanks.
谢谢。
采纳答案by Rowland Shaw
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)
您可以挂钩 Changed 事件,并将值设置回您想要的值(如果不同)——这样您就可以覆盖任何更改原因(通过鼠标或键盘)
Have you considered using a different control, such as a read only textbox or even a label control?
您是否考虑过使用不同的控件,例如只读文本框甚至标签控件?
回答by RvdK
handle the DateTimePicker.MouseClick Event and set the event.handled to true
处理 DateTimePicker.MouseClick 事件并将 event.handled 设置为 true
回答by Sudhir Jonathan
"I'm not happy with the display if it is disabled"
“如果它被禁用,我对显示不满意”
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.
为什么?如果是因为禁用的文本框看起来很奇怪,您可以更改禁用的样式以使其看起来正常,或者以更漂亮的方式指示它仅通过日期选择器接受输入。可能没有边框,也就是说它不是真正的文本框。
回答by Sudhir Jonathan
How about just picking up the Changed event and setting e.Cancel = true?
只是拿起 Changed 事件并设置 e.Cancel = true 怎么样?
回答by Adis
Try this:
尝试这个:
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
dateTimePicker1.Value = DateTime.Now;
}
回答by HatSoft
Not the nicest way to do it but this does stop the update of datetime by keyUp
不是最好的方法,但这确实会通过 keyUp 停止更新日期时间
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private DateTime originalValue;
private void dateTimePicker1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
originalValue = dateTimePicker1.Value;
}
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
dateTimePicker1.Value = originalValue;
}
}
回答by Rasulbek
We have to remember original datetime firstly.
procedure TForm1.dtpDateEnter(Sender: TObject); begin oldDtpDate := dtpDate.DateTime; end;
procedure TForm1.dtpDateClick(Sender: TObject); begin dtpDate.DateTime := oldDtpDate; end;
procedure TForm1.dtpDateExit(Sender: TObject); begin dtpDate.DateTime := oldDtpDate; end;
我们必须首先记住原始日期时间。
procedure TForm1.dtpDateEnter(Sender: TObject); begin oldDtpDate := dtpDate.DateTime; end;
程序 TForm1.dtpDateClick(Sender: TObject); 开始 dtpDate.DateTime := oldDtpDate; 结尾;
procedure TForm1.dtpDateExit(Sender: TObject); begin dtpDate.DateTime := oldDtpDate; end;
回答by Sam
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:
我知道这很旧,但为了帮助其他人搜索这个(因为它是我通过谷歌找到的第一个)你可以使用:
this.dateTimePicker1.Enabled = false;
to make it act the same way as a a textbox would with this.textbox1.ReadOnly = true
使其行为与 aa 文本框相同 this.textbox1.ReadOnly = true
回答by Matthew Nash
One way I've done this is to simply constrain the allowable range of dates to a single value.
我这样做的一种方法是简单地将允许的日期范围限制为单个值。
dateTimePicker.MinDate = dateTimePicker.Value;
dateTimePicker.MaxDate = dateTimePicker.Value;
回答by IVSoftware
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)
这个问题 - 六年后 - 似乎仍然引起了一些兴趣,所以我会投入 2 美分:对我有用的是 1) 创建一个 UserControl 并将基类更改为 DateTimePicker 2) 拍摄一个小的位图快照每当值改变时控制控件 3) 拦截 WM_PAINT 消息,如果我们的控件被禁用,则绘制位图而不是控件。(注意:designer.cs 中的 AutoScaleMode 属性会导致编译错误,所以只需删除)
public partial class DateTimePickerWithReadOnly : DateTimePicker
{
Bitmap ReadOnlyImage;
// We maintain a "shadow" control to avoid capturing selections in the snapshot.
// If you use different formatting or styles just make sure the shadow is set to match!
DateTimePicker Shadow = new DateTimePicker();
public DateTimePickerWithReadOnly()
{
InitializeComponent();
CaptureBitmap();
this.ValueChanged += new EventHandler(DateTimePickerWithReadOnly_ValueChanged);
}
private void CaptureBitmap()
{
Shadow.Value = Value;
Shadow.Size = Size;
ReadOnlyImage = new Bitmap(Width, Height);
Shadow.DrawToBitmap(ReadOnlyImage, new Rectangle(0, 0, Size.Width, Size.Height));
}
void DateTimePickerWithReadOnly_ValueChanged(object sender, EventArgs e)
{
CaptureBitmap();
}
protected override void DefWndProc(ref Message m)
{
base.DefWndProc(ref m);
// WM_PAINT is 0x000F
if ((m.Msg == 0x000F) && !Enabled)
{
Graphics g = base.CreateGraphics();
g.DrawImage(ReadOnlyImage, new Rectangle(0, 0, Size.Width, Size.Height));
g.Dispose();
}
}
}