2016-07-05 82 views
-4

虽然运行下面的代码,我得到这个异常:无法转换到事件处理KeyPressEventHandler

无法隐式转换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; 
     } 
    } 
+0

你想达到什么目的? – cverb

+0

可能重复的[无法隐式转换类型'System.EventHandler'为'System.EventHandler '故事板完成](https://stackoverflow.com/questions/16636830/cannot-implicitly-convert-type-system-eventhandler-到系统eventhandlerobj) –

回答

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