Loop Window Properties and save to database

I am in the planning stages of my app ( converting from VB6 ) and can’t find how to do this.

Either I am searching for the wrong terms, or it can’t be done.

I would like to be able to loop through all the Properties of a New Instance of a Window ( or any other control ) when I close the Window.

This is both for standard Window properties and also for user created properties.

My app will have multiple instances of the same window ( there is a custom Window Property that is a unique identifying ID for the window instance ), and I am wanting to save all the properties to a database. So when I later open another instance with the same unique ID, I can retrieve the properties from the database.

I know I can list them manually, but was thinking of adding a large number of Window Properties, that will then keep track of a lot of check boxes, user settings, etc.
A loop through all the properties will ensure that I do not miss any.

[quote=186172:@Dave OBrien]I am in the planning stages of my app ( converting from VB6 ) and can’t find how to do this.

Either I am searching for the wrong terms, or it can’t be done.

I would like to be able to loop through all the Properties of a New Instance of a Window ( or any other control ) when I close the Window.

This is both for standard Window properties and also for user created properties.

My app will have multiple instances of the same window ( there is a custom Window Property that is a unique identifying ID for the window instance ), and I am wanting to save all the properties to a database. So when I later open another instance with the same unique ID, I can retrieve the properties from the database.

I know I can list them manually, but was thinking of adding a large number of Window Properties, that will then keep track of a lot of check boxes, user settings, etc.
A loop through all the properties will ensure that I do not miss any.[/quote]

You can use Introspection to get all the attributes of a window. See http://documentation.xojo.com/index.php/Introspection

Thank You SO MUCH, Michel.

From your pointers, I have got to the following code to list the window properties, and it appears to be working :

  Dim ipft as string
  Dim internalProperty() As xojo.Introspection.PropertyInfo = xojo.Introspection.GetType(me.window).Properties()
  
  TextArea1.text = TextArea1.text + EndOfLine +  "Window Properties"
  TextArea1.text = TextArea1.text + EndOfLine +  "Count = " + str(UBound(internalProperty))
  
  For i as Integer=0 to Ubound(internalProperty)
    
    ipft = internalProperty(i).PropertyType.Fullname
    
    if ipft = "Boolean" or  ipft = "Int32" or  ipft = "String" then
      TextArea1.text = TextArea1.text + EndOfLine +  str(i) + " -- " + internalProperty(i).Name + " --- " + cstr(internalProperty(i).Value(me.Window))
    else
      TextArea1.text = TextArea1.text + EndOfLine +  str(i) + " -- " + internalProperty(i).Name + " --------------------------- " + internalProperty(i).PropertyType.Fullname
    end if
  next

The results show me :

Window Properties
Count = 39
0 -- BackColor --------------------------- Color
1 -- Backdrop --------------------------- Picture
2 -- Bounds --------------------------- REALbasic.Rect
3 -- CloseButton --- True
4 -- Composite --- False
5 -- ContentsChanged --- False
6 -- ControlCount --- 28
7 -- DockItem --------------------------- DockItem
8 -- Focus --------------------------- RectControl
9 -- Frame --- 0
10 -- FullScreen --- False
11 -- FullScreenButton --- False
12 -- Graphics --------------------------- Graphics
13 -- Handle --- 330406
14 -- HasBackColor --- False
15 -- Height --- 416
16 -- Left --- 369
17 -- LiveResize --- True
18 -- MacProcId --- 0
19 -- MaxHeight --- 32000
20 -- MaximizeButton --- True
21 -- MaxWidth --- 32000
22 -- MenuBar --------------------------- MenuBar
23 -- MenuBarVisible --- True
24 -- MinHeight --- 64
25 -- MinimizeButton --- True
26 -- MinWidth --- 64
27 -- mModifiedInitialized --- 0
28 -- MouseCursor --------------------------- MouseCursor
29 -- MouseX --- 553
30 -- MouseY --- 122
31 -- Placement --- 0
32 -- Resizeable --- True
33 -- Title --- 1
34 -- Top --- 176
35 -- Visible --- True
36 -- Width --- 628
37 -- MyDBFile --------------------------- FolderItem
38 -- MyDatabase --------------------------- SQLiteDatabase
39 -- winID --- 1

I should now be able to save the properties for each instance of the window as the window closes.

Thanks again.

Also just an observation for those that follow with my minimal level of understanding :

The code that gets the PropertyInfo :

Dim internalProperty() As xojo.Introspection.PropertyInfo = xojo.Introspection.GetType(me.window).Properties()

and is used to list / return the property name :

internalProperty(i).Name

is listing the Property Name of the Super / Parent of the instance of the window.

So if you have Window1 and create a New Instance of Window1, then list the Proprty Names, it is listing the property names of the parent ( Window1 ).

( hope I worded that right ).

This was great until I wanted the actual value of each property in the Instance of the Window.

So this would not work :

  internalProperty(i).Value

and had to change to read the value of the properties for the individual instance of the Window where the code was being run :

internalProperty(i).Value(me.Window)

I hope this helps someone in the future from searching for hours like I did.

What is it about the actual window you wish to save? Is it actually Window properties such as size and position or is it content that exists in the windows, such as the .Text property of TextField’s or the state of CheckBoxes?

Most of the properties you listed above are set a design time and are always going to be the same.

Hi Jeremy

My thinking was the save the width, height, etc.

But I also thought to add properties to the Window to track all the individual settings ( like checkboxes ) that I have on the instance of the window.

So if I change, for example, CheckBox1, then the Window property mwCheckBox1 would be used to ‘track’ the value of the item.
I imagine something like : CheckBox1 action = set me.window.mwCheckBox1.value = me.value ( correct syntax still to be worked out, but you get the idea ).

When it comes to saving the settings, then I only have to loop the properties of the instance of the Window ( maybe saving only those property values where the property name starts with ‘mw’ ) and it also makes it simpler for me to read the values from another window.

So maybe WindowB has a button that loops through all the open instances of Window1 and lists all of the mwCheckBox1 properties from each window1 instance.

Thanks Jeremy. Am aware of that, but as you see from the above reply, I am looking at adding many other properties to the window, so those would be a lot more ‘non-constant’ than the standard built-in control properties.

I just didn’t want to start adding properties if I was not able to easily loop through the properties save the values to a database.

If I were you, I’d write the window location and size using normal code (not introspection) and then loop through the Controls() array:

for controlIndex as Integer = 0 to ControlCount - 1
  dim ctrl as Control = Controls(controlIndex)
  //do something here
next

I believe that would be a cleaner solution and do more closely to what you actually want.