Determining Which Window is Active and Other Things related

I am looking for ideas on how I can programmatically determine which Window in my app is the active window when more than one is open.

As well, need ideas on how to determine from which window another window is opened from.

Currently I have a public variable (Property) called “VisibleWindow” that I populate with a given Window’s name when it gets the focus. I also have a variable (Property) in each Window called OpenArgs. This is programmatically populated by the calling window’s name. I then refer to VisibleWindow and OpenArgs when doing other things such as when a window closes.

This all seems to work but I’m thinking there must be a more efficient and perhaps “built-in” way to do this.

Window(0)

In WE it is Session.CurrentPage

Thanks… but how do I use it? (can’t find any examples in the documentation)

Without knowing what it is you’re trying to do it’s hard to say

I hope the following is as clear as mud LOL

Further to my OP then…

Scenario … I have 4 Windows.

Main
wOne
wTwo
wThree

Each window has a timer with code to catch the Control Key combinations ie, CtrlP, CtrlA, CtrlR, etc.
The code needs to know which window is the active one otherwise CtrlP used on wTwo could actually run the code on wOne’s window.

As I said above, I would assign the window’s name (or something) to a Public property called VisibleWindow when a window gets opened. The code in the Timer of each window starts off with <If VisibleWindow=“windowname” then … this code can run. Windowname would be ie, wOne, wTwo, etc.

When I close a window, I then have to assign whatever window is then active to VisibleWindow. Additional code is required to specify to a window’s “OpenArgs” property as to which window called it.

For example, wOne can be opened from Main.
wTwo can be opened from wOne and Main
wThree can be opened from wOne or wTwo.

When closing any of those windows, I use OpenArgs to determine what to populate “VisibleWindow” with.

Example, in the Close event of wTwo…

If OpenArgs=”wOne” then
VisibleWindow=”wOne”
wTwo.Close
wOne.Show
End If

I am just looking for a slicker way to do this and reduce coding if possible.

RE: your timer code, you can simply ask, “am I the active window” and skip the code if you’re not.

If self = window(0) then
   // I'm the active window
   process keystokes
end

The above assumes the timer is embedded in the window and the code is in the timer’s Action event in the window. If the code is in a timer subclass, the test would be something like

if me.window = window(0) then

[quote=46441:@Tim Hare]RE: your timer code, you can simply ask, “am I the active window” and skip the code if you’re not.

If self = window(0) then
   // I'm the active window
   process keystokes
end

The above assumes the timer is embedded in the window and the code is in the timer’s Action event in the window. If the code is in a timer subclass, the test would be something like

if me.window = window(0) then [/quote]

Thanks Tim, I’ll give that a try. Now just need to simplify the “OpenArgs” thing.

Is there a way to determine which window is the active window in code in a Module?

Example, if I create a Method in a module (which would be publicly visible/accessible), how might I code it to determine which window is active on the screen if more than one window is open or visible?

You can test the Window.Title property of window(0) if each window has a different title. If they are all called the same thing then I’m not sure how it could be done. Maybe add a unique name property to each window and test window(0).name in the module’s method?

Jason, thanks, I’ll look into that; however, “window(0).name” returns an error “this item does not exist”.

Right, you will need to create a property in each window called name, then test it against the values available to determine which window you re dealing with. Hope that makes sense?

I created a Property in each of 3 windows called wName and assigned the “name” of the window to the property.

In a module I created a Method called ActiveWindowName and entered a simple If statement…

If window(0).wName=“Contacts” then
//blah blah
End If

I still get the same error “This item does not exist”

Sorry, I am just not “getting it”

Ok so I think I just realized the problem. The windows will have to be a subclass of Window for a method like that to work. That will complicate things so my solution is probably not the best. In the app I was thinking of, I used a subclass of window so this was relatively easy for me to implement. If the Windows are not subclasses you will probably need someone else to help you with a different method of doing this.

Can you subclass a window? I didn’t think so.

Anyway, window(0) by itself would not know what kind of window you are referring to so that is why it won’t find a property.

You need to do something like this:

if Window(0) isa MyWindow then dim w As MyWindow w = Window(0) If w.wName="Contacts" then whatever end

[quote=46608:@Steve Albin]Can you subclass a window? I didn’t think so.

Anyway, window(0) by itself would not know what kind of window you are referring to so that is why it won’t find a property.

You need to do something like this:

if Window(0) isa MyWindow then dim w As MyWindow w = Window(0) If w.wName="Contacts" then whatever end[/quote]

“MyWindow” … that must be generic for something but what? What should I really be referencing?

[quote=46613:@Rick Yerex]“MyWindow” … that must be generic for something but what? What should I really be referencing?

[/quote]
MyWindow is the name of your window as named in the Navigator. Or, in other words, the class name.

That’s what I thought so I put the following code into the ActiveWindowName method in the module…

  if Window(0) isa wContacts then
    dim w As wContacts
    w = Window(0)
    If w.wName="Contacts" then 
      //whatever
    end
  end

and I get the following when I try and run it…

Code, Module1.ActiveWindowName, line 3, Type mismatch error. Expected wContacts.wContacts, but got Window, w = Window(0)

You need to cast to the appropriate type:

w = wContacts(Window(0))

I knew I was missing something.

My code should be adjusted as so:

for i as integer = 0 to WindowCount - 1 if Window(i) isa wContacts then if wContacts(window(i)).wName = "Contacts" then MsgBox "Found it" end next