开发者

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

开发者 https://www.devze.com 2023-03-26 13:27 出处:网络
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?.

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开发者_Go百科 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?


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
        }
    }


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号