473,385 Members | 1,341 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Override or Event - Take 2

OK Try again

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.ToStr ing());
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 Form1_KeyDown(object sender, KeyEventArgs e)
{
System.Diagnostics.Debug.WriteLine(e.KeyCode.ToStr ing());
}

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


Aug 3 '06 #1
2 1409
With the tabstop, the button is getting focus initially; that means that it
is also getting the keypresses.

For instance; take waya the tab stop and run - press keys: both override and
event fire; now (without closing) click on the button (it gets focus); press
keys: neither override nor event fires.

By convention, OnSomeEvent wraps up firing the SomeEvent event, testing for
null etc. Overriding provides a mechanism for:
* knowing that your code (in a subclass) runs first or last, since events
are in subscriber order
* suppressing an event
* altering the parameters to an event
* slightly quicker (more noticeable in 1.1; in 2.0 delegate invokation has
been improved)

etc

Marc

My test code:

class Program : Form {
protected override void OnKeyDown(KeyEventArgs e) {
Debug.WriteLine("OnKeyDown");
base.OnKeyDown(e);
}
Program(bool tabStop) {
KeyDown += delegate { Debug.WriteLine("KeyDown"); };
Button btn = new Button();
btn.TabStop = tabStop;
Controls.Add(btn);
Text = "TabStop:" + btn.TabStop.ToString();
}
static void Main()
{
new Program(true).ShowDialog();
new Program(false).ShowDialog();
}
}
Aug 3 '06 #2
Generally speaking there's no difference between overriding and
attaching and event handler, other than the fact that overriding
allows you to decide if and when to call the base method.

What you're experiencing is specific to key input handling on
controls. Check the help page for the Form.KeyPreview property in the
MSDN Library for details. I don't know exactly what happened but I'm
sure it had to do with control focus issues...
--
http://www.kynosarges.de
Aug 3 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: JPRoot | last post by:
Hi I use the following syntax to have events inherited from base to child classes which works nicely (virtual and override keyword on events). But I am wondering if it is a "supported" way of using...
8
by: JPRoot | last post by:
Hi M. Jeffrey Tan, Just hopping you didn't forget me? :) Thanks JPRoot ----- \"Jeffrey Tan\" wrote: -----
18
by: bhavin | last post by:
Hi, Can someone point me to some good best practices kind of documentation on use of events compared to method overriding. Ex. In Windows Forms when should i have an event handler for Paint, and...
2
by: Ryan Joseph So | last post by:
Hi, Is it possible to override an event replacing its EventArgs w/ my own event? like this is native event protected override void OnDropDownClosed(EventArgs e) { //code goes here... }
8
by: Keith Smith | last post by:
Could some one help me a little? I am trying to understand when/where you would ever want to use "protected override..." code such as this. How is this any different from creating a...
0
by: DotNetBBK | last post by:
Hi, I am using a custom text box, where I need to override TabSelection, as I have to generate an event and based on that event, I need to do some processing in other class, who is listening to...
8
by: bdeviled | last post by:
I am deploying to a web environment that uses load balancing and to insure that sessions persist across servers, the environment uses SQL to manage sessions. The machine.config file determines how...
3
by: Publicjoe | last post by:
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...
4
by: Rob | last post by:
I've constructed a user control inherited from ListView so I can handle and respond to scrolling events (to keep 2 listviews scrolling in sync). My user control includes an Overrides of WndProc...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.