Classname as String

I use this code to close a window if open:

    for i As Integer = WindowCount - 1 DownTo 0
     
      if Window(i) isa WndContacts then Window(i).Close
      
    next

Now i want to use this in a method so i don’t have to write it several times in my code. I want to use the WindowName as a Parameter for this method. In my for-next loop, WndContacts is the name of a window. When i just give a string to the parameter (“WndContacts”) i get an error, because the method need a Classname (WndContacts) not a string.

How can i give the Classname as a parameter to the method?

You could always use Introspection.TypeInfo.

Sub CloseAll (windowInfo As Introspection.TypeInfo)
  for i as integer = WindowCount - 1 downto 0
    dim ti as Introspection.TypeInfo = Introspection.GetType( Window( i ) )
    if ti.FullName = windowInfo.FullName then
      Window( i ).Close
    end if
  next
End Sub

Call it as:

CloseAll( GetType( WindowClass ) )

Damn Kem, why are you so fast?

I’ve heard his wife asks the same question (JUST KIDDING!!! :slight_smile: )

Thanks Kem. Works fine.

Can i use this approach also to store the Window.Top and .Left Position?

I don’t understand the question. You don’t need Introspection to access the Left and Top properties.

You’re right.

My first question is answered, thank you.

I’m now sitting on a problem where i try to store the position of the window before i close it so i can open it later on the same position. Let me try some things, and maybe i’ll be back later with my question on how to store the window-position.

Create a preferences class or module that includes methods that take a window as a parameter, Store and Restore. In the Window.Close event, call pref.Store( self ) and in Open call pref.Restore( self ). Those methods will know how to save and restore the settings for the window.

Just avoid some “gotchas”. Do not restore a window to a position that would make it invisible to the user, for example, placing it on a second screen that no longer exists. Do not restore Height and Width if they would take those values out of the Min and Max bounds.

Thanks, Kem. I will try this.