If a Windows is Opened o Closed

Hi group, I need to display messages only if a certain window is open. How can I check if it is open or closed … I wrote if reminder.visible then … but it doesn’t work.
Thanks.

It is possible to test the window.title

In that window, add a shared property IsOpen As Boolean. In the Opening event, set that to True, and in the Closing event, False.

Elsewhere in code, you can check reminder.IsOpen.

7 Likes

With some introspection you can get the name:

Public Function getWindowName(Extends theWindow as Window) As String
  
  'returns the name of a Window
  
  Dim theTypeInfo as Introspection.TypeInfo = Introspection.GetType(theWindow)
  Return theTypeInfo.Name
  
End Function

And then:

dim FoundWindow as Boolean
for currentWindow as Integer = 0 to App.WindowCount - 1
  dim theWindow as Window = App.Window(currentWindow)
  if theWindow.getWindowName = "PreferencesWindow" then
    theWindow.Show
    FoundWindow = true
  end if
next

if not FoundWindow then
  dim theChecker as CheckForUpdatesDelegate
  theChecker = AddressOf CheckForUpdates
  dim thePrefs as new PreferencesWindow(theChecker)
  thePrefs.Show
  thePrefs.GoToRegistration
  
end if

You can iterate through windows to see if that specific window is open:

If WindowCount > 0 Then
  Var foundWindow As Boolean = False
  Var n As Integer = WindowCount - 1
  
  For i As Integer = 0 To n
    If Window(i) IsA FileSetting Then//FileSetting is a window
      foundWindow = True
      Exit//no point in searching anymore, we found it
    End If
  Next i
  
  FileSettings.Enabled = If(foundWindow, False, True)//enable this thing if it found a FileSetting window
  
End If
4 Likes

Hi Beatrix, I’m trying your solution, but I get several errors, I’m probably doing something wrong or I’m missing something.

I quickly grabbed some code. The second part with the update check is not needed.

getWindowName comes from some introspection:

Public Function getWindowName(Extends theWindow as Window) As String
  
  'returns the name of a Window
  Dim theTypeInfo as Introspection.TypeInfo = Introspection.GetType(theWindow)
  Return theTypeInfo.Name

End Function

Hi Beatrix, there are errors that I don’t understand how to solve.
What does it depend on?


Thanks

dim theWindow as DesktopWindow

It improved a bit but it says it’s not a member of getWindowName…

Did you add the function “getWindowName” I posted above?

Yes but it doesn’t work. It gives me errors like it does in photos…

I wrote an alternative code.
Is it ok?
Can you tell me if it is ok or can be improved?
(I need to find the name of a window in the open windows, and then I can check if this window is open or closed)

Look carefully at your code. It will examine window number zero and no others.

Sorry for the delay in responding TIM, but my code tells me that there are X windows open, and one of these has the title as “WindowName”". I did some tests and it seems to work.
If I open 6 windows, it reads all 6 and identifies if there is one with the indicated title.

In both branches of the if-statement, you Return. Therefore it can only examine one window. The exit statements are never reached.

1 Like

Sorry, I can’t understand the problem, maybe I don’t translate English well. Maybe I understood what you mean, basically, the window you are looking for is there or it isn’t, it comes out of the if statement. You’re right, I have to remove the return from the if that tells me if it isn’t there. In practice, if it isn’t there, you have to keep looking until you find it or until the windows run out. Right ?

Rewrite it thus:

Var  n as Integer = WindowCount - 1

for i as Integer = 0 to n
  if  (Window(i).Title=NomeFinestra)  then Return True
next

Return False
2 Likes

Correct. Tim’s code is clean and short but I wanted to comment on your original code to try to make sense to you:

Dim myReturn as Boolean // this will be False by default, will change to True if we find the Window
Dim n as Integer = WindowCount
for i as integer = 0 to n-1
  messagebox " Are " + n.ToString + " windows opened"
  if Window(i).Title=NomeFinestra then
    messagebox "OK window " + NomeFinestra + " is already open"
    //return True
     myReturn = True //only change to True if we find the Title
    exit //exit the for/next because we found the Window
    //or we can just Return True and don't use the above 2 lines
  Else
    messagebox "NO," + NomeFinestra + " windows is closed"
    //Return False //we should not return False on the first 'No' window found
    //exit  // we should  not exit either
  end if
next
Return myReturn //either True if we found the window or False if not

I guess you are using MessageBox to help you debug your code.
Someone recommended me to look at DebugLog instead (when I started using Xojo and learn about debug), so now is my turn to recommend you use System.DebugLog instead of MessageBox.

4 Likes

I strongly agree with @AlbertoD here.

Using MessageBox (or MsgBox in older versions) changes the execution of the program. It may pause code, cause additional events to be fired, etc…

In other words, you may be causing additional issues of getting false positives in the code you’re debugging when you try to “Message Box Debug”.

It’s very easy to log errors, just change any MessageBox call like this:

Before:

MessageBox("My Error.")

After:

System.Log(System.LogLevelDebug, "My Error.")

The first parameter is the “Log Level”. It’s easiest to just use “System.LogLevelDebug” unless you have a reason to use a different option.

The errors show up in the messages pain in the IDE:

Simple example app attached.

myLoggingApp.xojo_binary_project.zip (5.2 KB)

3 Likes

And to see the logs, just click in the right icon (@ window IDE bottom)
image
the one with a star.

The area is updated in real time during the application excution.
Try it and love it.