nythrix's picture

OnResize before OnLoad

Why is the first OnResize called before OnLoad? Shouldn't it go the other way round?

protected override void OnResize( EventArgs e )
{
    ...
    //fails, projectionMatrixLocation isn't valid, GL setup takes place during OnLoad
    GL.UniformMatrix4( projectionMatrixLocation, false, ref projectionMatrix ); 
}

Comments

Comment viewing options

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

Design limitation in <= 0.9.8 GameWindow: without the resize event, Width and Height would be 0 inside the Load event, causing different issues (GL.TexImage2D(..., Width, Height, ...) -> InvalidOperation).

This can be fixed in 0.9.9+. For consistency's sake, do Windows.Forms or WPF raise a Resize event before or after Load?

nythrix's picture

Don't know about WPF but WinForms do:
1. OnLoad
2. OnResize
The second seems to run only if I resize the visible window manually. No resize on startup.

the Fiddler's picture

Interesting, thanks. I'll check this tomorrow.

Mincus's picture

I'm finding that a resize event is fired from InitializeComponent() in the constructor of a Visual Studio produced form (Add -> Windows Form):

Form: Beginning of Constructor.
Form: OnResize called.
Form: End of Constructor.
Form: OnLoad called.

However, if you created a form yourself, then I guess the resize event wouldn't be fired unless you did so manually or resized the window.

nythrix's picture

@ Mincus: I can't reproduce it on a fresh form. Can you be more verbose? What do you mean "create a form yourself"? I tried hooking the Resize event and overloading OnResize but neither is executed unless I mess with the window.

Mincus's picture

Ok, all I've done here is created a new Windows Form Application (in Visual Studio 2008 Professional), overridden OnResize and OnLoad and added some Debug.WriteLine() messages.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
 
namespace OnResizeExample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            Debug.WriteLine("Constructor called.");
            InitializeComponent();
            Debug.WriteLine("Constructor ended.");
        }
 
        protected override void OnResize(EventArgs e)
        {
            Debug.WriteLine("OnResize override called.");
            base.OnResize(e);
        }
 
        protected override void OnLoad(EventArgs e)
        {
            Debug.WriteLine("OnLoad override called.");
            base.OnLoad(e);
        }
    }
}

The output window after running shows:

Constructor called.
OnResize override called.
Constructor ended.
OnLoad override called.

Which has a resize event fired inside InitializeComponent(), which is part of the Designer section of the class.

By "do it yourself" I mean create your own Form instead of using the Visual Studio process:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;
 
namespace OnResizeExample2
{
    class Form1 : Form
    {
        static void Main(string[] args)
        {
            Form1 form = new Form1();
 
            Application.Run(form);
        }
 
        public Form1()
        {
            Debug.WriteLine("Constructor called.");
        }
 
        protected override void OnResize(EventArgs e)
        {
            Debug.WriteLine("OnResize called.");
            base.OnResize(e);
        }
 
        protected override void OnLoad(EventArgs e)
        {
            Debug.WriteLine("OnLoad called.");
            base.OnLoad(e);
        }
    }
}

The output from this is:

Constructor called.
OnLoad called.

So no resize event fired.

But you don't get all the fancy designer stuff doing it that way.

Attached both solution files. Example1 is the Visual Studio created form, Example2 is the DIY method.
Hope that explains it!

AttachmentSize
OnResizeExample2.zip20.96 KB
OnResizeExample.zip34.63 KB
nythrix's picture

Now I see why the differences. You have:

        private void InitializeComponent()
        {
            //...
            this.ClientSize = new System.Drawing.Size(548, 361);
            //...
        }

which fires the Resize event. But if you let the form have its default size there's no Resize event. Consistent that is.
Long story short there's no Resize event coming from Windows.Forms.Form() constructor itself.