Newbie - Center Main Window

I am trying to open my main form in the center of the screen. I found this code on this form:

Center Window self.Left = (screen(0).Width - self.Width) / 2 = (screen(0).Height - self.Height) / 2

I got a syntax error, but no reason for the error… so I tried this:

self.Left = (screen(0).AvailableWidth - self.Width) / 2 = (screen(0).AvailableHeight - self.Height) / 2

Again a syntax error with no reason to what is wrong.

It says to put this code in the open event but there isn’t one list. So I tried in the Activate event.

I can’t believe that in 2 hours I can’t find a way to center a form. It should not be this hard.

@David_Ferguson Try something like this in the Window.Open event.

Var myScreenW as Double = screen(0).Width 
Var myScreenH as Double = screen(0).Height 
self.Left = (myScreenW /2) - (self.Width /2)
self.Top = (myScreenH /2) - (self.Height /2)

Thank You!!

sorry for the frustrated tone of my post.

1 Like

No worries - been there :slight_smile:
Let us know if you have any other blockers.


I do have an other question. In the original post it was said to put the code in the event. I am using Xojo 2020 R1 and when I look at the list of events for my main form. There isn’t an “open” one listed. So I put this code in the “Activate” event. And while it does make my form open in the center, it also makes it move back to center each time it regains the focus. This isn’t what I want. Only when the form opens.

Do you mean Window (What form)? Is this Web, Desktop, or iOS?

In the IDE, set the ‘Placement’ property to ‘Parent Window’
Should be enough.

If this is a desktop app, and you do not have an Open event, add one by right clicking your window, ‘Add to Window’, Event Handler, Open…


This is a desktop app, on Windows 10, it is the main form that opens when the app stars

When I set the “Placement” property to “Parent Window” then run and open the app for the very, very fist time it is centered. Then if I move the window, close the app and restart, it doesn’t open centered. It opens wherever it was when it was closed.

I may be stupid here, but I can’t find this “Open” event that I have read so much about today.
I click on my from “winMain” to make sure it is current selected. Then I right click and use “Add to Window” ==> Event Handler ==> this is as far as i get I just don’t see “open” listed. I have a screenshot but apparently I can’t add that here. The events are listed in alphabetical order and it goes like this.



Nothing about “open” nothing that starts with “O” at all.

Are you working off of a previous project or a sample project where maybe the “open event” has already been added

Yes, I was unaware that a method would not be listed in that dialog if was in use. how do I find it?

Look in the IDE on the left and under the window name expand it and see if that event is already listed
If it is then it has already been added to the window

Found it. :slight_smile:

Everything is much better now. Found the right event. the code works as expected. :slight_smile:

I need to go watch some more Xojo videos.


Glad you got it solved