DataGridView TextBox单元格编辑,将Enter键更改为类似于Tab [英] DataGridView TextBox cell Editing, Change Enter Key to Act Like Tab

查看:219
本文介绍了DataGridView TextBox单元格编辑,将Enter键更改为类似于Tab的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在重新询问这个问题,因为我现在有了一些与我的问题相关的代码(我删除了旧的Question)。

I am re-asking this question as I now have some code to go with my issue (I deleted the old Question).

基本上,当输入键为在编辑文本框单元格时按下该按钮,我希望它的作用就像按Tab键(当前行的下一列而不是同一行的下一行)。

Basically when the enter key is pressed while editing a textbox cell, I would like it to act like a Tab press (Next Column in current Row Instead of Next row same column).

我的问题是到目前为止,我尝试过的所有方法都无效,但这是我当前尝试的解决方案。

My problem is most of what I have tried so far doesn't work, This was my current attempted solution however.

此代码应该更改正在编辑/选择的单元格。

This code is supposed to change which cell is being edited/Selected.

    private void PreTranslateDGV_KeyPressEvent(object sender, KeyEventArgs e)
    {
        DataGridViewTextBoxEditingControl a = (DataGridViewTextBoxEditingControl) sender;
        //a.PreviewKeyDown -= PreviewKeyDownEventHandler (dataGridView1_PreviewKeyDown)
        MyDataGridView s = (MyDataGridView) a.EditingControlDataGridView;
        if (e.KeyCode == Keys.Enter)
        {
            e.SuppressKeyPress = true;
            int newRow;
            int newColumn;
            if (s.CurrentCell.ColumnIndex == s.ColumnCount - 1)         // it's a last column, move to next row;
            {
                newRow = s.CurrentCell.RowIndex + 1;
                newColumn = 0;

                if (newRow == s.RowCount)
                    return; // ADD new row or RETURN (depends of your purposes..)
            }
            else                // just change current column. row is same
            {
                newRow = s.CurrentCell.RowIndex;
                newColumn = s.CurrentCell.ColumnIndex + 1;
            }

            s.CurrentCell = s.Rows[newRow].Cells[newColumn];
        }
    }

这是将上述事件添加到单元格的文本框

This is the code that Adds the Above event to the Cell's textbox

private void PreTranslateDGV_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        DataGridViewTextBoxEditingControl tb = (DataGridViewTextBoxEditingControl)e.Control;
        tb.KeyDown += new KeyEventHandler (PreTranslateDGV_KeyPressEvent);
    }

其中大部分是我从StackOverflow找到的代码,因为我一直在尝试获取

Most of this was code I found from StackOverflow as I have been trying to get it working for a while.

如果有人知道如何从datagridview中正确获取 Enter键,那么在编辑单元格时请帮忙。

If anyone Knows how to properly Get the "Enter" Keypress from within a datagridview, While editing a cell please help.

PS:我在MSDN论坛上阅读(丢失链接),当编辑文本框单元格时,按Enter时它将停止编辑。这就可以解释为什么我上面的代码不会在Enter时触发,而是在其他所有条件下触发。

PS: I Read on an MSDN forum (lost link) That when Editing a textbox cell, When you press enter it Stops editing. Which would explain why my code above doesn't fire On Enter, but it fires on everything else.

我现在试图通过重写processcmdkey

I am now attempting to do this by over-riding the processcmdkey

    class MyDataGridView : KryptonDataGridView
{
    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if ((keyData == Keys.Enter) && (this.EditingControl != null))
        {
            return false;
        }
        //for the rest of the keys, proceed as normal
        return base.ProcessCmdKey(ref msg, keyData);
    }
}

但是无论我似乎要返回什么,输入密钥没有传递给KeyPressEvent。

But no matter what I seem to Return, the Enter key isn't passed to the KeyPressEvent.

推荐答案

麻烦很多。这里是我当前正在使用的解决方案:

After much, annoying trouble. Here the solution I am currently using:

 private void PreTranslateDGV_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
    {
        DataGridViewTextBoxEditingControl a = (DataGridViewTextBoxEditingControl) sender;
        //a.PreviewKeyDown -= PreviewKeyDownEventHandler (dataGridView1_PreviewKeyDown)
        MyDataGridView s = (MyDataGridView) a.EditingControlDataGridView;
        if (e.KeyCode == Keys.Enter)
        {
            int newRow;
            int newColumn;
            if (s.CurrentCell.ColumnIndex == s.ColumnCount - 1)         // it's a last column, move to next row;
            {
                newRow = s.CurrentCell.RowIndex + 1;
                newColumn = 0;

                if (newRow == s.RowCount)
                    s.Rows.Add(1); // ADD new row or RETURN (depends of your purposes..)
            }
            else                // just change current column. row is same
            {
                newRow = s.CurrentCell.RowIndex;
                newColumn = s.CurrentCell.ColumnIndex + 1;
            }
            s.CurrentCell = s.Rows[newRow].Cells[newColumn];
        }
    }
    private void PreTranslateDGV_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        DataGridViewTextBoxEditingControl tb = (DataGridViewTextBoxEditingControl)e.Control;
        tb.PreviewKeyDown -= PreTranslateDGV_PreviewKeyDown;
        tb.PreviewKeyDown += PreTranslateDGV_PreviewKeyDown;

        //e.Control.KeyDown += new KeyEventHandler(PreTranslateDGV_KeyPressEvent);
    }

我将KeyPressEvent更改为PreviewKeyDown事件。在ProcessCmdKey获取输入之前触发。使用此代码和修改后的datagridview,我可以在单元格内部获取Enter键以充当Tab角色。

I changed the KeyPressEvent, to a PreviewKeyDown Event. Which fires before ProcessCmdKey Gets input. Using this and my modified datagridview I was able to Get the Enter key to Act like Tab, while Inside a cell.

   class MyDataGridView : KryptonDataGridView
{
    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if ((keyData == Keys.Enter) && (this.EditingControl != null))
        {
            return true;
        }
        //for the rest of the keys, proceed as normal
        return base.ProcessCmdKey(ref msg, keyData);
    }
}

当给ProcessCmdKey输入Enter键时,它立即返回 true表示已处理。 它通过PreviewkeyDown事件

When ProcessCmdKey is given the Enter Key, It Immediately returns "true" to state that it has been handled. which it has by the PreviewkeyDown event

希望这对其他人有帮助。我不确定还有其他方法可以使用,但是这种方法对我有用。

Hopefully this helps other people. I'm not sure how many other ways there are to do this, But this method worked for me.

这篇关于DataGridView TextBox单元格编辑,将Enter键更改为类似于Tab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆