按键事件

发布于 2025-01-07 04:18:37 字数 107 浏览 0 评论 0原文

我使用代码 datagrid1.controls.add(frmnew) 将表单添加到 datagridview。问题是,该表单中的控件的按键事件未触发。请给我一个解决方案。

I added a form to a datagridview using the code datagrid1.controls.add(frmnew). The issue is that, key press event of the controls in that form is not firing. Please give me a solution for this.

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

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

发布评论

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

评论(2

嗳卜坏 2025-01-14 04:18:37

如果您使用的是 Windows 窗体:

检查您的 .Designer.cs 文件是否有事件处理程序。

事件处理程序如下所示:

this.datagrid1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.datagrid1_KeyPress);

If you are using windows forms:

Check in your .Designer.cs file that you have an event handler.

An event handler looks like this:

this.datagrid1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.datagrid1_KeyPress);
你的往事 2025-01-14 04:18:37

如果要捕获 datagridview 控件内的按键事件,则必须连接事件 EditingControlShowing。下面是捕获按键事件并仅允许将数字数据输入到 datagridview 单元格的示例。

    /// <summary>
    /// Occurs when a control for editing a cell is showing
    /// </summary>
    /// <remarks>Capture key press to handle key entry in datagridview</remarks>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void dgDCAL_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        dgDCAL.EditingControl.KeyPress -= EditingControl_KeyPress;
        dgDCAL.EditingControl.KeyPress += EditingControl_KeyPress;
    }

    /// <summary>
    /// Handle datagridview cell keypress event
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void EditingControl_KeyPress(object sender, KeyPressEventArgs e)
    {
        // Only allow 0-9, backspace, period and return key
        if (!Char.IsNumber(e.KeyChar) && 
            (int)e.KeyChar != 8 &&
            (int)e.KeyChar != 46 &&
            (int)e.KeyChar != 13) e.Handled = true;
    }

If you want to capture that keypress event inside of the datagridview control, then you must wire up the event EditingControlShowing. Here is an example of capturing the keypress event and only allowing numeric data to be entered into the datagridview cell.

    /// <summary>
    /// Occurs when a control for editing a cell is showing
    /// </summary>
    /// <remarks>Capture key press to handle key entry in datagridview</remarks>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void dgDCAL_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        dgDCAL.EditingControl.KeyPress -= EditingControl_KeyPress;
        dgDCAL.EditingControl.KeyPress += EditingControl_KeyPress;
    }

    /// <summary>
    /// Handle datagridview cell keypress event
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void EditingControl_KeyPress(object sender, KeyPressEventArgs e)
    {
        // Only allow 0-9, backspace, period and return key
        if (!Char.IsNumber(e.KeyChar) && 
            (int)e.KeyChar != 8 &&
            (int)e.KeyChar != 46 &&
            (int)e.KeyChar != 13) e.Handled = true;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文