Moving to next control in tab order

rahul kumar 605 Reputation points
2023-07-09T12:58:48.43+00:00

I am trying to move out to the next control in taborder when the user presses enter and there is nothing in cell of column 1 of datagridview butthe focus is not shifting . the next control in taborder is textbox . i had double checked tabindex and taborder window but nothing is working . please suggest .

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace datagridviewusingchatgpt.Supercontrols
{
    public partial class betterdatagridview : DataGridView
    {
        public betterdatagridview()
        {
            InitializeComponent();
           
        }
              

        protected override bool ProcessDialogKey(Keys keyData)
        {
          int  currentrowposition = CurrentCell.RowIndex; // it must stay here
           int currentcolumnposition = CurrentCell.ColumnIndex; // it must stay here 
            if (keyData == Keys.Enter)
            {
                this.CommitEdit(DataGridViewDataErrorContexts.Commit);
                this.EndEdit();
                if(currentcolumnposition ==0 && string.IsNullOrEmpty(this.CurrentCell.Value?.ToString()))
                {
                    this.SelectNextControl(this, true, true, true, true);
                    return true;
                }
                if(currentcolumnposition ==this.ColumnCount-1)
                {
                    if(currentrowposition==this.RowCount-1)
                    {
                        this.Rows.Add();
                        this.CurrentCell = this[0, currentrowposition + 1];
                    }
                    else
                    {
                        this.CurrentCell = this[0, currentrowposition + 1];
                    }
                }
                else
                {
                    this.CurrentCell = this[currentcolumnposition + 1, currentrowposition];

                }
                this.BeginEdit(true);
                return true;
            }
            return base.ProcessDialogKey(keyData);
        }

        protected override void OnEnter(EventArgs e)
        {
            base.OnEnter(e);
            this.CurrentCell = this.Rows[0].Cells[0];
            this.BeginEdit(true);
        }
    }
}


User's image

Developer technologies | Windows Forms
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2023-07-10T07:34:35.6+00:00

    Hi @rahul kumar , Welcome to Microsoft Q&A.

    SelectNextControl doesn't work very well here, You can use sendtab directly instead. In your scenario, the code below works directly for you.

    I found that you need to keep the first line, you can choose to use tabindex directly to modify.

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if (CurrentCell != null)
        {
            int currentrowposition = CurrentCell.RowIndex; // it must stay here
            int currentcolumnposition = CurrentCell.ColumnIndex; // it must stay here 
            if (keyData == Keys.Enter)
            {
                if (currentcolumnposition == 0 && string.IsNullOrEmpty(this.CurrentCell.Value?.ToString()))
                {
                    Control nextControl = GetNextControlWithTabIndex(this.Parent, TabIndex + 1);
                    if (nextControl != null)
                    {
                        nextControl.Select();
                    }
                    return true;
                }
                return true;
            }
        }
        return base.ProcessCmdKey(ref msg, keyData);
    }
    private Control GetNextControlWithTabIndex(Control container, int tabIndex)
    {
        foreach (Control control in container.Controls)
        {
            if (control.TabStop && control.TabIndex == tabIndex)
            {
                return control;
            }
        }
        return null;
    }
    

    SelectNextControl doesn't work very well here, You can use sendtab directly instead. In your scenario, the code below works directly for you.

    protected override void OnLeave(EventArgs e)
    {
        base.OnLeave(e);
        this.AllowUserToAddRows = true;
    }
    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if (CurrentCell != null)
        {
            int currentrowposition = CurrentCell.RowIndex; // it must stay here
            int currentcolumnposition = CurrentCell.ColumnIndex; // it must stay here 
            if (keyData == Keys.Enter)
            {
                if (currentcolumnposition == 0 && string.IsNullOrEmpty(this.CurrentCell.Value?.ToString()))
                {
                    this.AllowUserToAddRows = false;
                    SendKeys.Send("{Tab}");
                    return true;
                }
                return true;
            }
        }
        return base.ProcessCmdKey(ref msg, keyData);
    }
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.