Override or event

  • Thread starter Thread starter Publicjoe
  • Start date Start date
P

Publicjoe

OK Folks, I am one confused puppy. Can someone please explain what the
difference is and when to use each.

I have a form with a button dropped onto it. Without actually doing anything
to the button, I next add the following code:

protected override void OnKeyDown(KeyEventArgs e)
{
System.Diagnostics.Debug.WriteLine(e.KeyCode.ToString());
base.OnKeyDown(e);
}

When you press a key, it is not handled. If you change the tabstop property
of the form to false, the it is now handled. Why?

However, if I put the tabstop back to true and I add an keydown event
instead:

private void button1_KeyDown(object sender, KeyEventArgs e)
{
System.Diagnostics.Debug.WriteLine(e.KeyCode.ToString());
}

When you press a key it is handled. What is happening that is different?

My understanding of what happens when you override a form method seems to be
shot to pieces.

So when should you override form methods and when should you use events?

Any clarification on this subject area would be most appreciated.

Thanks

Mike
 
OK Slight clarification, (read the question before i post it.)

Can someone please explain what the difference is and when to use each.

I have a form with a button dropped onto it. Without actually doing anything
to the button, I next add the following code to the form:

protected override void OnKeyDown(KeyEventArgs e)
{
System.Diagnostics.Debug.WriteLine(e.KeyCode.ToString());
base.OnKeyDown(e);
}

When you press a key, it is not handled. If you change the tabstop property
of the button to false, the key down event is now handled. Why?

However, if I put the tabstop property of the button back to true and I add
an keydown event
instead:

private void button1_KeyDown(object sender, KeyEventArgs e)
{
System.Diagnostics.Debug.WriteLine(e.KeyCode.ToString());
}

When you press a key it is handled. What is happening that is different?

My understanding of what happens when you override a form method seems to be
shot to pieces.

So when should you override form methods and when should you use events?

Any clarification on this subject area would be most appreciated.

Thanks

Mike
 
Hi Publicjoe,

the OnKeyDown method you are overriding corresponds to the KeyDown event
of the form not of the button.

Regards Christof
 
whoops, I meant the keydown event of the form



Christof Nordiek said:
Hi Publicjoe,

the OnKeyDown method you are overriding corresponds to the KeyDown event
of the form not of the button.

Regards Christof
 
Back
Top