B4J Question [ABMaterial] Why SetFocus didn't work?

incendio

Well-Known Member
Licensed User
Longtime User
Hi guys,

I modified Login modal sheet from demo, into like these
B4X:
Sub ConnectLoginSheet() As ABMModalSheet
    Dim myModal As ABMModalSheet
    myModal.Initialize(AppPage, "login", False, False, "")
    myModal.Content.UseTheme("modalcontent")
    myModal.Footer.UseTheme("modalfooter")
    myModal.IsDismissible = False

    ' create the grid for the content
    myModal.Content.AddRows(3,True, "").AddCells12(1,"")
    myModal.Content.BuildGrid 'IMPORTANT once you loaded the complete grid AND before you start adding components
    
    ' create the grid for the footer
    ' we add a row without the default 20px padding so we need to use AddRowsM().  If we do not use this method, a scrollbar will appear to the sheet.
    myModal.Footer.AddRowsM(1,True,0,0, "").AddCellsOS(1,9,9,9,3,3,3, "")
    myModal.Footer.BuildGrid 'IMPORTANT once you loaded the complete grid AND before you start adding components
    ' add paragraph
    myModal.Content.Cell(1,1).AddComponent(ABMShared.BuildParagraph(AppPage,"par1","Please login: (login and password are 'demo')") )

    ' create the input fields for the content
    Dim inp1 As ABMInput
    inp1.Initialize(AppPage, "inp1", ABM.INPUT_TEXT, "Login", False, "")
    myModal.Content.Cell(2,1).AddComponent(inp1)

    Dim inp2 As ABMInput
    inp2.Initialize(AppPage, "inp2", ABM.INPUT_PASSWORD, "Password", False, "")
    myModal.Content.Cell(2,1).AddComponent(inp2)

    ' create the button for the footer
    Dim msbtn1 As ABMButton
    msbtn1.InitializeFlat(AppPage, "msbtn1", "", "", "Login", "transparent")
    myModal.Footer.Cell(1,1).AddComponent(msbtn1)

    inp1.SetFocus

    Return myModal
End Sub

Look at near the line Return myModal, added code : inp1.SetFocus.
Was hoping that when Login Form showed, focus already on inp1, but it didn't work, must clicked inp1 with mouse to make it focused.

Any hints ?
Thanks in advance.
 

incendio

Well-Known Member
Licensed User
Longtime User
You will probably have to set the .Setfocus after page.showmodal. The modal form does not exist yet at that point in the browser so there is nothing to set focus to.
Thanks.

Added & changed these codes on Sub WebSocket_Connected on ABMApplication module
B4X:
If ws.Session.GetAttribute2("IsAuthorized", "") = "" Then
     AppPage.ShowModalSheet("login")
     Dim mymodal As ABMModalSheet = AppPage.ModalSheet("login")
     Dim inp1 As ABMInput = mymodal.Content.Component("inp1")
     inp1.SetFocus
     Return
End If
Worked as expected.
 
Upvote 0
Top