473,756 Members | 7,560 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(KeyEv entArgs e)
{
System.Diagnost ics.Debug.Write Line(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 Form1_KeyDown(o bject sender, KeyEventArgs e)
{
System.Diagnost ics.Debug.Write Line(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


Aug 3 '06 #1
2 1435
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(KeyEv entArgs e) {
Debug.WriteLine ("OnKeyDown" );
base.OnKeyDown( e);
}
Program(bool tabStop) {
KeyDown += delegate { Debug.WriteLine ("KeyDown"); };
Button btn = new Button();
btn.TabStop = tabStop;
Controls.Add(bt n);
Text = "TabStop:" + btn.TabStop.ToS tring();
}
static void Main()
{
new Program(true).S howDialog();
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
12146
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 events since I never saw it used anywhere in MSDN documentation/samples?! Or it will just break when I upgrade to .NET Framework 2.x in the coming years namespace MyNamespac public delegate void MyDel() public class MyBase public virtual...
8
2895
by: JPRoot | last post by:
Hi M. Jeffrey Tan, Just hopping you didn't forget me? :) Thanks JPRoot ----- \"Jeffrey Tan\" wrote: -----
18
4848
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 when should i override OnPaint? I have to implement added functionality in a child class, and am in two minds .. to have the parent fire an event, or make the method virtual and override in the child. Am hoping some reading with description of...
2
4511
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
11795
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 SelectedTextChanged type event? This is an example I found on the web... protected override void OnTextChanged(System.EventArgs e) { try
0
1099
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 that event. This thing works fine, when I am stopping in the debugger for the event, but as soon as I remove the breakpoint from the event handler method. This doesn't work and even the release version of the software also doesn't work. Following...
8
5497
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 all applications will use sessions and to insure that all application use this method, the session properties cannot be overriden. Within the sessionstate tags, the webadmin (upon my request)r emoved the property for timeout, hoping that...
3
1792
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 button, I next add the following code: protected override void OnKeyDown(KeyEventArgs e) { System.Diagnostics.Debug.WriteLine(e.KeyCode.ToString()); base.OnKeyDown(e);
4
12024
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 (I've attached the code at the end of this mail). Is it possible to 'disable' this override until I need it - it's just when I populate the ListViews this sub is called repeatadly. What I'd like to do is populate the listviews and then 'enable'...
0
9287
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9886
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9857
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9722
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8723
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7259
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5155
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3369
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2677
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.