0

Please, what is wrong with this code:

InitializeComponent();
this.KeyDown += new KeyEventHandler(dgvC_KeyDown);

private void dgvC_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyValue == 188)
    {
        System.Media.SystemSounds.Beep.Play();
        e.SuppressKeyPress = true;
    }
    if (e.KeyValue == (int)Keys.Delete)
    {
        MessageBox.Show("323");
    }
}

MessageBox appears twice!

I want to execute some code if comma is pressed and another code if DelKey is pressed on a DataGridView (dgvC).

MessageBox is just for test.

David Hall
  • 32,624
  • 10
  • 90
  • 127
Bonaca
  • 313
  • 3
  • 5
  • 15
  • Looks like you need to show more code. Maybe you bound `dgvC_KeyDown` to multiple events, e.g. KeyDown and KeyUp/KeyPress? – Mario S Jun 04 '12 at 08:13
  • Mario, you're right. I posted more codes. Could you examine it please ? – Bonaca Jun 04 '12 at 08:25
  • Try to search your *form*.cs and *form*.Designer.cs after `dgvC_KeyDown` and see if the event is subscribed to more than once. – Mario S Jun 04 '12 at 08:36
  • I searched the whole project. In form.designer - only one instance: this.dgvC.KeyDown += new System.Windows.Forms.KeyEventHandler(this.dgvC_KeyDown);. On form.cs also - no duplicates. – Bonaca Jun 04 '12 at 08:43
  • According to your code and comments, you've assigned the KeyDown event of **both** the Form and DataGridView to dgvC_KeyDown? – RJ Lohan Jun 04 '12 at 08:44
  • If I change "this.KeyDown += new KeyEventHandler(dgvC_KeyDown);" to "this.dgvC.KeyDown += new KeyEventHandler(dgvC_KeyDown); - then e.SuppressKeyPress does not work. What should I change, pls. – Bonaca Jun 04 '12 at 08:51
  • Yeah, like @RJLohan says, you probably need to remove this line in the constructor `this.KeyDown += new KeyEventHandler(dgvC_KeyDown);` or remove the line from the designer.cs. – Mario S Jun 04 '12 at 08:51
  • But, in this case "e.SuppressKeyPress = true;" does not work ! – Bonaca Jun 04 '12 at 08:53

3 Answers3

2

You are misunderstanding the purpose of e.SupporessKeyPress. In your dataGridView_KeyDown event, your use of;

e.SuppressKeyPress = true;

only prevents the event from being passed to the DataGridView after your method exits.

In your code, you also assign this event handler to the Form_KeyDown event, so the event is captured for 2 different controls, and SuppressKeyPress does not stop them from both receiving this event.

You need to remove this line from your code, which is assigning the handler (I believe erronesouly) to the Form_KeyDown event;

this.KeyDown += new KeyEventHandler(dgvC_KeyDown);
RJ Lohan
  • 6,497
  • 3
  • 34
  • 54
1

It looks like you have subscribed to the KeyDown event twice.

Remove the line in the .Designer.cs file and the e.SuppressKeyPress will still work.

Mario S
  • 11,715
  • 24
  • 39
  • 47
  • Yess! if i remove that line in form.cs - suppress does not work. But if i remove that line in Designer everything works well. Although I don't understand the best - ThankYou ! – Bonaca Jun 04 '12 at 09:03
1

Try inserting inside the ifs.

e.Handler = true;

this way the event won't be repeated.

  • Even though this would work, it's a *better* design to not subscribe to an event more than needed. – Mario S Jun 04 '12 at 09:01