简体   繁体   English

KeyPress事件处理程序方法未执行

[英]KeyPress event handler method is not executed

I have written the following code for the DataGridView KeyPress which is not executed in VS 2010: 我为DataGridView KeyPress编写了以下代码,该代码在VS 2010中未执行:

private void DataGridView1_KeyPress(object sender, KeyPressEventArgs e)
    {
         MessageBox.Show("Entering DataGridView Key Press event");
    }

When I press keys in any of the cells of the DataGridView, this method is not getting executed. 当我在DataGridView的任何单元格中按下键时,此方法未执行。 Can anyone tell me where I might be wrong? 谁能告诉我我可能在哪里错了?

I have written the following code. 我写了下面的代码。 Code gets executed on the specified cell only but it is always executing the else part even though I press a Number key. 代码仅在指定的单元格上执行,但是即使按数字键,它也始终在执行else部分。 Else part is executed twice for a single key press event. 对于一个按键事件,其他部分将执行两次。

private void MatCompDtlDataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        DataGridViewTextBoxEditingControl tb = (DataGridViewTextBoxEditingControl)e.Control;
        //tb.KeyPress += new KeyPressEventHandler(dataGridViewTextBox_KeyPress);
        e.Control.KeyPress += new KeyPressEventHandler(dataGridViewTextBox_KeyPress);
    }
    private void dataGridViewTextBox_KeyPress(object sender, KeyPressEventArgs e)
    {
        DataGridViewRow CurrentRow = new DataGridViewRow();
        CurrentRow = MatCompDtlDataGridView.CurrentRow;
        if ((CurrentRow.Index != -1) && (MatCompDtlDataGridView.CurrentCell.ColumnIndex == txtQtyDGV.Index))
        {
            if (!char.IsControl(e.KeyChar)
            && !char.IsDigit(e.KeyChar)
            && (sender as TextBox).Text.IndexOf('.') > -1)
            {
                e.Handled = true;
            }
            else
            {
                e.Handled = false;
                MessageBox.Show("You can only enter Digits and a single Decimal Point. Maximum number of digits on the left side of the decimal point can be four and on the right hand side three");
            }
        }

Key press events when you are editing content in a cell does not come through the DataGridView's KeyPress event. 当您在单元格中编辑内容时,按键事件不是通过DataGridView的KeyPress事件发生的。 You will need to either handle the KeyPress event directly on the editing control (you can get the editing control in the EditingControlShowing event). 您将需要直接在编辑控件上处理KeyPress事件(可以在EditingControlShowing事件中获取编辑控件)。 Alternatively you can derive from the DataGridView and override the ProcessDialogKey method which will get called for key events on the editing control. 或者,您可以从DataGridView派生并重写ProcessDialogKey方法,该方法将在编辑控件上针对键事件被调用。

Add the following line to form's constructor

public Form1()
{
   InitializeComponent();    
   dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);    
}

dataGridView1_EditingControlShowing event

void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
   dataGridView1.EditingControl.TextChanged += new EventHandler(EditingControl_TextChanged);
}

EditingControl_TextChanged event

void EditingControl_TextChanged(object sender, EventArgs e)
{
   //Capturing the value typed into grid's cell
   string text = dataGridView1.EditingControl.Text;
}

be sure your DGV isn't read only, and then try something like: 确保您的DGV不是只读的,然后尝试执行以下操作:

  private void dataGridView1_EditingControlShowing(object sender, 
        DataGridViewEditingControlShowingEventArgs e)
    {
       MessageBox.Show("Entering DataGridView Key Press event");
  }

Your current validation logic look a bit off. 您当前的验证逻辑看起来有些偏离。 You can't be sure that the cell value is a valid decimal value -with specified format- by only checking currently pressed key. 仅通过检查当前按下的键,就不能确定单元格值是有效的十进制值(具有指定的格式)。 I think following sample is closer to what you want : 我认为以下示例更接近您想要的:

//only allow number and decimal separator
if(char.IsDigit(e.KeyChar) || e.KeyChar == '.')
{
    //curren text in cell
    var previousText = (sender as TextBox).Text;
    //cell content after the pressed key char appended
    var nextText = previousText + e.KeyChar.ToString();
    //validate cell content to make sure the format comply
    if(ValidateText(nextText)) e.Handled = true;
    else
    {
        //what to do if it doesn't comply
    }
}
else
{
    e.Handled = false;
    MessageBox.Show("You can only enter Digits and a single Decimal Point. Maximum number of digits on the left side of the decimal point can be four and on the right hand side three");
}

private bool ValidateText(string cellText)
{
    //TODO: put your validation logic here to ensure that..
    //Maximum number of digits on the left side of the decimal point can be four and on the right hand side three
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM