DataGridView 在 Enter/Return 上导航

发布于 2024-08-03 18:21:57 字数 1148 浏览 1 评论 0原文

我正在尝试使用 Enter 键移至 DataGridView 中的下一个(非只读)列。在正常情况下,我处理 KeyDown 事件并且一切正常。

但是,当我编辑字段时,Enter 键始终会完成编辑,然后移至下一个

奇怪的是,我在测试台上得到了以下一系列事件。 (括号中的数字是事件参数的(rowIndex,columnIndex))。

没有备用导航:

按 Enter 键(不编辑字段):

PreviewKeyDown key = Return
KeyDown key = Return
CellLeave (0, 0)
RowLeave (0, 0)
CellValidating (0, 0)
CellValidated (0, 0)
RowEnter (1, 0)
CurrentCellChanged
CellEnter (1, 0)
SelectionChanged
KeyUp key = Return

按 Enter 键(编辑字段时):

CellClick (0, 0)
CellBeginEdit (0, 0)
EditingControlShowing
    (I press Enter now)
CellLeave (0, 0)
RowLeave (0, 0)
CellValidating (0, 0)
CellValidated (0, 0)
CellEndEdit (0, 0)
RowEnter (1, 0)
CurrentCellChanged
CellEnter (1, 0)
SelectionChanged
KeyUp key = Return

即,当我在编辑字段时按 Enter 键时,我永远不会收到 PreviewKeyDown 或 KeyDown 事件。

我尝试在事件处理程序中破解一些备用导航。出于测试目的,我总是编辑 (r0, c0),并通过将 CurrentCell 属性显式设置为 (r0, c1) 来“导航”。

当我在 CellEndEdit 中设置 CurrentCell 时,我最终得到 (r1, c1)。当我在 KeyUp 中设置 CurrentCell 时,我最终得到 (r0, c1),这很好,但前提是它明显选择了 (r1, c0)。我想我可以在导航过程中禁用控件的更新,但这个解决方案似乎不太优雅。

有什么建议吗?我错过了什么吗?

I'm trying to use the Enter key to move to the next (non-read-only) column in a DataGridView. Under normal circumstances, I handle the KeyDown event and everything works great.

However, when I'm editing a field, the Enter key always completes the edit and then moves to the next row.

Weirdly enough, I'm getting the following sequence of events in my test bed. (Numbers in parentheses are (rowIndex, columnIndex) of the event arg).

With no alternate navigation:

Press Enter (without editing the field):

PreviewKeyDown key = Return
KeyDown key = Return
CellLeave (0, 0)
RowLeave (0, 0)
CellValidating (0, 0)
CellValidated (0, 0)
RowEnter (1, 0)
CurrentCellChanged
CellEnter (1, 0)
SelectionChanged
KeyUp key = Return

Press Enter (while editing the field):

CellClick (0, 0)
CellBeginEdit (0, 0)
EditingControlShowing
    (I press Enter now)
CellLeave (0, 0)
RowLeave (0, 0)
CellValidating (0, 0)
CellValidated (0, 0)
CellEndEdit (0, 0)
RowEnter (1, 0)
CurrentCellChanged
CellEnter (1, 0)
SelectionChanged
KeyUp key = Return

i.e. I never get a PreviewKeyDown or KeyDown event when I press Enter while editing a field.

I tried hacking some alternate navigation in the event handlers. For test purposes, I always edit (r0, c0), and "navigate" by setting the CurrentCell property explicity to (r0, c1).

When I set the CurrentCell in CellEndEdit, I end up at (r1, c1). When I set the CurrentCell in KeyUp, I end up at (r0, c1), which is good, but only after it visibly selects (r1, c0). I suppose I could disable updates on the control during navigation, but this solution seems inelegant.

Any advice? Am I missing something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧伤还要旧人安 2024-08-10 18:21:57

经过随机胡乱仔细分析,看起来关键是从DataGridView派生并重写DataGridView.ProcessDialogKey()和ProcessDataGridViewKey()

After random flailing about careful analysis, it looks like the key is to derive from DataGridView and override DataGridView.ProcessDialogKey () and ProcessDataGridViewKey ()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文