Window Menu example CloseThisWindow method

If I add a MenuItem in the Window menu from the XOJO examples I run into a problem when I close a window.
The menuitem I added has a name WindowRestor
The line of code is

w = WindowMenuItem(WindowMenu.Item(i))

I tried adding this set of lines in but I’m wrong since I have an undefined operator “=”.

If WindowMenuItem(WindowMenu.Item(i)).Name = WindowRestor Then Continue End If

To put the .name inside doesn’t work.

How do I identify the item by name in an array of menuitems.

Name is a string, so you probably want

if WindowMenuItem(WindowMenu.Item(i)).Name = "WindowRestor" then

Well. It improved. I now have quotes and

If WindowMenuItem(WindowMenu.Item(i)).Name = "WindowRestor" Then

I now have a different error on the same line

I also have an EnableMenuItems problem since “WindowRestor” does not exist.

You may have missed something in the example. Which one was it?

How did you add it?

The example Is found in Desktop/Windows and is named WindowMenu.xojo_binary_project.

I copied the code for enablemenuItems to each window and other window and the class.
I also copied the code into App.
It doesn’t have its own manager, so I didn’t have to rewrite anything.

How did I add it (WindowRestor). The Menubar, which I call MenubarAll and it was added to “tab” called WindowMenu with the text of Window

Do you have a class in your project named WindowMenuItem, and is the Super of that class MenuItem?
Are you adding menu items in code?
And are they all of type WindowMenuItem?

What is your code that creates the menu item?

The code is below. It is straight from the example. I already had the Window menu and I added the WindowRestor like I wrote above.
It is from Desktop/Windows/WindowMenu.xojo_binary_projec
WindowMenuItem is the class. When it threw the error that WindowRestor was the only item in the WindowMenu.

It runs without flaw here. What problems are you experiencing?

Note: I’m running an older version of the IDE. Maybe something broke?

You are absolutely correct. By itself it will run correctly. and the example has been around for a while.

That isn’t the problem. If you add anything to the WindowMenu, such as WindowRestore, WindowNext, it will fail.

Here is a slightly modified version. Window Menu with Restore
Try adding to EnableMenuItems “WindowRestor”
It will not autocomplete.
Try running it and you will have the problem I reported in #1

If you are putting a menu in the Window menu that does not inherit from WindowMenuItem then you’ll need to modify the code to check for that.

In App.CloseThisWindow:

[code]
// Search for the specified window and close it.
Dim count As Integer
Dim w As WindowMenuItem

count = WindowMenu.Count - 1

For i As Integer = 0 To count
If WindowMenu.Item(i) IsA WindowMenuItem Then // Ensure the menu is a WindowMenuItem
w = WindowMenuItem(WindowMenu.Item(i))

// Check if the window referenced in the menu is the window to close
If w.IsWindow(closeWin) Then
  // Remove the MenuItem from Recent Items
  WindowMenu.Remove(i)
  
  // Close the Window
  closeWin.Close
  Exit
End If

End If
Next[/code]

In MainWindow.EnableMenuItems:

[code]// Set Checked = True for the current window
// Only the top-most window calls this event, so
// we first uncheck all the MenuItems and then just
// check the one that matches this window.

Dim count As Integer
Dim w As WindowMenuItem

count = WindowMenu.Count - 1

For i As Integer = 0 To count
If WindowMenu.Item(i) IsA WindowMenuItem Then // Ensure the menu is a WindowMenuItem
w = WindowMenuItem(WindowMenu.Item(i))
w.Checked = False

// Check if the window in WindowMenu is this window (Self)
If w.IsWindow(Self) Then
  w.Checked = True
  
End If

End If
Next[/code]

I’ll update the example with these changes.

Thank you very much Paul. Can you also add a similar one for the RecentMenu example.
It needs something like Clear.
The title is [quote]OpenRecentMenu[/quote] and it in Desktop/Menu.

I used [quote]Designing a Recent Items Submenu[/quote] By Charles Yeomans.
It is very complete but overkill for the examples.
It is in RB Developer 2.1 from 2003.