Need to press ctrl + enter key twice

rahul kumar 605 Reputation points
2023-11-10T10:50:55.5133333+00:00

Below is the code snippet of my custom datagridview's processcmdkey event . It has a logical problem due to which for executing the block which activate when the user presses ctrl + enter activates only when i hit the key combination twice

     protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Enter || keyData == Keys.Right)
            {
                var current = CurrentCell;
                if (current != null)
                {
                    if (IsCurrentCellDirty) // fires if the cell state is changed by typing 
                    {
                        CommitEdit(DataGridViewDataErrorContexts.Commit);
                        EndEdit(DataGridViewDataErrorContexts.Commit);
                    }
                    if (IsCurrentCellInEditMode)
                    {
                        EndEdit(DataGridViewDataErrorContexts.Commit);
                    }
                    int row = current.RowIndex;
                    int col = current.ColumnIndex;
                    if (row == LastRowIndex)
                    {
                        if (col == FirstVisibleColumn.Index)
                        {
                            if (Rows[row].IsNewRow || string.IsNullOrEmpty(CurrentCell.Value?.ToString()) || CurrentCell.Value.ToString() == "End of list")
                            {
                                return GotoNextControl();
                            }
                        }
                    }
                    if (col == LastVisibleColumn.Index) // if reached last cell of current row 
                    {
                        if (TryAddRow())
                        {
                            CurrentCell = this[FirstVisibleColumn.Index, RowCount - 1];
                            return true;
                        }

                    }
                }
                return ProcessTabKey(Keys.Tab);
            }
            const int WM_KEYDOWN = 0x0100;
            const int WM_SYSKEYDOWN = 0x0104;
            int rowIndex = this.CurrentCell.RowIndex;
            int columnIndex = this.CurrentCell.ColumnIndex;
            // add new item mode 
            if (columnIndex == 0 && (msg.Msg == WM_KEYDOWN || msg.Msg == WM_SYSKEYDOWN))
            {
                if (keyData == (Keys.Alt | Keys.C))
                {
                    ItemEdit edit = new ItemEdit();
                    Point PositionX = this.Parent.PointToScreen(Point.Empty);
                    int LeftScreenX = PositionX.X;
                    Point PositionY = this.Parent.PointToScreen(Point.Empty);
                    int TopScreenY = PositionY.Y;
                    edit.StartPosition = FormStartPosition.Manual;
                    edit.Location = new Point(LeftScreenX, TopScreenY);
                    edit.ShowInTaskbar = false;
                    edit.FormBorderStyle = FormBorderStyle.None;
                    edit.label1.Text += "Creation";
                    if (edit.ShowDialog() == DialogResult.Yes)
                    {
                        this.Rows[rowIndex].Cells[columnIndex].Value = edit.textbox1data;
                        this.EndEdit();
                    }
                    return true; // return true means the shortcut key has been processed
                }
            }
                ////edit the current item mode 


                if (columnIndex == 0 && !string.IsNullOrEmpty(CurrentCell.Value?.ToString()) && keyData == (Keys.Control | Keys.Enter))
                {
                    if (keyData == (Keys.Control | Keys.Enter))
                    {
                        ItemEdit edit = new ItemEdit();
                        Point PositionX = this.Parent.PointToScreen(Point.Empty);
                        int LeftScreenX = PositionX.X;
                        Point PositionY = this.Parent.PointToScreen(Point.Empty);
                        int TopScreenY = PositionY.Y;
                        edit.StartPosition = FormStartPosition.Manual;
                        edit.Location = new Point(LeftScreenX, TopScreenY);
                        edit.ShowInTaskbar = false;
                        edit.label1.Text += "Alteration";
                        edit.textbox1data = CurrentCell.Value?.ToString();
                        if (edit.ShowDialog() == DialogResult.Yes)
                        {
                            this.Rows[rowIndex].Cells[columnIndex].Value = edit.textbox1data;
                            this.EndEdit();
                        }
                        return true;
                    }
                }            
            return base.ProcessCmdKey(ref msg, keyData); 
        }


I tried to debug the code but it shows the same result . The breakpoint hit only after i press the combination twice . I had double checked code several times but no success . Am i missing something

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,884 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,819 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiale Xue - MSFT 44,231 Reputation points Microsoft Vendor
    2023-11-10T12:38:58.2366667+00:00

    Hi @Rahul Kumar , Welcome to Microsoft Q&A,

    Updated:

    the problem has been solved . it was due to dirty cell that the value was null everytime .


    Try to use the code bellow.

    if (columnIndex == 0 && !string.IsNullOrEmpty(CurrentCell.Value?.ToString()) && keyData == (Keys.Control | Keys.Enter))
    {
        ItemEdit edit = new ItemEdit();
        Point PositionX = this.Parent.PointToScreen(Point.Empty);
        int LeftScreenX = PositionX.X;
        Point PositionY = this.Parent.PointToScreen(Point.Empty);
        int TopScreenY = PositionY.Y;
        edit.StartPosition = FormStartPosition.Manual;
        edit.Location = new Point(LeftScreenX, TopScreenY);
        edit.ShowInTaskbar = false;
        edit.label1.Text += "Alteration";
        edit.textbox1data = CurrentCell.Value?.ToString();
        if (edit.ShowDialog() == DialogResult.Yes)
        {
            this.Rows[rowIndex].Cells[columnIndex].Value = edit.textbox1data;
            this.EndEdit();
        }
        return true;
    }
    
    

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