B4J Question Dynamic text fields?

Tim Chapman

Active Member
Licensed User
Longtime User
I want to create text fields dynamically in code similar to what I have done in Sub CreateBillingInfoUI below.
Then I want to populate those text fields in Sub lstStores_SelectedIndexChanged.
Does anyone know a way to do this?

2 subs:
Sub CreateBillingInfoUI
    PaneBillingInfo.RemoveAllNodes
    
    Dim lblTitle As Label
    lblTitle.Initialize("")
    lblTitle.Text = "Billing Information Management"
    lblTitle.TextSize = 18
    PaneBillingInfo.AddNode(lblTitle, 20, 20, 400, 30)
    
    ' Add store list view with a title
    Dim lblStores As Label
    lblStores.Initialize("")
    lblStores.Text = "Stores"
    lblStores.TextSize = 14
    PaneBillingInfo.AddNode(lblStores, 20, 60, 200, 20)
    
    lstStores.Initialize("lstStores")
    
    ' Populate list with stores from database
    Dim rs As ResultSet = sql.ExecQuery("SELECT StoreID FROM InvoiceAddresses ORDER BY StoreID")
    Do While rs.NextRow
        lstStores.Items.Add(rs.GetString("StoreID"))
    Loop
    rs.Close
    
    PaneBillingInfo.AddNode(lstStores, 20, 100, 200, 300)
    
    'Create Billing Information Fields
    
    Dim lblBillingInfo As Label
    lblBillingInfo.Initialize("")
    lblBillingInfo.Text = "Billing Address"
    lblBillingInfo.TextSize = 14
    PaneBillingInfo.AddNode(lblBillingInfo, 240, 60, 200, 20)
    
    Dim fieldNames As List = Array( _
        "StoreID", _
        "LocationCode", _
        "BillToCompany", _
        "BillToName", _
        "BillToAddress", _
        "BillToAddress2", _
        "BillToCity", _
        "BillToState", _
        "BillToZip", _
        "BillToCountry", _
        "BillToEmail", _
        "BillToPhone", _
        "ShipToCompany")
    
    Dim y As Int = 100
    For Each fieldName As String In fieldNames
        ' Create a label for each field.
        Dim lbl As Label
        lbl.Initialize("lbl")
        lbl.Text = fieldName & ":"
        PaneBillingInfo.AddNode(lbl,240,y,150,30)
        y = y + 40
    Next
    
    Dim y As Int = 100
    For Each fieldName As String In fieldNames
        ' Create a corresponding TextField.
        Dim txt As TextField
        txt.Initialize("txt")
        txt.Tag = fieldName
        PaneBillingInfo.AddNode(txt,350,y,150,30)
        y = y + 40
    Next
    
    'Create Shipping Information Fields

    Dim lblShippingInfo As Label
    lblShippingInfo.Initialize("")
    lblShippingInfo.Text = "Shipping Address"
    lblShippingInfo.TextSize = 14
    PaneBillingInfo.AddNode(lblShippingInfo, 580, 60, 200, 20)

    Dim fieldNames As List = Array( _
        "ShipToCompany", _
        "ShipToName", _
        "ShipToAddress", _
        "ShipToAddress2", _
        "ShipToCity", _
        "ShipToState", _
        "ShipToZip", _
        "ShipToCountry", _
        "ShipToEmail", _
        "ShipToPhone" )
    
    Dim y As Int = 100
    For Each fieldName As String In fieldNames
        ' Create a label for each field.
        Dim lbl As Label
        lbl.Initialize("lbl")
        lbl.Text = fieldName & ":"
        PaneBillingInfo.AddNode(lbl,580,y,150,30)
        y = y + 40
    Next
    
    Dim y As Int = 100
    For Each fieldName As String In fieldNames
        ' Create a corresponding TextField.
        Dim txt As TextField
        txt.Initialize("txt")
        txt.Tag = fieldName
        PaneBillingInfo.AddNode(txt,690,y,150,30)
        y = y + 40
    Next
    
    ' Add buttons for actions
    Dim btnSave As Button
    btnSave.Initialize("btnSaveBilling")
    btnSave.Text = "Save Changes"
    PaneBillingInfo.AddNode(btnSave, 350, 650, 120, 30)
    
    Dim btnDelete As Button
    btnDelete.Initialize("btnDeleteBilling")
    btnDelete.Text = "Delete Store"
    PaneBillingInfo.AddNode(btnDelete, 480, 650, 120, 30)
    
    Dim btnBack As Button
    btnBack.Initialize("btnBackFromBilling")
    btnBack.Text = "Back"
    PaneBillingInfo.AddNode(btnBack, 610, 650, 80, 30)
    
End Sub

' Event handler for when a store is selected from the list
Sub lstStores_SelectedIndexChanged(Index As Int)
    LogColor("Sub lstStores_SelectedIndexChanged started", 0xFF009901)
    
    If lstStores.SelectedIndex = -1 Then Return

    ' Get the selected store ID
    Dim storeID As String = lstStores.Items.Get(Index)

    Dim rs As ResultSet = sql.ExecQuery2("SELECT * FROM InvoiceAddresses WHERE StoreID = ?", Array As Object(storeID))

    If rs.NextRow Then
    
        ' Populate the Store ID field.
        txtStoreID.Text = rs.GetString("StoreID")
        
        ' Populate Bill-To information.
        txtBillToName.Text = rs.GetString("BillToName")
        txtBillToName2.Text = rs.GetString("BillToCompany")
        txtBillToAddress.Text = rs.GetString("BillToAddress")
        txtBillToCity.Text = rs.GetString("BillToCity")
        txtBillToZip.Text = rs.GetString("BillToZip")
        txtBillToEmail.Text = rs.GetString("BillToEmail")
        txtBillToPhone.Text = rs.GetString("BillToPhone")
    
        ' Populate Ship-To information.
        txtShipToName.Text = rs.GetString("ShipToName")
        txtShipToAddress.Text = rs.GetString("ShipToAddress")
        txtShipToCity.Text = rs.GetString("ShipToCity")
        txtShipToZip.Text = rs.GetString("ShipToZip")
        txtShipToEmail.Text = rs.GetString("ShipToEmail")
        txtShipToPhone.Text = rs.GetString("ShipToPhone")
    End If

    rs.Close
    LogColor("Sub lstStores_SelectedIndexChanged finished", 0xFFFF6600)
End Sub
 
Top