Window event order (and timing) question.

I was under the impression that the Window.Show event occurs after the window is instantiated.

What I’m trying to do is set up each window, pass it a few values then have it display something in a Listbox when the window first appears.

I have a Window2 type, implicit instance set to off.
My code is something like this:

var i as integer for i = 0 to numberOfWindow var w2 as new Window2 Window2Array(i) .myString = theString Window2Array(i).myValue = theValue Window2(i)Array.Title = "Window Name" Window2Array.Show next

What seems to be happening is that the window appears as soon as I create it with w2 = new Window2 so the Show event seems to be happening before the values get set.
Clearly there is a hole in my understanding about the order of things and what is actually going on. What might I be doing wrong?

Thanks All

Hello Phillip

Since you are creating an instance of the Window and doing work in a For....Next but you also have Show in the loop

Try calling Show after the loop?

Window event order was undefined, isn’t it ?

I believe you are correct
But calling Show according to the Docs Forces the window to immediately become visible

You can very easily set up a window before making it shown. I think you’re probably stumbling on Visible=true with your Window in the IDE. Check to make sure that’s false so that when you create new instances they’re not visible yet.

Perhaps you’d be interested in my full document based desktop application example on Github. If you are trying to make a document based application (one that opens and saves documents, like TextEdit) then this project is for you.
https://github.com/devtimi/Retinassets

@Tim Parnell,

Thanks. I’ll have a look at your project at some point – I’m sure there’s a lot I can learn from it.
Is there anything wrong with the code snippet I posted that you think might explain the behavior I’m seeing though?

Check to make sure the Window’s Visible property is false/off in the IDE. If it’s true/on, when you make a new instance of the window it will be visible, as you’re seeing.

@Tim Parnell,

Yes, visibility is false.

There is certainly something strange going on (at least strange and unexpected to me).

For example, when I instantiate the window (among other things) I set it’s title to some string:

var i as integer for i = 0 to numberOfWindow var w2 as new Window2 Window2Array(i) .myString = theString Window2Array(i).myValue = theValue Window2(i)Array.Title = "Window Name" Window2Array.Show next

In the new window’s Open event I display in the window’s listbox it’s title:

Listbox.AddRow("My title is: " + self.Title)

It shows that the title is the default title that I entered in the IDE.
If I put the same line of code in the Activate event it’s displays the title that I set by code when I instantiated the window.

I don’t understand this behavior, if the Open even is supposed to fire only after the window is shown.

Read the reality here .

@Emile Schwarz,

Interesting – thank you for that.
What does this mean in a practical sense? If I need to perform any data loading and set up so that when the window does open I can display accurate information?

It might be something you’re up to with the window arrays. Can you share your project, or recreate this issue in an smaller sample project?

Hi @Tim Parnell ,
Those code snippets I’ve shared really is all there is to it.
That and the window setup in IDE (visible = false, implicitinstance = false)

I’ve done nothing with the array of windows besides instantiate the windows (three), add them to the array, show them then check values of variables.

When I pause the app the variable show what I would expect. Just not in the Open event.

The Window.Open event happens when the Window is created i.e. before returning from the “new Window” instruction: check it with debugger.

Usually these activities area done in the Activate event: use a boolean flag to keep track that initialization was already done so the initialization code isn’t repeated every time the window is activated.

@Maurizio Rossi,
Thanks. I just figured out that the Open event is happening when the window is created. I may be overlooking something but that doesn’t sound to me to be the advertised behavior.

I’ll give that a try.

Thanks

I suggest you check out Retinassets. The way I do it is to have a method like “Display” or “LoadDocument” on the window. It sets the properties for the items on the window, and then shows the window. With this approach, I don’t have issues with items not existing yet.

Phillip, I may be missing something here but what is the relationship between w2 and Window2Array?[quote=488305:@Phillip Bond]
For example, when I instantiate the window (among other things) I set it’s title to some string:

var i as integer for i = 0 to numberOfWindow var w2 as new Window2 Window2Array(i) .myString = theString Window2Array(i).myValue = theValue Window2(i)Array.Title = "Window Name" Window2Array.Show next

In the new window’s Open event I display in the window’s listbox it’s title:

Listbox.AddRow("My title is: " + self.Title)

It shows that the title is the default title that I entered in the IDE.
If I put the same line of code in the Activate event it’s displays the title that I set by code when I instantiated the window.[/quote]
As I see it, you are creating a new Window2 named w2 but how is it getting in as an element of the Window2Array()? If in fact it isn’t, then you are never assigning any values to it so it would retain whatever you gave it in the IDE.

Do you have any code in the Open event that affects a visible control?

Documentation about Open event is something that, at least for me, say nothing very precisely.
What I do comes from observation of how and when events gets generated.
Yes, documentation could be better in this case.

@Maurizio Rossi,

So what you suggested sort of works.
However the Activate event still does something strange.

Even though I have a boolean that checks whether or not the window display has been “initialized” with the code in that event handler it still seems to try to refresh the display every time I alter anything having to do with the window display: for example it always scrolls to the top of the listbox when I move the scroll bar.

@Jeff Tullin,
No, nothing that affects any control.
I’d love to have my initialization stuff in the open event but, like I said it doesn’t seem to fire before the window actually opens and is visible.