Loading preferences not working right

I’m loading preferences to set the window position, and I have a really odd problem. In the code below, the first line runs, but the second doesn’t. If I swap the lines the new first line runs but the second doesn’t. The third line runs and puts up a messagebox (screenshot link below) confirming that the Left window position property has loaded but the Top property hasn’t.

Can anyone suggest what I am doing wrong here?

// code
// =====
// Put the main window back where it was when last closed

winMain.Left = Preferences.WinMainLeft // 929 in prefs, 929 in msgbox
winMain.Top = Preferences.WinMainTop // 537 in prefs, 50 in msgbox
msgbox (“Top is " + Str(winMain.Top) +”, should be " + Str(Preferences.WinMainTop) + “; Left is " + Str(winMain.Left) +”, should be " + Str(Preferences.WinMainLeft))
// ====
//end code

Do you have an exception block at the bottom of the method?

Curious:

If This is happening in the winMain Open (or activate) event, should the code not say

self.Left = Preferences.WinMainLeft self.Top = Preferences.WinMainTop msgbox ("Top is " + Str(self.Top) +", should be " + Str(Preferences.WinMainTop) + "; Left is " + Str(self.Left) +", should be " + Str(Preferences.WinMainLeft))

Im wondering if implicit instantiation is turned on, so that the first line is actually creating a new window called winMain instead of affecting THIS instance.

Yes; the code I posted is in a Try-Catch block (is that what you are asking?). I didn’t include the whole method before as I am not sure how to analyse any exception that might be raised, or indeed whether I’m testing for the right type of exception. Here it is:

// Put the main window back where it was when last closed

Try
winMain.Left = Preferences.WinMainLeft // 929 in prefs
winMain.Top = Preferences.WinMainTop // 537 in prefs
msgbox (“Top is " + Str(winMain.Top) +”, should be " + Str(Preferences.WinMainTop) + “; Left is " + Str(winMain.Left) +”, should be " + Str(Preferences.WinMainLeft))

Catch e As RunTimeException

msgbox(“Exception raised”)

End Try

[quote=357761:@Jeff Tullin]Curious:

If This is happening in the winMain Open (or activate) event, should the code not say

self.Left = Preferences.WinMainLeft self.Top = Preferences.WinMainTop msgbox ("Top is " + Str(self.Top) +", should be " + Str(Preferences.WinMainTop) + "; Left is " + Str(self.Left) +", should be " + Str(Preferences.WinMainLeft))

Im wondering if implicit instantiation is turned on, so that the first line is actually creating a new window called winMain instead of affecting THIS instance.[/quote]

I tried it with self.Left… and also with me.Left, with the same odd result.
How would I check for implicit instantiation?

You can check or uncheck implicit instantiation n the properties pane when the window is selected.
Try putting the msgbox in a timer with a short period. I’m wondering if you aren’t giving the window a chance to adjust before calling the modal msgbox

You can check for latency by using fixed values that are obvious

set
self.top = 500
self.left = 500

and dont rely on the message box… look at the window!

As stated, you can turn off instantiation, but you can also do this:

winMain.title= "Ice Cream"
self.title= "Banana"

Which caption do you see?
If you have two windows, these instructions will hit different ones.
And you can also pause in the debugger, look in global variables, see how many windows are open after these calls, and how many of them are wndMain

Where do you set the values for Preferences.WinMainLeft and Preferences.WinMainTop?

If you are doing it in the Moved event of the window, then the first line will cause the Moved event to fire and overwrite your previous preferences with the window’s current position (causing the second line to not make any changes).

[quote=357809:@Jared Feder]Where do you set the values for Preferences.WinMainLeft and Preferences.WinMainTop?

If you are doing it in the Moved event of the window, then the first line will cause the Moved event to fire and overwrite your previous preferences with the window’s current position (causing the second line to not make any changes).[/quote]
Ah, that is it! I have moved the code that updates the Preferences value from window.moved to the window.close event, and now it updates correctly.

Thank you very much. Perfect answer.