Save Control Positions

Hi ,
In my App , i have use the 2 different windows, In Window 1 has 16 labels. Using window 2 i have adjust the all label position.
Till now everything is fine… But I want to save the changes (Label Position).

While i run the next time , Label position is should be new one (Changed position).

Help me out.

You will need to write your positions to disk to make them persistent. I assume your labels remain the same size. If so, you will just need to write the .top and .left properties of the 16 labels and then restore those values from that data.
I hope you have declared your labels as a control set. That way you can loop thru the set to get your values.

[code]dim mTop(15), mLeft(15) As integer
for i As integer = 0 to 15
mTop(i) = myLabel(i).top
mLeft(i) = myLabel(i).left
next

//now do another loop to write the values to disk, or you can write within this same loop
dim b As binaryStream

for i As integer = 0 to 15
b.writeShort mTop(i)
b.writeShort mLeft(i)
Next[/code]

Then reverse the process to recover your properties upon reloading of the window.

Hi,

I wrote positions of my dialogs in separate location text files.

This is the “SaveLoc” method, which has two parameters:
frmDialog As Window, strFileName As String

[code]// This method saves the location of frmDialog

Dim tosValue As TextOutputStream
Dim f_Temp As FolderItem
Dim strLocList As String

// Assemble Location list
strLocList = Str(frmDialog.Left) + “#” +_
Str(frmDialog.Top) + “#” +_
Str(frmDialog.Width) + “#” +_
Str(frmDialog.Height)

f_Temp = GetFolderItem(f_Dialogs_Loc.NativePath + strFileName)
tosValue = TextOutputStream.Create(f_Temp)
tosValue.WriteLine(strLocList)
tosValue.Close[/code]

This is the “LoadLoc” loading code :

[code]// This method loads the last saved location and size
// of a form specified in frmDialog. When there is no
// location file for that form present, the present location
// and sizes are saved in a new location file.

Dim tisValue As TextInputStream
Dim tosValue As TextOutputStream
Dim f_Temp As FolderItem
Dim strPosList As String

// Does the filename for the specified form exists?
f_Temp = GetFolderItem(f_Dialogs_Loc.NativePath + strLocFileName)
If f_Temp.Exists Then
// Location file does exist
// Read dialog locations
tisValue = TextInputStream.Open(f_Temp)
strPosList = tisValue.ReadLine
tisValue.Close
Else
// Location file does NOT exist
// Assemble Position list
strPosList = Str(frmDialog.Left) + “#” +_
Str(frmDialog.Top) + “#” +_
Str(frmDialog.Width) + “#” +_
Str(frmDialog.Height)
// Save Position list
tosValue = TextOutputStream.Create(f_Temp)
tosValue.WriteLine(strPosList)
tosValue.Close
End If

// Set Dialog location
frmDialog.Left = Val(NthField(strPosList, “#”, 1))
frmDialog.Top = Val(NthField(strPosList, “#”, 2))
frmDialog.Width = Val(NthField(strPosList, “#”, 3))
frmDialog.Height = Val(NthField(strPosList, “#”, 4))

// Done[/code]

The above code has the same parameters as the SaveLoc method. f_Dialog_Loc is a global property which points to the place within my user directory where the files are stored. For each dialog there is a separate file. By deleting a file, position location values are restored to standard.

In my dialogs I have a constant “cstrLocFilename” which contains the name of the dialog location file. In my “Open” event of the dialog I put the following call :

LoadLoc(me, me.cstrFileName)

which load and set the location of the dialog.

In the Moved or resized events of the dialog I save the location.

Instead of binary files, I use text files because the user can also easily change values. Values are always stored in this order :

Left
Top
Right
Bottom

I hope this helps.

Chris

I like to add one remark or question to my above post.

I am using these methods for years now and they always worked fine. They came from the time Xojo was RealBasic (I think i developed them around 2006) and where practically never changed by my knowledge. To get you an idea, I know all those lines out of my head, so many times copied these methods in a project.

Anybody any ideas for improvements? Or should I leave them as they are? Sometimes a method which works for years, is not always the best way to do things.

Thank you very much for sharing your ideas.

Chris

If it ain’t broke, don’t fix it :wink:

If it worked for your satisfaction for all those years, why modify it ? I suppose you have chosen a method with one file per dialog for good reason. In the case of the OP, though, I would tend to save all controls position to the same time using Window.Control, and restore all at the same time. Maybe the influence of RubberViews, but it could be simpler to use.

Sub SaveControlsPosition(Win as Window, f as folderitem) Dim t As TextOutputStream If f <> Nil Then t = TextOutputStream.Create(f) for i as Integer = 0 to win.ControlCount-1 if win.Control(i) isa RectControl then t.WriteLine(win.Control(i).name)+","+str(rectcontrol(win.Control(i)).left)+","+str(rectcontrol(win.Control(i)).top) _ +","+str(rectcontrol(win.Control(i)).width)+","+str(rectcontrol(win.Control(i)).height) end if next t.Close End If End Sub

Sub LoadControlsPosition(Win as Window, f as FolderItem) If f <> Nil Then If f.Exists Then // Be aware that TextInputStream.Open coud raise an exception Dim t As TextInputStream Try t = TextInputStream.Open(f) t.Encoding = Encodings.MacRoman dim controls() as string = split(t.ReadAll,EndOfLine) for i as integer = 0 to controls.Ubound-1 PositionControl(Win,controls(i)) next i Catch e As IOException t.Close MsgBox("Error accessing file.") End Try End If End If End Sub

Sub PositionControl(Win as window, Properties as string) //PushButton1,332,221,80,20 dim elements() as string = split(Properties, ",") for i as integer = 0 to Win.ControlCount-1 if RectControl(Win.Control(i)).Name = elements(0) then RectControl(Win.Control(i)).Left = val(elements(1)) RectControl(Win.Control(i)).Top = val(elements(2)) RectControl(Win.Control(i)).Width = val(elements(3)) RectControl(Win.Control(i)).Height = val(elements(4)) exit end if next End Sub

savecontrols.xojo_binary_project

Hello Michel,

Thank you very much for your suggestions.

You are correct, I use separate files instead of one file, because in the occasion that the user put a window by accident out of reach, only that particular file has to be changed, or if he/she does not know how to change, just delete the particular file and the standard values are used. Only problem is that when there are many dialogs, the Dialog_Pos_Loc directory becomes quickly crowded with files. I give the location files the same name as the dialogs, but still sometimes people seems even to have a problem finding that one particular file.

Have a nice day.

Chris