B4J Question Form center to parent not working for ShowAndWait

IndieDev

Active Member
Licensed User
Hi,

This code, from Erel, works for Form.show...
B4X:
Sub MainForm_MouseClicked (EventData As MouseEvent)
   Dim frm2 As Form
   frm2.Initialize("frm2", 500, 500)
   frm2.Show
   SetFormInCenter(MainForm, frm2)
End Sub

Sub SetFormInCenter(Parent As Form, Form As Form)
   Form.WindowTop = Parent.WindowTop + (Parent.WindowHeight - Form.WindowHeight) / 2
   Form.WindowLeft = Parent.WindowLeft + (Parent.WindowWidth - Form.WindowWidth) / 2
End Sub

But, when using Form.ShowAndWait, one has to close the second form once and open again to center to its parent.

Is there any way we can center second form to parent (everytime) while using Form.ShowAndWait?

Thanks.
Regards.
 

MegatenFreak

Active Member
Licensed User
Try performing the position adjustment before calling ShowAndWait:
B4X:
Sub MainForm_MouseClicked (EventData As MouseEvent)
   Dim frm2 As Form
   frm2.Initialize("frm2", 500, 500)
   SetFormInCenter(MainForm, frm2)
   frm2.ShowAndWait
End Sub
 
Upvote 0

IndieDev

Active Member
Licensed User
Hi MegatenFreak,

Thank you for your reply.

Had tried this too.
It doesn't make any difference whether the "centering" code is placed before or after the Form.ShowAndWait command.

Every time I load the main form, I have to click each "second form loading" button twice to center to parent.

Regards.
 
Last edited:
Upvote 0

IndieDev

Active Member
Licensed User
I don't recommend using ShowAndWait. Real modal forms or dialogs are a thing of the past. Use Wait For as done with B4XDialogs. Or just use B4XDialogs with a custom layout.

Ok.
Will try using that.
Thank you.

Regards.
 
Upvote 0

IndieDev

Active Member
Licensed User
I don't recommend using ShowAndWait. Real modal forms or dialogs are a thing of the past. Use Wait For as done with B4XDialogs. Or just use B4XDialogs with a custom layout.

Hi Erel,

Wait For worked like a charm!👍

Thank you. 🙏

Regards.
 
Upvote 0

IndieDev

Active Member
Licensed User
This might be a stupid question, but what do you use "Wait For" with? (Instead of using ShowAndWait)

Hi,
Sorry for the late reply.

This is how I used Wait For....

. On click of button to load a new form (from Main form)
B4X:
Private Sub Btn_New_Click
    FrmNew.Show
    Wait For FrmNew_Done
End Sub


. In FrmNew close button click
B4X:
Sub Btn_Close_MouseClicked (EventData As MouseEvent)
    FrmNew.Close
    CallSubDelayed(Main, "FrmNew_Done")
End Sub

Hope this helps.

Basically, borrowed code from Splash Screen code by Erel. (Splash Screen)

Regards.
 
Last edited:
Upvote 0
Top