B4J Question CenterForm Problem

GMan

Well-Known Member
Licensed User
Longtime User
i am using this Sub from @LucaMs

B4X:
'Center the form, on the parent form if it is passed (you can pass Null)
'otherwise on the Desktop.
Private Sub CenterForm(Frm As Form, Parent As Form)
    Dim JFX1 As JFX
    If Initialized(Parent) Then
        Frm.WindowLeft = Parent.WindowLeft + (Parent.Width - Frm.Width) / 2
        Frm.WindowTop  = Parent.WindowTop  + (Parent.Height - Frm.Height) / 2
    Else
        Dim PS As Screen = JFX1.PrimaryScreen
        Frm.WindowLeft = (PS.MaxX - PS.MinX) / 2 - Frm.Width / 2
        Frm.WindowTop  = (PS.MaxY - PS.MinY) / 2 - Frm.Height / 2
    End If
End Sub

I want to center my MainForm (B4XPages), but this doesnt work:
B4X:
 CenterForm(Root,Null)

What am i missing ?
 
Last edited:

GMan

Well-Known Member
Licensed User
Longtime User
Try B4XPages.GetNativeParent(Me) for the current form
Does nothing (of course), but this gives me the parent (which i dont need 'cause i can pass Null (for Desktop))
Its the Form (the 1. parameter) which i need.

B4X:
CenterForm(???,Me)

CenterForm(???,Null)
Should also work.

So, your code seems OK, but this error comes up in the IDE


B4X:
CenterForm(Me,Null)
src\b4j\example\b4xmainpage.java:329: Fehler: Inkompatible Typen: b4xmainpage kann nicht in Form konvertiert werden
parent._centerform((anywheresoftware.b4j.objects.Form)(parent),(anywheresoftware.b4j.objects.Form)(parent.__c.Null));

when using this:
B4X:
CenterForm(Me,Null)

But this works and sets the MAinForm to the center of the screen:

B4X:
    B4XPages.GetNativeParent(Me)
    CenterForm(B4XPages.GetNativeParent(Me),Null)
 
Last edited:
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Like This: CenterForm(B4XPages.GetNativeParent(Me),Null)

It returns the Parent Form for the current page.

CenterForm(???,Me) won't work either as the keyword Me returns the Page Class, not a Form.
 
Last edited:
Upvote 0

behnam_tr

Active Member
Licensed User
Longtime User
 
Upvote 0
Top