无法将Eventhandler转换为KeyPressEventHandler

时间:2016-07-05 08:45:49

标签: c#

在运行以下代码的同时,我得到了这个例外:

  

无法将System.Eventhandler隐式转换为   System.Window.Form.KeyPressEventHandler

    private void grdPOItems_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        int colIndex = grdPOItems.CurrentCell.ColumnIndex;
        string colName = grdPOItems.Columns[colIndex].Name;
        if(colName == "Qty" || colName == "Rate")
        {
            e.Control.KeyPress += new EventHandler(CheckKey);
        }
    }

    private void CheckKey(object sender, EventArgs e)
    {
        if (((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar!='.'))
        {
            e.Handled = true;
        }  
    }

1 个答案:

答案 0 :(得分:1)

根据MSDN,您的处理程序具有错误的签名。请改用它。

 e.Control.KeyPress += CheckKey;

private void CheckKey(object sender, KeyPressEventArgs  e)
{
    if (((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar!='.'))
    {
        e.Handled = true;
    }  
}