VB.NET Update Issue in DataGridView Cell for TimePicker: Enter Key Does not Update Displayed Value

不想你离开。 提交于 2019-12-05 20:24:45

My initial comment was definitely wrong. After reproducing your problem, I noticed something funny/frustrating: placing a breakpoint within the EditingControlWantsInputKey method on the following line, allowed the updates to occur as expected (a debugging fix of the problem):

Select key And Keys.KeyCode

That was beyond annoying and in my frustration, I frantically tested month entries in succession: 1, 2, 3, ..., 12

That's when I noticed: months 10-12 updated correctly. In fact, entering 01 updated correctly. My subsequent research led me to the comments section of this article, where user Dean Wiles in his comment titled Changes lost using Tab or Enter provides this Microsoft support source which indicates that:

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.

And this can be addressed by adding the following vb.net version of his code to your TimeEditingControl class:

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

C# Conversion

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{

    switch (keyData & Keys.KeyCode)
    {
        case Keys.Enter:
        case Keys.Tab:
            this.dataGridView.Focus();
            break; 
    }

    return base.ProcessCmdKey(ref msg, keyData);

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!