C# 将击键发送到 DataGridViewCell

发布于 2024-09-17 12:23:05 字数 647 浏览 8 评论 0原文

我的 DataGridView 中有一个 DateTimePicker 单元格。我希望能够进入编辑模式并在单击按钮时删除日历。我可以毫无困难地完成第一部分,但第二部分不起作用。如果我有一个独立的 DateTimePicker,则 SendKeys 调用确实会按预期工作。

//Select the cell and enter edit mode -  works
myDGV.CurrentCell = myDGV[calColumn.Index, e.RowIndex];
myDGV.BeginEdit(true);

//Send an ALt-Down keystroke to drop the calendar  - doesn't work
SendKeys.SendWait("%{DOWN}");

通过调试,我认为问题在于击键被发送到 DGV,而不是我要编辑的特定单元格。我认为的原因是我已将代码放入日志由网格 KeyPress 和 KeyDown 事件接收的按键中。它们记录我在网格周围的箭头和 SendKeys 发送的按键,但不记录我通过在其中键入来编辑单元格时的按键。

I have a DateTimePicker cell in my DataGridView. I'd like to be able to enter edit mode and drop the calendar when a button is clicked. I'm able to do the first part without difficulty but the second isn't working. If I have a standalone DateTimePicker the SendKeys call does work as expected.

//Select the cell and enter edit mode -  works
myDGV.CurrentCell = myDGV[calColumn.Index, e.RowIndex];
myDGV.BeginEdit(true);

//Send an ALt-Down keystroke to drop the calendar  - doesn't work
SendKeys.SendWait("%{DOWN}");

From debugging I believe that the problem is that the keystroke is being sent to the DGV and not the specific cell that I'm trying to edit. The reason I think is is that I've put code to log keys received by the grids KeyPress and KeyDown events. They log my arrowing around the grid and the keys sent by SendKeys, but not those from when I'm editing a cell by typing in it.

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

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

发布评论

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

评论(2

情释 2024-09-24 12:23:05

请参阅我在C# Winforms DataGridView时间列上的答案。我相信它会完美地满足您的需求。您还可以将其用于具有组合框的列。

Please see my answer on C# Winforms DataGridView Time Column. I believe it will fit your needs perfectly. You can also use it for a column that has a ComboBox.

清引 2024-09-24 12:23:05

我最近重新审视了这个问题,因为 0A0D 提供的实现并不总是能很好地与网格的键盘导航(箭头/选项卡)配合使用。有时可以绕过 DateTimePicker 并将文本输入到 DataGridViewTextBoxCell 中。这导致我的验证逻辑崩溃了;在未能找到防止滑动发生的方法之后,我决定尝试让自定义列再次工作。

事实证明修复非常简单。我创建了一个扩展的 DateTimePicker,其中包含一种发送显示日历所需的击键的方法。

/// <summary>
/// Extended DateTimePicker with a method to programmatically display the calendar.
/// </summary>
class DateTimePickerEx : DateTimePicker
{
    [DllImport("user32.dll")]
    private static extern bool PostMessage(
    IntPtr hWnd, // handle to destination window
    Int32 msg, // message
    Int32 wParam, // first message parameter
    Int32 lParam // second message parameter
    );

    const Int32 WM_LBUTTONDOWN = 0x0201;

    /// <summary>
    /// Displays the calendar input control.
    /// </summary>
    public void ShowCalendar()
    {
        Int32 x = Width - 10;
        Int32 y = Height / 2;
        Int32 lParam = x + y * 0x00010000;

        PostMessage(Handle, WM_LBUTTONDOWN, 1, lParam);
    }
}

然后,我修改了 MSDN DateTime 列示例 以具有 CalendarEditingControl< /code> 继承自 DateTimePickerEx

然后,在托管 DataGridView 的表单中,我使用 EditingControl 属性调用 ShowCalendar() 方法。

DateTimePickerEx dtp = myDataGridView.EditingControl as DateTimePickerEx;
if (dtp != null)
    dtp.ShowCalendar();

I recently revisited this issue because the implementation provided by 0A0D didn't always play nicely with keyboard navigation of the grid (arrows/tab). At times it was possible to bypass the DateTimePicker and enter text into the DataGridViewTextBoxCell. This caused my validation logic to freak out; and after failing to find a way to prevent the slip around from happening I decided to try and get the custom column working again.

The fix turned out to be very simple. I created an extended DateTimePicker with a method to send the keystroke needed to display the calendar.

/// <summary>
/// Extended DateTimePicker with a method to programmatically display the calendar.
/// </summary>
class DateTimePickerEx : DateTimePicker
{
    [DllImport("user32.dll")]
    private static extern bool PostMessage(
    IntPtr hWnd, // handle to destination window
    Int32 msg, // message
    Int32 wParam, // first message parameter
    Int32 lParam // second message parameter
    );

    const Int32 WM_LBUTTONDOWN = 0x0201;

    /// <summary>
    /// Displays the calendar input control.
    /// </summary>
    public void ShowCalendar()
    {
        Int32 x = Width - 10;
        Int32 y = Height / 2;
        Int32 lParam = x + y * 0x00010000;

        PostMessage(Handle, WM_LBUTTONDOWN, 1, lParam);
    }
}

I then modified the MSDN DateTime column example to have CalendarEditingControl inherit from DateTimePickerEx.

Then in the form hosting the DataGridView I used the EditingControl property to call the ShowCalendar() method.

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