DateTimePicker控制特定方案中的用法

时间:2009-02-12 17:06:12

标签: .net winforms datetimepicker

此问题与客户为我正在设计的应用程序请求的特定功能有关。基本上,客户希望DateTimePicker在选择日期后提示问题。

这听起来很简单,但是,我很难完成这个简单的任务。

  1. 如果我提示OnCloseUp - 键盘输入将不会执行此事件
  2. 如果我提示OnValueChanged - 每次日期都会触发事件 改变
  3. 如果我提示OnLeave - 事件有点火。什么时候不开火 例如,单击工具条。我想避免使用这种方法,因为它只会在用户点击控件时触发。
  4. 所以基本上,我试图想一下在用户从dateTimePicker控件中选择日期后提示用户的最佳方法。

    我也没有建立自定义控件的问题。我已经开始制作一个,因为我还需要允许NULL值。

3 个答案:

答案 0 :(得分:1)

我会使用OnValueChanged事件。在他们改变价值后,问问题。如果他们回答错误(示例 - 问:你确定吗?答:不是。)然后重置日期选择器并将焦点返回给它。

这个例子有点乱,但是有效。

Private is_reset As Boolean = False
Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged

    Dim answer As Integer
    If Not is_reset Then
        answer = MsgBox("Are you Sure?", MsgBoxStyle.YesNo)
        is_reset = False
    End If

    If answer = MsgBoxResult.No Then
        is_reset = True
        DateTimePicker1.Value = Now
        DateTimePicker1.Select()
    End If

答案 1 :(得分:1)

“选择日期”表示:

  1. 使用鼠标选择日期,或
  2. 使用键盘输入/更改日期,然后将焦点移至另一个控件。
  3. 那么,OnCloseUpOnValidate / OnLeave的组合怎么样?

    首先观看OnValueChanged个事件。如果一个人开火,请设置更改的标志。

    如果他们使用鼠标选择,您可以使用OnCloseUp调出提示并重置已更改的标记。然后再次关注OnValueChanged个事件。

    OnValidateOnLeave触发,并且您的旗帜已设置(可能是在使用键盘更改日期后),然后调出提示。

答案 2 :(得分:0)

我最终编写了一个自定义控件来正确处理这个问题。 我创建了一个带按钮的文本框,并添加了MonthCalendar控件。

文本框+按钮打开MonthCalendar控件。现在选择日期时间的唯一方法是通过MonthCalendar。您无法从文本框中进行选择。我还创建了一个自定义事件,在选择日期时触发。它完美地运作。代码如下:

Public Class CustomDatePicker

  'Variables
  Friend WithEvents cal As MonthCalendar
  Private _isCalendarVisible As Boolean = False
  Private _currentSelectedDate As DateTime = Nothing

  'Events
  Public Event OnDateTimeSet(ByVal sender As Object, ByVal dateValue As DateTime)
  Public Event OnDateCleared(ByVal sender As Object)

  'Constructor
  Public Sub New()
    InitializeComponent()
  End Sub

  'Onload
  Private Sub CustomDatePicker_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'Initially setup calendar
    cal = New MonthCalendar
    cal.Name = "Calendar"
    cal.MaxSelectionCount = 1
    cal.BringToFront()
    cal.Location = New Point(Me.Location.X + 5, Me.Location.Y + 25)
    Me.Parent.Controls.Add(cal)
    cal.Hide()
    _isCalendarVisible = False
  End Sub

  'Returns the currently selected date from the TextBox field
  Public ReadOnly Property CurrentSelectedDate()
    Get
      Return _currentSelectedDate
    End Get
  End Property

  'Display calendar
  Private Sub ShowCalendar()

    'Close any other custom date controls that are open on the parent form
    Dim cont As Control
    For Each cont In Parent.Controls
      If (cont.GetType().Name = "CustomDatePicker") Then
        CType(cont, CustomDatePicker).HideCalendar()
      End If
    Next

    'display the calendar
    If Not (_isCalendarVisible) Then
      tbxSelectedDate.BackColor = Color.Cornsilk
      cal.BringToFront()
      cal.Show()
      cal.Focus()
      _isCalendarVisible = True
      btnCalendarToggle.Checked = True
    End If

  End Sub

  'Hide the Calendar
  Private Sub HideCalendar()
    If (_isCalendarVisible) Then
      tbxSelectedDate.BackColor = Color.White
      cal.Hide()
      _isCalendarVisible = False
      btnCalendarToggle.Checked = False
      tbxSelectedDate.Focus()
    End If
  End Sub

  'Display the selected datetime into the textbox
  Private Sub SetDateTime()
    Me.tbxSelectedDate.Text = FormatDateTime(cal.SelectionRange.Start, DateFormat.LongDate)
    _currentSelectedDate = FormatDateTime(cal.SelectionRange.Start, DateFormat.LongDate)
    RaiseEvent OnDateTimeSet(Me, _currentSelectedDate)
  End Sub

  'Event when selection is made in the Calendar
  Private Sub Calendar_Selection(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DateRangeEventArgs) Handles cal.DateSelected
    SetDateTime()
    HideCalendar()
  End Sub

  'Handle the keyboard events associated with the calendar control
  Private Sub Calendar_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles cal.KeyPress
    If e.KeyChar = ChrW(Keys.Return) Then
      SetDateTime()
      HideCalendar()
    ElseIf e.KeyChar = ChrW(Keys.Escape) Then
      HideCalendar()
    End If
  End Sub

  'Handles keypresses on the textbox field
  Private Sub tbxSelectedDate_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles tbxSelectedDate.KeyUp
    If (e.KeyCode = Keys.Down) Then
      ShowCalendar()
    ElseIf (e.KeyCode = Keys.Delete) Then
      tbxSelectedDate.Text = ""
    End If
  End Sub

  'Show the calendar when button is clicked
  Private Sub btnCalendarToggle_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles btnCalendarToggle.MouseUp
    ToggleCalendar()
  End Sub

  'Show the calendar when button is 'clicked' via ENTER on keyboard
  Private Sub btnCalendarToggle_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles btnCalendarToggle.KeyPress
    If e.KeyChar = ChrW(Keys.Return) Then
      ToggleCalendar()
    End If
  End Sub

  'Toggle calender.  If on, turn off.  If off, turn on.
  Private Sub ToggleCalendar()
    If Not (_isCalendarVisible) Then
      ShowCalendar()
      btnCalendarToggle.Checked = True
    Else
      HideCalendar()
      btnCalendarToggle.Checked = False
    End If
  End Sub

  'When textbox value is changed, check to see if it was cleared.  If cleared, raiseevent.
  Private Sub tbxSelectedDate_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbxSelectedDate.TextChanged
    If (tbxSelectedDate.Text = "") Then
      _currentSelectedDate = Nothing
      RaiseEvent OnDateCleared(Me)
    End If
  End Sub