Passing Window into Function Question

Hello all,

I know I am doing something slightly wrong here. I am testing for an Open Window STATICALLY via the following Function (which works fine) however it is not dynamic.

Function fOpenPrefWinCheck() As Boolean
  Dim n as Integer = WindowCount
  for i as integer = 0 to n-1
    if Window(i) isa Preferences_Window then
      Return True
      Exit
    Else
    end if
  next
End Function

I have been trying to replace the ISA Preferences_Window with ISA InWindow (which InWindow is a variable passed into the function as a WINDOW).

This isn’t working for me as the compiler says “There is no class with this name if Window(i) Isa InWindow then”

Am I limited to having to have a specific function like this per window?

Thanks again!

Without testing, I think you’re running into the trickery that Xojo pulls where windows are concerned, i.e., a window can be both a subclass of Window and and instance of that window. When you pass it in a parameter, you are passing the instance, not the class, so you cannot use IsA.

Again without testing, Introspection might help here.

That makes sense based on my testing thus far Kem. Thanks for confirming that.

Something like this seems to work (warning, it’s only been tested for a simple scenario):

  Dim inputWin as Introspection.TypeInfo
  inputWin=Introspection.GetType(InWindow) //Pass in InWindow as Window
  
  Dim n as Integer = WindowCount
  dim thisWin as Introspection.TypeInfo
  
  for i as integer = 0 to n-1
    thisWin = Introspection.GetType(window(i))
    if thisWin.Name = inputWin.Name then
      Return True
      Exit
    Else
    end if
  next

Bill cool thanks! I am not that familiar with Introspection outside of Christian using it to Find people’s Ships during last years BattleShip XDC 2013 :slight_smile:

Ill give it a try and thank you again.

Bill every time i iterate through this function it always returns true even when the window is hiding or not open. How did you test that on your side? Thanks again!!

What do you mean “hiding”? And can you post your code?

Window.Hide

I’m a little confused. In order to pass a window to the function, it has to already exist, which means it will always be open and in the list of windows. There is no way to pass the class name to a function, you have to pass an instance. Which means you have to have an open window to pass.

Tim thank you as that is what Kem was alluding to above I bet.

No problem as I only have 4 windows in this one app I will manage with 4 functions. thank you again for all of everyones input!

You would have to pass in the name of the window class as a string. Then the introspection solution would work.

I tried the exact code that Bill posted above, but it doesn’t work properly as you guys explained above. :slight_smile:

Got it. I like Tim’s idea of passing in the name instead.

Well I warned you I only tested it for a simple scenario. LOL But you could add “and window(i).visible = true” to my if statement.