PreviewKeyDown事件触发两次

本文关键字:两次 事件 PreviewKeyDown | 更新日期: 2023-09-27 18:08:10

我期望的是当我完成编辑单元格并单击enter时,光标将聚焦到指定的单元格。

在我的表单中,我希望光标依次集中在单元格列索引5,2,3上。之后下一行列的索引将是5。

但是PreviewKeyDown事件id被处理了两次。

所以我传递了我想要的第二步,结果最后得到了一个错误。

这是我目前为止尝试过的实现:

void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    TextBox txtCell = e.Control as TextBox;
    if (txtCell != null) 
    {
        txtCell.PreviewKeyDown += new PreviewKeyDownEventHandler(txtCell_PreviewKeyDown);
        txtCell.PreviewKeyDown += new PreviewKeyDownEventHandler(txtCell_PreviewKeyDown);
        txtCell.KeyDown += new KeyEventHandler(txtCell_KeyDown);
        txtCell.KeyDown += new KeyEventHandler(txtCell_KeyDown);
    }
}
void txtCell_KeyDown(object sender, KeyEventArgs e)
{
    try
    {
        TextBox tCell = (TextBox)sender;
        if (dataGridView1.CurrentCell.ColumnIndex == 5)
        {
            if (e.KeyCode == Keys.Return)
            {
                e.Handled = true;
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
void txtCell_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
    try
    {
        if (e.KeyCode == Keys.Return)
        {
            int iColumn = dataGridView1.CurrentCell.ColumnIndex;
            int iRow = dataGridView1.CurrentCell.RowIndex;
            if (iColumn == 5)
            {
                dataGridView1.Focus();
                dataGridView1.CurrentCell = dataGridView1[2, iRow];
                //-----I want to test the focus across the cell or not.
                dataGridView1.CurrentCell.Value = "123";
            }
            if (iColumn == 2)
            {
                dataGridView1.Focus();
                dataGridView1.CurrentCell = dataGridView1[3, iRow];
                //-----I want to test the focus across the cell or not.
                dataGridView1.CurrentCell.Value = "123";
            }
            if (iColumn == 3)
            {
                dataGridView1.Focus();
                dataGridView1.CurrentCell = dataGridView1[5, iRow + 1];
                //-----I want to test the focus across the cell or not.
                dataGridView1.CurrentCell.Value = "123";
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

PreviewKeyDown事件触发两次

谢谢你,Nimesh。
我也不知道为什么。但我是这样解决的......

public Form3()
    {
        InitializeComponent();
        dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);
    }
    bool flag = false;
    void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        TextBox txtCell = e.Control as TextBox;
        if (txtCell != null) 
        {
            txtCell.PreviewKeyDown -= new PreviewKeyDownEventHandler(txtCell_PreviewKeyDown);
            txtCell.PreviewKeyDown += new PreviewKeyDownEventHandler(txtCell_PreviewKeyDown);
            txtCell.KeyDown -= new KeyEventHandler(txtCell_KeyDown);
            txtCell.KeyDown += new KeyEventHandler(txtCell_KeyDown);
        }
    }
    void txtCell_KeyDown(object sender, KeyEventArgs e)
    {
        try
        {
            TextBox tCell = (TextBox)sender;
            if (dataGridView1.CurrentCell.ColumnIndex == 5)
            {
                if (e.KeyCode == Keys.Return)
                {
                    e.Handled = true;
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
    void txtCell_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
    {
        TextBox tCell = (TextBox)sender;
        if (!flag)
        {
            flag = true;
            if (e.KeyCode == Keys.Return)
            {
                //e.SuppressKeyPress = true;
                int iColumn = dataGridView1.CurrentCell.ColumnIndex;
                int iRow = dataGridView1.CurrentCell.RowIndex;
                if (iColumn == 5)
                {
                    dataGridView1.Focus();
                    dataGridView1.CurrentCell = dataGridView1[2, iRow];
                    dataGridView1.CurrentCell.Value = "123";
                    iColumn = 0;
                    iRow = 0;
                    flag = false;
                    return;
                }
                if (iColumn == 2)
                {
                    dataGridView1.Focus();
                    dataGridView1.CurrentCell = dataGridView1[3, iRow];
                    dataGridView1.CurrentCell.Value = "123";
                    iColumn = 0;
                    iRow = 0;
                    flag = false;
                    return;
                }
                if (iColumn == 3)
                {
                    dataGridView1.Focus();
                    dataGridView1.CurrentCell = dataGridView1[5, iRow + 1];
                    dataGridView1.CurrentCell.Value = "123";
                    iColumn = 0;
                    iRow = 0;
                    flag = false;
                    return;
                }
                flag = false;
                return;
            }
            flag = false;
            return;
        }
        flag = false;
        return;
    }
    private void Form3_Load(object sender, EventArgs e)
    {
        dataGridView1.Focus();
        dataGridView1.CurrentCell = dataGridView1.Rows[0].Cells[5];
    }

更改以下代码。您创建了两次处理程序,您应该在添加新处理程序之前删除处理程序。如果有的话,它将删除已经创建的处理程序。

void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    TextBox txtCell = e.Control as TextBox;
    if (txtCell != null) 
    {
        txtCell.PreviewKeyDown -= new PreviewKeyDownEventHandler(txtCell_PreviewKeyDown);
        txtCell.PreviewKeyDown += new PreviewKeyDownEventHandler(txtCell_PreviewKeyDown);
        txtCell.KeyDown -= new KeyEventHandler(txtCell_KeyDown);
        txtCell.KeyDown += new KeyEventHandler(txtCell_KeyDown);
    }
}