将事件附加到DataGridView单元基础的TextBox [英] Attaching events to an TextBox underlying for a DataGridView cell

查看:71
本文介绍了将事件附加到DataGridView单元基础的TextBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以获取DataGridView单元的基础控件吗?
我想附加普通的texbox事件以捕获击键并捕获更改的值。

Is there any way to get underlying control for a DataGridView cell? I would like to attach normal texbox events to capture keystrokes and capture value changed.

所以我有4列,每列包含单元数,所有

So i have 4 columns, each of them contains number of cells and all cells in one row should be handled different way based on its type.

基本上我只需要在编辑单元格时才触发事件。

Basically i need my events to be fired only when a cell is being edited.

推荐答案

订阅 DataGridView.EditingControlShowing 事件,然后订阅 TextBox 您需要的事件。

Subscribe the DataGridView.EditingControlShowing event, then subscribe the TextBox event you need.

示例为 TextBox.KeyDown

void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    var txtBox = e.Control as TextBox;
    if (txtBox != null)
    {
        // Remove an existing event-handler, if present, to avoid 
        // adding multiple handlers when the editing control is reused.
        txtBox.KeyDown -= new KeyEventHandler(underlyingTextBox_KeyDown);

        // Add the event handler. 
        txtBox.KeyDown += new KeyEventHandler(underlyingTextBox_KeyDown);
    }
}

void underlyingTextBox_KeyDown(object sender, KeyEventArgs e)
{
    // ...
}






编辑:

现在代码更加正确,因为它遵循 MSDN

Now the code is more correct, because it follows the suggestion given on MSDN:


DataGridView控件托管一个
一次编辑控件,并且只要单元格
类型在两次编辑之间不发生变化,就重用
编辑控件。
将事件处理程序附加到
编辑控件时,因此,您必须
采取预防措施,以避免多次将
附加到同一处理程序。为了避免
出现此问题,请在将
处理程序附加到事件之前,从事件中删除处理程序
。如果处理程序已附加到事件中,则
将防止重复,但是
否则将无效。有关
的更多信息,请参见
DataGridViewComboBoxEditingControl
类概述中的示例代码

The DataGridView control hosts one editing control at a time, and reuses the editing control whenever the cell type does not change between edits. When attaching event-handlers to the editing control, you must therefore take precautions to avoid attaching the same handler multiple times. To avoid this problem, remove the handler from the event before you attach the handler to the event. This will prevent duplication if the handler is already attached to the event, but will have no effect otherwise. For more information, see the example code in the DataGridViewComboBoxEditingControl class overview.

编辑2:

根据评论:

TextChanged 事件在 EditingControlShowing 之前调用,然后在它之后再次调用。

TextChanged event is called before EditingControlShowing, and then again after it.

您可以使用此技巧来区分这两个调用:

You can distinguish between the two calls using this trick:

void txtBox_TextChanged(object sender, EventArgs e)
{
    var txtBox = (TextBox)sender;
    if (txtBox.Focused)
    {
        // second call (after EditingControlShowing) the TextBox is focused
    }
    else
    {
        // first call (before EditingControlShowing) the TextBox is not focused
    }
}

这篇关于将事件附加到DataGridView单元基础的TextBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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