B4J Question B4J Compile in one Monitor and Results in a second Monitor

tdocs2

Well-Known Member
Licensed User
Greetings from Newbieland.... or Stumbling my way through B4J (which I love).

Question 1
My laptop has an external monitor. How do I compile in the laptop and tell B4J to run the app on the second monitor?

Question 2
I always want to keep my MainForm centered on the screen. What event do I use to detect movement?

Question 3
I have modal forms (in separate code modules) which I display. How do I center the modal form on the MainForm?

My apologies if I missed the answers in the Documentation, the Forum...

Thank you for your replies.

Sandy
 

EnriqueGonzalez

Well-Known Member
Licensed User
Ey!
That is quite a very interesting question

Question1
B4X:
dim fx as JFX ' Every b4j project already has this variable
dim screens as list = fx.screens
dim secondscreen as screen
secondscreen.get(1)

mainform.left = secondscreen.MinX * 1.45 ' I have doubts here, the 1.45 is to set the mainform on the middle
mainform.show

I do not have 2 screens so i cannot test it but because in Java screens are not separete but extensions of the primaryscreen it should work

Question3
dialogform.setowner(parentform)

Question2
i am not so sure i cannot answer this question.
 
Upvote 0

tdocs2

Well-Known Member
Licensed User
Thank you, Enrique.

Question3
dialogform.setowner(parentform)

This I have tried, and I did use .SetOwner(Main.MainForm) and what it does it puts the modal form in the same monitor but does not necessarily center it against the MainForm.

I will test your reply to question 1 and post later to recap the issue.

Sandy
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
You can use this code to center the form on the primary or secondary monitor.
Note that it will work even if there is only a single monitor.

B4X:
Sub AppStart (Form1 As Form, Args() As String)
   MainForm = Form1
   MainForm.SetFormStyle("UNIFIED")
   'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
   MainForm.Show
   CenterForm(MainForm, True)
End Sub

Sub CenterForm(Frm As Form, SecondaryScreen As Boolean)
   Dim s As Screen
   If Not(SecondaryScreen) Then
     s = fx.PrimaryScreen
   Else
     For Each s As Screen In fx.Screens
       If s <> fx.PrimaryScreen Then Exit     
     Next
   End If
   Frm.WindowLeft = (s.MaxX + s.MinX) / 2 - Frm.Width / 2
   Frm.WindowTop = (s.MaxY + s.MinY) / 2 - Frm.Height / 2
End Sub
 
Upvote 0

tdocs2

Well-Known Member
Licensed User
Thank you, Erel.

I will try it out... I could not figure out the two questions below.

Question 3 would be addressed by your reply if I decide to center the MainForm and the user does not move it (everything would be centered on the screen so by definition, the modal form would be centered on the screen and so also on the MainForm).

Question 2
I always want to keep my MainForm centered on the screen. What event do I use to detect movement?

Question 3
I have modal forms (in separate code modules) which I display. How do I center the modal form on the MainForm?

My apologies if I missed the answers in the Documentation, the Forum...

Thanks again.

Sandy
 
Last edited:
Upvote 0
Top