objarni's picture

GLControl question

I've been checking out the WinForms examples.

Why is it that the OnLoad method is overloaded and, as is the Windows.Forms 'convention', not implemented as an event handler of the Load event of the form? Typical code is:

        // OnLoad overriden, not in event handler
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            glControl1_Resize(this, EventArgs.Empty);
            GL.ClearColor(Color.Crimson);
        }
 
        // Paint NOT overriden, handled by event handler code..
        private void glControl1_Paint(object sender, PaintEventArgs e)
        {
            GL.Clear(ClearBufferMask.ColorBufferBit);
            glControl1.SwapBuffers();
        }

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler.'s picture

As a rule of thumb, you should overload if you inherit from a class or hook events if you don't. There is a small performance hit associated with events (at least there was in .Net 1.1), and forgetting to unhook an event may lead in memory leaks.

Which example is this? From this snippet, it seems that the OnLoad method refers to the containing Form (which is inherited), while glControl1_Paint refers to the GLControl (which is not inherited).

objarni's picture


As a rule of thumb, you should overload if you inherit from a class or hook events if you don't. There is a small performance hit associated with events (at least there was in .Net 1.1), and forgetting to unhook an event may lead in memory leaks.

Thanks for the rule of thumb, seems easy enough to follow.

Apart from the unhook-eventhandler-thing, would there be any semantic difference if I hooked to the Form.Load event instead of overloading the OnLoad method, regarding the GLControl?


Which example is this? From this snippet, it seems that the OnLoad method refers to the containing Form (which is inherited), while glControl1_Paint refers to the GLControl (which is not inherited).

It example W01_First_Window.cs, but you are right; I'm confusing the form and the control here.

the Fiddler.'s picture

No, the OnLoad method just raises the Load event - using either shouldn't make any difference.