Close all open windows

I try to close all my open App Windows, one should remain open (WndCustomers):

    dim n, i As Integer
    n = WindowCount
    for i = 0 to n - 1
      if Window(i) isa WndActivitiesMonitor then WndActivitiesMonitor.Close
      if Window(i) isa WndContacts then WndContacts.Close
      if Window(i) isa WndNotes then WndNotes.Close
      if Window(i) isa WndEmails then WndEmails.Close
      if Window(i) isa WndActivities then WndActivities.Close
      if Window(i) isa WndLocations then WndLocations.Close

But not all Windows get closed. Only 3 of them get closed. Is this the right method to close windows when you do not know which is open?


Do it in reverse order. for i = n-1 downto 0. Doing it your way you only close every other one.

Perfect, Bob. This works.


Also, it would be better to use


Hi Tim,

i want one Window to stay open, so i use this:

    dim n, i As Integer
    n = WindowCount
    for i = n - 1 DownTo 0
      if Window(i) isa WndCustomers then Continue

How about

For i = n-1 downTo 0 If NOT window(i) isA WndCustomers then window(i).close Next

isn’t it almost the same? ok, one command less.

We posted at the same time, but no, it is not the same.

By using NOT you simplify his code and make it more readable/easier to follow.

Your code is the complementary code to mine.

Mine says: if it is not a wndCustomers then close it.

Yours says: if it is a wndCustomers then continue, otherwise close it.

I find mine easier to follow, but that might be a personal preferance.

it’s better, because it’s shorter.

I’m a big fan of readability as it makes it easier to follow, update, and maintain your code.

Shorter is not always better, otherwise I would use Perl for everything :wink:

Me too. In this case i didn’t find “my” way hard to read.