When overriding the Event Methods of a Form, I’ve so far seen no difference between calling the base method and not calling it.
Q – What do the overridden methods really do?
Thanks in advance.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The MSDN documentation for these methods makes no bones about it. They have a “Note to implementers” phrase that demands that you call the base method. Most of the base method implementations are pretty clean and do nothing but raise the event associated with the method. So if you override OnPaint, do all of the painting and not call base.OnPaint() then the Paint event won’t be raised. Which works well enough but can be an unpleasant surprise to another programmer that wants to use the Paint event and has no clue why his code doesn’t work.
Not all of these base methods are that simple though, they do sometimes do extra stuff. Like OnFormClosing() calls ValidateChildren(). That might matter, or you might not care by accident. The complexity of the control is a factor. A good example is DataGridView, quite unlikely it will work well when you skip the base calls. This kind of mis-behavior is very hard to diagnose.
There’s little point in risking this, you don’t have the time machine to tell you that it won’t cause trouble.
There’s is only one aspect about this that should concern you, not otherwise asked in the question. Should you call the base method first, then write the customization. Or should you customize first, then call the base method? This is the reason these methods exist. And it is up to you to decide who’s the boss.