B4J Question Tableview - save & set column widths for next session

Mark Stuart

Well-Known Member
Licensed User
Longtime User
Howdy folks,

I would like to allow the user to adjust the Tableview column widths as they want.
When the app closes, save the column widths to the KeyValueStore (kvs).
In AppStart, restore the column widths and set them from the stored values.

I'm able to write and read the column widths correctly, but after the MainForm.Show, the columns are at default width again.
What do I need to do to get this to work?

Restore and Save calls:
AppStart:
Utils.RestoreTableWidths("Customers",tvCustomers)

MainForm_CloseRequest:
Utils.SaveTableWidths("Customers",tvCustomers)

The Utils functions to Restore and Save:

B4X:
Sub RestoreTableWidths(Key As String, tv As TableView)
    If kvs.ContainsKey(Key) = False Then Return

    Dim lst As List = kvs.Get(Key)

    For i = 0 To lst.Size - 1
        Dim w As Double = lst.Get(i)
        tv.SetColumnWidth(i,w)
    Next
End Sub

B4X:
Sub SaveTableWidths(Key As String, tv As TableView)
    Dim lst As List
    lst.Initialize
    
    For i = 0 To tv.ColumnsCount - 1
        Dim colWidth As Double = tv.GetColumnWidth(i)
        lst.Add(colWidth)
    Next

    kvs.Put(Key, lst)
End Sub
 

Mark Stuart

Well-Known Member
Licensed User
Longtime User
I use this now. Works as long as the TableView is resized, which means not resizing the columns but resizing the form.
Again, works if the TableView is anchored.
B4X:
'Call to save the column widths:
Private Sub tvLicenses_Resize(Width As Double, Height As Double)
    Save_Column_Widths
End Sub

Sub Save_Column_Widths
    Dim colsizes As List
    colsizes.Initialize
    For i = 0 To tvLicenses.ColumnsCount - 1
        Dim s As Double = tvLicenses.GetColumnWidth(i)
        colsizes.Add(s)
    Next
    File.WriteList(File.DirApp, "colsizes.txt", colsizes)
End Sub

'Call to set the column widths:
Public Sub Show
    Set_Column_Widths
End Sub

Sub Set_Column_Widths
    If File.Exists(File.DirApp, "colsizes.txt") Then
        Dim colsizes As List
        colsizes.Initialize
        colsizes = File.ReadList(File.DirApp, "colsizes.txt")
        For i = 0 To colsizes.Size - 1
            Dim colw As Double = colsizes.Get(i)
            tvLicenses.SetColumnWidth(i,colw)
        Next
    End If
End Sub
 
Upvote 0
Top