PreviewKeyDown事件被触发两次

时间:2014-06-12 03:30:45

标签: c# winforms events datagridview

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

在我的表单中,我希望光标按顺序聚焦在单元格列索引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);
    }
}

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

谢谢你,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];
    }