Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 事件仅从第二次触发_C#_Winforms_Gridview_Event Handling_Keydown - Fatal编程技术网

C# 事件仅从第二次触发

C# 事件仅从第二次触发,c#,winforms,gridview,event-handling,keydown,C#,Winforms,Gridview,Event Handling,Keydown,在只能接受数字的gridview中,有一个文本框类型的“数量”列。代码运行良好,但仅从第二个输入开始。我只想在这里用钥匙 private void GridViewSale_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { if (GridViewSale.CurrentCell.ColumnIndex == 4) //Allow only numbers for

在只能接受数字的gridview中,有一个文本框类型的“数量”列。代码运行良好,但仅从第二个输入开始。我只想在这里用钥匙

private void GridViewSale_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{     
  if (GridViewSale.CurrentCell.ColumnIndex == 4) //Allow only numbers for QTY column
    {
      TextBox Qty = e.Control as TextBox;
      Qty.KeyDown += new KeyEventHandler(Qty_KeyDown);
    }
}
void Qty_KeyDown(object sender, KeyEventArgs e)
{         
  if ((e.KeyValue >= 48 && e.KeyValue <= 57) || (e.KeyValue >= 96 && e.KeyValue <= 105)//Allows numerics
    e.SuppressKeyPress = false;
  else
    e.SuppressKeyPress = true;           
  }
private void GridViewSale\u EditingControlShowing(对象发送方,DataGridViewEditingControlShowingEventArgs e)
{     
if(GridViewSale.CurrentCell.ColumnIndex==4)//仅允许数量列使用数字
{
文本框数量=e.控件作为文本框;
Qty.KeyDown+=新的KeyEventHandler(Qty\u KeyDown);
}
}
无效数量\u键向下(对象发送方,KeyEventArgs e)
{         

如果((e.KeyValue>=48&&e.KeyValue=96&&e.KeyValue找到了这种行为的原因:任何第一次按下的键(char、num或symbol)都直接转到“EditingControlShowing”方法而不是“keydown”。因此输入已经被接受,因此问题就出现了。我通过以下解决方法解决了这个问题。(将KeyDown替换为PreviewKeyDown并添加了CellBeginEdit,以便在单元格进入编辑模式之前检查键值:

private void GridViewSale_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
  {
    if (GridViewSale.CurrentCell.ColumnIndex == 4)//Allow only nums for QTY col.
          {
              TextBox Qty = e.Control as TextBox;
              Qty.KeyDown -= OnlyNums_KeyDown;
              Qty.KeyDown += OnlyNums_KeyDown;
          }
  }
private void GridViewSale_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
  {
     if ((e.KeyValue >= 48 && e.KeyValue <= 57) || (e.KeyValue >= 96 && e.KeyValue <= 105))
          {
              //Do Nothing
          }
          else
          {
              cancelEdit = true;
              GridViewSale.CellBeginEdit -= GridViewSale_CellBeginEdit;
              GridViewSale.CellBeginEdit += GridViewSale_CellBeginEdit;
          }
      }
private void GridViewSale_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
  {
      if (cancelEdit == true)
      {
          e.Cancel = true;
          cancelEdit = false;
      }
  } 
private void GridViewSale\u EditingControlShowing(对象发送方,DataGridViewEditingControlShowingEventArgs e)
{
if(GridViewSale.CurrentCell.ColumnIndex==4)//仅允许数量列的nums。
{
文本框数量=e.控件作为文本框;
Qty.KeyDown-=仅限数量\u KeyDown;
Qty.KeyDown+=仅限NUMS\U KeyDown;
}
}
私有void GridViewSale_PreviewKeyDown(对象发送方,PreviewKeyDownEventArgs e)
{

如果((e.KeyValue>=48&&e.KeyValue=96&&e.KeyValue我发现的一个问题是,您每次都在添加事件处理程序,这是不好的。您应该在添加之前先删除该处理程序,或者先检查null。尝试了此操作,但没有成功。Qty.KeyDown-=Qty\u KeyDown;Qty.KeyDown+=Qty\u KeyDown、 我不知道48和57对应什么。但是你为什么不试试这样的枚举呢?比如
如果e.KeyCode==Keys.Shift
…suppress??Thx用于帖子。48-57是num Keys 0-9的值范围;如果(e.KeyCode==Keys.Shift)e.SuppressKeyPress=true,那么就尝试了;但是它仍然允许Shift+1。,应该是这样的(e.KeyCode==Keys.Shift+'numeric'键)if
末尾缺少一个括号。您也可以使用它。
e.supresskeypress=!((e.KeyValue>=48&&e.KeyValue=96&&e.KeyValue)