Why checking windows state opens a the window?

I probably stumbled over weird behaviour in Xojo. I simply want to check if a Windows is opened (visible):

IF mywindow.Visible Then
msgobx (“entered”)
End If;

IF mywindow.Visible = True Then
msgobx (“entered”)
End If;

IF mywindow.Visible = False Then
msgobx (“entered”)
End If;

The weird thing: it opens the window mywindow in all 3 cases when it comes to that code.I have no clue how it comes to this weird effect and how I can check if the window is opened or not.

→ Version 2021 / 3.1 (Mac)

This might help explain what is happening:

https://documentation.xojo.com/topics/user_interface/desktop/windows.html#Implicit_Window_Instances

1 Like

Paul, can you perhaps explain the design decision behind Implicit Instance? This seems to cause quite a bit of confusion, as it violates the normal object life cycle (create object in code before you can access any of its properties).

It makes it a lot easier for new users, since they don’t have to think about when/how to instantiate a window. It just happens. That way they can focus on the program logic and not worry about the “details”.

But it makes these implicit instance windows so confusing to users new and old, because the moment you touch them they spring into existence and very often unexpected visibility. Why should these objects act any differently from other objects? It breaks the mental model of how objects behave in Xojo. If he were to try this with any other object, he’d likely get a compiler warning and would learn that he needs to instantiate the object first.

Furthermore, even if he was successful in using the implicit instance, continuing too far down this path lands him in a corner where the code all refers to the implicit instance instead of a class instance, severely constraining its flexibility.

99.9% of New users dont need to use explicit instances, window.show, window.close is all that matters. MOST apps dont even need a lot of explicit instances. Why complicate things for all the use cases?

That is total nonsense. You know that all the controls dragged to a window are implicit instances right? Do you really want to declare explicitly ALL the objects?

Dim Label001 As New Label
Label001.Left = 30
Label001.Top = 100
Label001.Width = 200
Label001.Height = 24
Label001.Value = "some text"
Label001.Visible = True
...
Dim Label061 As Label
...

Dragging a window with implicit instance is exactly the same “mental model” than dragging a control to a window, also a explicit instance of a window is the same as creating controls at runtime.

You just have to rename the window (Window1 to WindowSuper) and create a Variable with the name you were using (Var Window1 as New WindowSuper).

Or store the instance in whatever variable or data structure just asign the reference for the instance to a variable with the same old name to reuse the code (Var Window1 as WindowSuper = App.MyWindows(1)), etc, etc. plenty of flexibility.

1 Like

As for the solution, in the first place, “opened” is NOT the same as visible, you can have the window created but not visible.

Why checking windows state opens a the window?
You are NOT just checking the window state, if you show the window and then just HIDE it (visible = False) that works as you expect, BUT you are not doing that, you are CLOSING (destroying the instance) so the next time you use the window “check it”, Xojo creates a new instance for you. It is NOT weird at all.

To only “check” if a window is open without changing/using implicit instances, you can iterate over all the created windows of your app:

For i As Integer = 0 To WindowCount - 1 
  If window(i) IsA Window2 Then 
    'The window exists...
  End If
Next i
1 Like

I suspect the main reason was to provide a familiar route from Visual Basic, where this was normal.

I wrote a very popular blog article (no longer available) on how Implicit Instance is evil. It confuses new users and leads to undesirable behavior (as the OP has found). Windows are the only place this happens and I really wish in the API 2 overhaul they had just done away with it so that Windows behave like other objects in Xojo and require the new keyword.

I almost always turn implicit instance off.

3 Likes

I find it useful for global floating palettes, search windows and such. Otherwise as you say turn it off.

it is better to use once
MyWindowObject = new mywindow
MyWindowObject.Show
means not using this implicit instance where you access the class/object by name direct.
the disadvantage is you have to store the object somewhere.

Wow, I was surprised how many answers came in and how helpful they are!

I see this is not a new topic. I now understand that my initial posted code initiate an instance of the window and therefore shows it.

Your answer Ivan comes closest to what I I want but I couldn’t make it working. Should “Window2” be the name of the window or the type of the window? Whatsoever… I checked the documentation for “Window” and could evaluate a working solution for my purpose:

For i As Integer = 0 To WindowCount - 1 
  If Window(i).Title = "Title of window to be found" Then
    msgbox ("window opened")
  End If
Next i

The reason why I need to check if a specific window is opened (or not): My application supports an attached device. The user can turn off/on the device at any time (device listener checking that). Turning on/off affects some content of that specific window that I need to activate/deactivate.

You could say that is both… Each window you add to the proyect became a Subclass of Window. The Type of that object is the Name you put on the IDE.

Glad to help, you can check all the properties of the windows, the Type or the Title in this case.

1 Like