1

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

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

  1. 如果我提示 OnCloseUp - 键盘输入将不会执行此事件
  2. 如果我提示 OnValueChanged - 每次更改日期时都会触发事件
  3. 如果我提示 OnLeave - 事件会触发一些。例如,单击工具条时不会触发。我想避免这种方法,因为它只会在用户点击离开控件时触发。

所以基本上,我试图想出在用户从 dateTimePicker 控件中选择日期后提示用户的最佳方式。

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

4

3 回答 3

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
于 2009-02-12T17:13:40.990 回答
1

“选择日期”是指:

  1. 用鼠标选择日期,或
  2. 使用键盘输入/更改日期,然后将焦点移至另一个控件。

那么,OnCloseUpOnValidate/的组合怎么样OnLeave

从观察OnValueChanged事件开始。如果有人开火,请设置一个更改的标志。

如果他们使用鼠标进行选择,您可以使用提示符显示OnCloseUp并重置您更改的标志。然后OnValueChanged再次关注事件。

OnValidateorOnLeave触发,并且您的标志已设置(大概是在使用键盘更改日期之后),然后显示提示。

于 2009-02-12T17:14:49.413 回答
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
于 2009-02-17T13:45:02.420 回答