我正在开发一个带有两个DateTimePicker
控件的WinForms UI。最初,我想将控件的值设置为null,直到用户选择日期。如果用户未选择日期,则会将值传递给数据库。
默认采用当前日期。
你能不能提出建议或附上一段能帮助我的代码。
我已尝试设置MinDate
属性但不起作用。
代码位于C#.Net。
谢谢, 马纳里。
答案 0 :(得分:14)
我认为最好的解决方案是使用内置复选框,告诉用户是否指定了值。
设置控件属性ShowCheckBox = true
将值绑定到它时,请执行类似
的操作 if (value == DateTime.MinValue) {
datePicker.Checked = false;
} else {
datePicker.Checked = true;
datePicker.Value = value;
}
回读值时,请检查Checked
属性。
如果您在取消选中时不喜欢显示的值,则可以将其与其他建议结合使用。
if (!datePicker.Checked) {
// hide date value since it's not set
datePicker.CustomFormat = " ";
datePicker.Format = DateTimePickerFormat.Custom;
} else {
datePicker.CustomFormat = null;
datePicker.Format = DateTimePickerFormat.Long; // set the date format you want.
}
答案 1 :(得分:8)
我知道这是一篇较旧的帖子,但其他人可能仍然觉得这很有用。它相当优雅和简洁。我在.Net 4.0 DateTimePicker中使用它,但早期版本应该使用最小的调整。它利用MinDate和Checked。
// Use ValueChanged to decide if the value should be displayed:
dateTimePicker1.ValueChanged += (s, e) => { dateTimePicker1.CustomFormat = (dateTimePicker1.Checked && dateTimePicker1.Value != dateTimePicker1.MinDate) ? "MM/dd/yyyy" : " "; };
//When getting the value back out, use something like the following:
DateTime? dt = (dateTimePicker1.Checked && dateTimePicker1.Value != dateTimePicker1.MinDate) ? (DateTime?) dateTimePicker1.Value : null;
// or
DateTime dt2 = (dateTimePicker1.Checked && dateTimePicker1.Value != dateTimePicker1.MinDate) ? dateTimePicker1.Value : DateTime.MinValue;
答案 2 :(得分:2)
要在DateTimePicker
控件中显示空值,请在Designer.cs
文件中进行以下更改:
this.DateTimePicker.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
this.DateTimePicker.CustomFormat = " ";
当用户选择日期时,编码控件的ValueChanged
属性并根据您的需要制作格式。这将在控件中显示日期。
以上只显示UI中的控件为空白,但默认值仍为当前日期。
如果要将值设置/初始化为null或空字符串:
temp = DateTimePicker.CustomFormat.ToString();
这是我找到的最简单的解决方法。
答案 3 :(得分:2)
假设dtpStartDate
是你的DateTimePicker控件。
在Page_Load -
中编写此代码`dtpStartDate.Format = DateTimePickerFormat.Custom;
dtpStartDate.CustomFormat = " ";`
将此代码写入Value_Changed事件 -
`startDate = Convert.ToString(dtpStartDate.Value);
dtpStartDate.Format = DateTimePickerFormat.Short;`
这里选择'short'作为格式。您可以选择“长”和其他可用选项。
答案 4 :(得分:1)
可能需要使用TextBox的Appereance创建UserControl,当用户单击Calender Show时,当您选择Date时将其设置为TextBox。它将允许空值。
DateTimePicker不允许null。
答案 5 :(得分:1)
答案 6 :(得分:0)
这是我使用的可以为空的日期时间选择器的代码:
Public Class DateTimePickerNullable
Inherits DateTimePicker
Public Shadows Event GotFocus(sender As Object, e As EventArgs)
Public Shadows Event LostFocus(sender As Object, e As EventArgs)
Private fvalue As DateTime?
Private NoValueChange As Boolean = False
Public Shadows Property Value As DateTime?
Get
Return fvalue
End Get
Set(value As DateTime?)
fvalue = value
If fvalue Is Nothing Then
Me.Format = DateTimePickerFormat.Custom
Me.CustomFormat = Space(Now.ToShortDateString.Length)
NoValueChange = True
MyBase.Value = Now
NoValueChange = False
Else
Me.Format = DateTimePickerFormat.Short
NoValueChange = True
MyBase.Value = value.Value
NoValueChange = False
End If
End Set
End Property
Protected Overrides Sub OnValueChanged(eventargs As EventArgs)
If Not NoValueChange Then
Value = MyBase.Value
End If
MyBase.OnValueChanged(eventargs)
End Sub
Protected Overrides Sub OnKeyDown(e As KeyEventArgs)
If e.KeyCode = Keys.Delete Then
Value = Nothing
End If
If Me.Format = DateTimePickerFormat.Custom Then
Try
If Char.IsNumber(ChrW(e.KeyCode)) Then
If Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern.ToLower.StartsWith("m") Then
Value = New Date(Now.Year, CInt(ChrW(e.KeyCode).ToString), Now.Day)
Else
Value = New Date(Now.Year, Now.Month, CInt(ChrW(e.KeyCode).ToString))
End If
SendKeys.Send(ChrW(e.KeyCode))
Exit Sub
End If
Catch
End Try
End If
MyBase.OnKeyDown(e)
End Sub
Private HasFocus As Boolean = False
Protected Overrides Sub OnGotFocus(e As EventArgs)
MyBase.OnGotFocus(e)
If Not HasFocus Then
HasFocus = True
RaiseEvent GotFocus(Me, New EventArgs)
End If
End Sub
Protected Overrides Sub OnValidated(e As EventArgs)
MyBase.OnValidated(e)
HasFocus = False
RaiseEvent LostFocus(Me, New EventArgs)
End Sub
End Class
答案 7 :(得分:-1)
dateTimePicker1.CustomFormat = " ";