简体   繁体   English

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

[英]Impossible Task On Custom DGV Set Focus To Next Control On Enter Key Press

This is the real challenge I brings here for you. 这是我为您带来的真正挑战。 Solve it if you can. 如果可以解决。

The Enter Key On Datagridview Is it good Idea? Datagridview上的Enter键是个好主意吗? 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. 如果不是,那么在按Enter键时Datagridview Control无效。

I have Following Custom DGV: 我有以下自定义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. 在正常情况下,该控件工作正常,它可以将焦点放在输入按键上的下一个控件上,但是如果在数据绑定模式下设置AllowUserToAddRows = false ,则该控件不起作用。 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. 当您编辑最后一列的单元格值并按Enter键时,将出现确切的问题,不允许将焦点放在下一个控件上。

How to overcome from this?. 如何克服这个? how to solve the issue? 该如何解决呢?

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 . 它适用于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
        }
    }

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. 用户将转到DataGridView中的最后一个控件,并且没有要返回的GetNextControl控件。

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. 例如,您可以将焦点设置为向导形式的“提交”或“下一步”按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM