自定义 DGV 上不可能完成的任务 按 Enter 键将焦点设置为下一个控件

发布于 2024-11-28 00:15:45 字数 2484 浏览 0 评论 0原文

这是我为您带来的真正挑战。如果可以的话解决一下。

Datagridview 上的 Enter 键是个好主意吗?或者这里有任何机构可以按照以下条件进行操作吗?

如果不是,则 Datagridview 控件在按 Enter 键时毫无用处。

我有以下自定义 DGV:

public class MyDataGridView : DataGridView
    {
        protected override bool ProcessDialogKey(Keys keyData)
        {
            Keys key = (keyData & Keys.KeyCode);
            if (key == Keys.Enter)
            {
                Tab(keyData);
                return true;
            }
            return base.ProcessDialogKey(keyData);
        }
        protected override bool ProcessDataGridViewKey(KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Tab(e.KeyData);
                return true;
            }
            return base.ProcessDataGridViewKey(e);
        }
        private void Tab(Keys KeyData)
        {
            Point cca = CurrentCellAddress;
            bool Forward = ((KeyData & Keys.Shift) != Keys.Shift);
            if (Forward)
                if (cca.Y == Rows.Count - 1)            // last row?
                    if (cca.X == Columns.Count - 1)     // last column?
                        ToNextControl(Forward);
                    else
                        ProcessTabKey(KeyData);
                else
                    ProcessTabKey(KeyData);
            else
                if (cca.Y == 0)         // first row?
                    if (cca.X == 0)     // first column?
                        ToNextControl(Forward);
                    else
                        ProcessTabKey(KeyData);
                else
                    ProcessTabKey(KeyData);
        }

        /// <summary>
        /// Go to the next control, forward or backword. This does not support
        /// wrapping from the first to the last or the last to the first.
        /// </summary>
        /// <param name="Forward">Whether to go forward</param>
        private void ToNextControl(bool Forward)
        {
            Control c = Parent.GetNextControl(this, Forward);
            while (c != null && !c.TabStop) // get next control that is a tabstop
                c = Parent.GetNextControl(c, Forward);
            if (c != null)
                c.Select();
        }
    }

在正常情况下同样可以很好地工作,它允许在输入按键时专注于下一个控件,但如果您在数据绑定模式下设置 AllowUserToAddRows=false ,则它不起作用。当您编辑最后一列的单元格值并按 Enter 键时,会产生确切的问题,它不允许将焦点设置在下一个控件上。

如何克服这个问题?如何解决问题?

This is the real challenge I brings here for you. Solve it if you can.

The Enter Key On Datagridview Is it good Idea? Or Is it possible for any body here to work it as per following condition?.

If not than Datagridview Control is useless on enter key press.

I have Following Custom DGV:

public class MyDataGridView : DataGridView
    {
        protected override bool ProcessDialogKey(Keys keyData)
        {
            Keys key = (keyData & Keys.KeyCode);
            if (key == Keys.Enter)
            {
                Tab(keyData);
                return true;
            }
            return base.ProcessDialogKey(keyData);
        }
        protected override bool ProcessDataGridViewKey(KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Tab(e.KeyData);
                return true;
            }
            return base.ProcessDataGridViewKey(e);
        }
        private void Tab(Keys KeyData)
        {
            Point cca = CurrentCellAddress;
            bool Forward = ((KeyData & Keys.Shift) != Keys.Shift);
            if (Forward)
                if (cca.Y == Rows.Count - 1)            // last row?
                    if (cca.X == Columns.Count - 1)     // last column?
                        ToNextControl(Forward);
                    else
                        ProcessTabKey(KeyData);
                else
                    ProcessTabKey(KeyData);
            else
                if (cca.Y == 0)         // first row?
                    if (cca.X == 0)     // first column?
                        ToNextControl(Forward);
                    else
                        ProcessTabKey(KeyData);
                else
                    ProcessTabKey(KeyData);
        }

        /// <summary>
        /// Go to the next control, forward or backword. This does not support
        /// wrapping from the first to the last or the last to the first.
        /// </summary>
        /// <param name="Forward">Whether to go forward</param>
        private void ToNextControl(bool Forward)
        {
            Control c = Parent.GetNextControl(this, Forward);
            while (c != null && !c.TabStop) // get next control that is a tabstop
                c = Parent.GetNextControl(c, Forward);
            if (c != null)
                c.Select();
        }
    }

The Same work well on normal it's allow focus on next control at enter keypress but it's not work if you set AllowUserToAddRows=false on databound mode. The exact problem creates when you edit last column's cell value and press enter it's not allow to set focus on next control.

How to overcome from this?. how to solve the issue?

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

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

发布评论

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

评论(2

夏末染殇 2024-12-05 00:15:45

我找到了一个适合您的解决方案,通过在当前控件上添加焦点事件。我认为这就是你想要的,但无法真正理解你的问题。让我知道是否有不同。它适用于 AllowUserToAddRows = falsetrue

 private void ToNextControl(bool Forward)
    {
        Control c = Parent.GetNextControl(this, Forward);         
        while (c != null && !c.TabStop) // get next control that is a tabstop
            c = Parent.GetNextControl(c, Forward);
        if (c != null)
        {
            //c.Select(); // Not needed for it to work
            c.Focus(); // force it to focus
        }
    }

I found a solution that should work for you by adding a focus event on the current control. I think this is what you want, but couldn't really understand your question. Let me know if it is something different. It works for AllowUserToAddRows = false or true.

 private void ToNextControl(bool Forward)
    {
        Control c = Parent.GetNextControl(this, Forward);         
        while (c != null && !c.TabStop) // get next control that is a tabstop
            c = Parent.GetNextControl(c, Forward);
        if (c != null)
        {
            //c.Select(); // Not needed for it to work
            c.Focus(); // force it to focus
        }
    }
只有一腔孤勇 2024-12-05 00:15:45

我只查看代码而不运行它所看到的问题是:

用户到达 DataGridView 中的最后一个控件,并且没有 GetNextControl 返回的控件。

您可能需要添加一些逻辑来处理这种情况。例如,如果是向导表单,您可以将焦点设置在“提交”或“下一步”按钮上。

The problem I'm seeing just looking at the code without running it is this:

The user gets to the last control in the DataGridView and there is no control for GetNextControl to return.

You may want to add is some logic to handle that case. For example, you could set focus on a submit or next button if this is in a wizard form.

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