【问题标题】:PreviewKeyDown event fired twice timePreviewKeyDown 事件触发了两次
【发布时间】:2014-08-02 06:48:35
【问题描述】:

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

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

【问题讨论】:

  • 这是 wpf 吗?如果是这样,您需要将事件标记为已处理。
  • 是的,它是窗体应用程序。需要什么?我只是一个初学者。你能再解释一下吗。感谢您的帮助。
  • 这是winform还是WPF?
  • 这是一个winform。不过谢谢。我现在解决了。

标签: c# winforms events datagridview


【解决方案1】:

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

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

【讨论】:

  • 您怎么知道您的代码执行了两次该事件?我的意思是您是否使用 F11/F10 键调试您的代码?在该事件Console.WriteLine(DateTime.Now.ToString()); 中放置一行并签入输出窗口。当您按任意键时,您的代码是否会打印两次日期时间?并且不要按任何其他键,例如 alt+tab 或 ctrl+tab 来切换窗口。请改用鼠标。
  • 是的,我使用调试和 F10。
【解决方案2】:

谢谢你,尼梅什。
我也不知道为什么。 但是我是这样解决的……

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

【讨论】:

  • e.Handled = true;是关键部分
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-07
相关资源
最近更新 更多