gpt4 book ai didi

TimePicker : Enter Key Does not Update Displayed Value 的 DataGridView 单元格中的 VB.NET 更新问题

转载 作者:行者123 更新时间:2023-12-02 04:47:17 26 4
gpt4 key购买 nike

我有一个奇怪的问题,即在 VB.NET 中重载 DataGridView 的单元格以使用 DateTimePicker(允许用户选择日期和时间)。我拿了Microsoft implementation of a Calendar picker并将其修改为日历和时间选择器。当我输入新日期并按 ENTER 键时,DataGridView 单元格中显示的日期是旧日期。

只有在该键上再次按下 ENTER 或单击该单元格并单击离开时才会显示新值。

如果我单击该单元格,输入新日期,然后单击另一个单元格,该单元格会显示正确的日期时间值。然后,显示的日期与我输入的相符。它使用的是我遇到问题的 ENTER 键。

我修改了微软重载的代码如下:

Public Class TimeColumn
Inherits DataGridViewColumn

Public Sub New()
MyBase.New(New TimeCell())
End Sub

Public Overrides Property CellTemplate() As DataGridViewCell
Get
Return MyBase.CellTemplate
End Get
Set(ByVal value As DataGridViewCell)
' Ensure that the cell used for the template is a TimeCell.
If (value IsNot Nothing) AndAlso _
Not value.GetType().IsAssignableFrom(GetType(TimeCell)) _
Then
Throw New InvalidCastException("Must be a TimeCell")
End If
MyBase.CellTemplate = value
End Set
End Property

End Class

Public Class TimeCell
Inherits DataGridViewTextBoxCell

Public Sub New()
' Use the short date format.
Me.Style.Format = "MM/dd/yy h:mm tt"
'm_isTime = True
End Sub
Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, _
ByVal initialFormattedValue As Object, _
ByVal dataGridViewCellStyle As DataGridViewCellStyle)

' Set the value of the editing control to the current cell value.
MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, _
dataGridViewCellStyle)

Dim ctl As New TimeEditingControl
ctl = CType(DataGridView.EditingControl, TimeEditingControl)
ctl.CustomFormat = "MM/dd/yyyy h:mm tt"
Me.Value = ctl.Value

End Sub

Public Overrides ReadOnly Property EditType() As Type
Get
' Return the type of the editing contol that TimeCell uses.
Return GetType(TimeEditingControl)
End Get
End Property

Public Overrides ReadOnly Property ValueType() As Type
Get
' Return the type of the value that TimeCell contains.
Return GetType(DateTime)
End Get
End Property

Public Overrides ReadOnly Property DefaultNewRowValue() As Object
Get
' Use the current date and time as the default value.
Return DateTime.Now
End Get
End Property

End Class

Class TimeEditingControl
Inherits DateTimePicker
Implements IDataGridViewEditingControl

Private dataGridViewControl As DataGridView
Private valueIsChanged As Boolean = False
Private rowIndexNum As Integer

Public Sub New()
Me.Format = DateTimePickerFormat.Custom
End Sub
Public Property EditingControlFormattedValue() As Object _
Implements IDataGridViewEditingControl.EditingControlFormattedValue

Get
Return Me.Value.ToShortDateString()
End Get

Set(ByVal value As Object)
If TypeOf value Is String Then
Me.Value = DateTime.Parse(CStr(value))
End If
End Set

End Property

Public Function GetEditingControlFormattedValue(ByVal context _
As DataGridViewDataErrorContexts) As Object _
Implements IDataGridViewEditingControl.GetEditingControlFormattedValue

Dim result, tempDateTime As DateTime
tempDateTime = Me.Value

Return tempDateTime.ToString
End Function


Public Sub ApplyCellStyleToEditingControl(ByVal dataGridViewCellStyle As _
DataGridViewCellStyle) _
Implements IDataGridViewEditingControl.ApplyCellStyleToEditingControl

Me.Font = dataGridViewCellStyle.Font
Me.CalendarForeColor = dataGridViewCellStyle.ForeColor
Me.CalendarMonthBackground = dataGridViewCellStyle.BackColor

End Sub

Public Property EditingControlRowIndex() As Integer _
Implements IDataGridViewEditingControl.EditingControlRowIndex

Get
Return rowIndexNum
End Get
Set(ByVal value As Integer)
rowIndexNum = value
End Set

End Property



Public Function EditingControlWantsInputKey(ByVal key As Keys, _
ByVal dataGridViewWantsInputKey As Boolean) As Boolean _
Implements IDataGridViewEditingControl.EditingControlWantsInputKey
Dim lDateTime As DateTime
' Let the DateTimePicker handle the keys listed.
Select key And Keys.KeyCode

Case Keys.Enter

Console.WriteLine(DateTime.Now.ToString & "." & DateTime.Now.Millisecond.ToString & ": EditingControlWantsInputKey--Detected [ENTER] key.")
End Select
valueIsChanged = True
Console.WriteLine("EditingControlWantsInputKey (" & Me.Value & ")")
Dim lDataGridView1 As DataGridView
lDataGridView1 = Me.dataGridViewControl
lDataGridView1.BeginEdit(True)
lDataGridView1.BeginEdit(False)
Return True


End Function

Public Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) _
Implements IDataGridViewEditingControl.PrepareEditingControlForEdit

' No preparation needs to be done.

End Sub

Public ReadOnly Property RepositionEditingControlOnValueChange() _
As Boolean Implements _
IDataGridViewEditingControl.RepositionEditingControlOnValueChange

Get
Return False
End Get

End Property

Public Property EditingControlDataGridView() As DataGridView _
Implements IDataGridViewEditingControl.EditingControlDataGridView

Get
Return dataGridViewControl

End Get
Set(ByVal value As DataGridView)
dataGridViewControl = value
End Set

End Property

Public Property EditingControlValueChanged() As Boolean _
Implements IDataGridViewEditingControl.EditingControlValueChanged

Get
Return valueIsChanged
End Get
Set(ByVal value As Boolean)
valueIsChanged = value
End Set

End Property

Public ReadOnly Property EditingControlCursor() As Cursor _
Implements IDataGridViewEditingControl.EditingPanelCursor

Get
Return MyBase.Cursor
End Get

End Property

Protected Overrides Sub OnValueChanged(ByVal eventargs As EventArgs)

' Notify the DataGridView that the contents of the cell have changed.
valueIsChanged = True
Me.EditingControlDataGridView.NotifyCurrentCellDirty(True)
MyBase.OnValueChanged(eventargs)

End Sub

End Class

到目前为止我的发现:

  1. 奇怪的是 Public Property EditingControlValueChanged() As Boolean Implements IDataGridViewEditingControl.EditingControlValueChanged 根本没有检测到 ENTER 键!您可以看到我为该事件输入了一些调试代码,以查看它是否检测到按下了 ENTER 键。

  2. 事件 Public Property EditingControlValueChanged() As Boolean Implements IDataGridViewEditingControl.EditingControlValueChanged 直到第二次按下 ENTER 键时才检测到值更改( bool 值IsChanged 设置为 false) ,此时它被设置为 true。

  3. 我还尝试更新 DataGridView 的 CellEndEdit 事件中的单元格值,但这没有用。

  4. 我已经更新了我的 Visual Studio Express 2013,以防它是一个 Micosoft 错误。

有什么想法可以让显示的值与按下 ENTER 键时输入的值相匹配吗?谢谢。

最佳答案

我最初的评论肯定是错误的。重现您的问题后,我注意到一些有趣/令人沮丧的事情:在下一行的 EditingControlWantsInputKey 方法中放置一个断点,允许更新按预期发生(调试修复问题):

Select key And Keys.KeyCode

这实在是太烦人了,让我感到沮丧的是,我疯狂地连续测试了月份条目:1、2、3、...、12

就在那时我注意到:第 10-12 个月更新正确。事实上,输入 01 已正确更新。我随后的研究使我找到了 comments section of this article ,其中用户 Dean Wiles 在其标题为 Changes lost using Tab or Enter 的评论中提供了 this Microsoft support source这表明:

The ValueChanged event of the [DateTimePicker] is raised only after you type any one of the following:

  • All the digits of a year.
  • All the digits of a day.
  • All the digits of a month.

这可以通过将以下他的代码的 vb.net 版本添加到您的 TimeEditingControl 类来解决:

Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean
Select Case keyData And Keys.KeyCode
Case Keys.Enter, Keys.Tab
Me.dataGridViewControl.Focus()
Exit Select
End Select

Return MyBase.ProcessCmdKey(msg, keyData)
End Function

关于TimePicker : Enter Key Does not Update Displayed Value 的 DataGridView 单元格中的 VB.NET 更新问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31889198/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com