A table with two columns, how to fit the colums in the table?

IoSonoPiero

Active Member
Licensed User
Longtime User
Hello,
I have a table that has two columns.
When I fill the table, I want that the second column will be resized to stay in the table width.

There are two cases:
  1. The rows are a lot so the table shows a vertical scrollbar
  2. The rows are few so I don't need the scrollbar
How can I manage this?
I've tried to do this:
B4X:
[LEFT][FONT=Verdana][SIZE=2][COLOR=#010101][COLOR=#010101][FONT=Times New Roman]Table1.ColWidth("Col1")  = 100
[/FONT][/COLOR][/COLOR][/SIZE][/FONT][FONT=Verdana][SIZE=2][COLOR=#010101][COLOR=#010101][FONT=Times New Roman]Table1.ColWidth("Col2")  = Table1.Width - [/FONT][/COLOR][/COLOR][/SIZE][/FONT][FONT=Verdana][SIZE=2][COLOR=#010101][COLOR=#010101][FONT=Times New Roman]Table1.ColWidth("Col1")[/FONT][/COLOR][/COLOR][/SIZE][/FONT][/LEFT]
but of course this is not the correct way to accomplish this.

I've read this in the forum, but I'm not sure if this will be a solution for me.

Someone can help me, please?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use the Door library to find the vertical scroll bar and horizontal scroll bar status.
obj1 and obj2 are Objects from the Door library:
B4X:
Sub GetScrollBarsProperties (tableName)
    obj1.FromControl(tableName)
    obj1.Value = obj1.GetProperty("Controls")
    obj2.Value = obj1.GetProperty2("Item", 0)
    HScrollVisible = obj2.GetProperty("Visible") 'Horizontal scroll bar visible (true or false)
    HScrollHeight = obj2.GetProperty("Height") 'Horizontal scroll height
    obj2.Value = obj1.GetProperty2("Item", 1)
    VScrollVisible = obj2.GetProperty("Visible") 'Vertical scroll bar visible
    VScrollHeight = obj2.GetProperty("Width") 'Vertical scroll bar width
End Sub
 

IoSonoPiero

Active Member
Licensed User
Longtime User
I've put on Sub Globals of a module
B4X:
obj1.New1(False)
obj2.New1(False)

Then,
I've put
B4X:
Sub GetScrollBarsProperties (tableName)
    obj1.FromControl(tableName)
    obj1.Value = obj1.GetProperty("Controls")
    obj2.Value = obj1.GetProperty2("Item", 0)
    HScrollVisible = obj2.GetProperty("Visible") 'Horizontal scroll bar visible (true or false)
    HScrollHeight = obj2.GetProperty("Height") 'Horizontal scroll height
    obj2.Value = obj1.GetProperty2("Item", 1)
    VScrollVisible = obj2.GetProperty("Visible") 'Vertical scroll bar visible
    VScrollHeight = obj2.GetProperty("Width") 'Vertical scroll bar width
    Msgbox(HScrollVisible & "." & HScrollHeight & "." & VScrollVisible & "." & VScrollHeight)
End Sub

So, when I call
B4X:
GetScrollBarsProperties(tbEditor)

I receive this error:
B4X:
An error occurred on sub editor.frmeditor_show

Line number: XXX

GetScrollBarsProperties(tbEditor)
Error description:
Variable: editor.tbeditor was not assigned any value.
Continue?
editor is the module name
tbEditor is the table name

What I can do???
 

IoSonoPiero

Active Member
Licensed User
Longtime User
All right, all right, I have to pass the table name in double quotes!

B4X:
GetScrollBarsProperties("tbEditor")

So all works!

Thank you very much, Erel!
 

IoSonoPiero

Active Member
Licensed User
Longtime User
Mmmhh,
I always receive False in Horizontal and Vertical scroll bar visible (True or False)

It's correct???
I have scrollbars!
 

IoSonoPiero

Active Member
Licensed User
Longtime User
:sign0161:

I have to pass True in the constructor for Door object, according to documentation:
New1
InitializeDataGrid should be set to true if you are using the object with a Table control.
 

IoSonoPiero

Active Member
Licensed User
Longtime User
Ok, there a strange "issue"

The table width is 240
The first column width is 192
The second colum width is 48

So: 192 + 48 = 240, but...

I have to subtract 4 to to first or second colums to avoid horizontal scrollbar!

Why 4?

Maybe I have to consider:
  • 1 pixel border from left side of first column
  • 1 pixel border from rigth side of first column
  • 1 pixel border from left side of second column
  • 1 pixel border from rigth side of second column
??
 

IoSonoPiero

Active Member
Licensed User
Longtime User
I've edited this post to make it clear the problem:

I have a 240 width table,
I want two colums.

I have only the second column width fixed,
the first colums must adapt his width dinamically if vertical scrollbar is visible or not.

I use this code:
B4X:
Public Sub FixScrollBars
obj1.FromControl("tbEditor")
    obj1.Value = obj1.GetProperty("Controls")
    
    obj2.Value = obj1.GetProperty2("Item", 0)
    'HScrollVisible = obj2.GetProperty("Visible") 'Horizontal scroll bar visible (True o False)
    'HScrollHeight = obj2.GetProperty("Height") 'Horizontal scroll height
    
    obj2.Value = obj1.GetProperty2("Item", 1)
    VScrollVisible = obj2.GetProperty("Visible") 'Vertical scroll bar visible
    VScrollWidth = obj2.GetProperty("Width") 'Vertical scroll bar width

    If VScrollVisible = True Then
        tbEditor.ColWidth("Word") = tbEditor.Width - priorityColumnWidth - VScrollWidth
    Else
        tbEditor.ColWidth("Word") = tbEditor.Width - priorityColumnWidth
    End If
End SUb
To calculate first column width, I do:

table.width - second_column.widht - Vscrollwidth if scrollbar is visible
table.width - second_column.widht if scrollbar is NOT visible

In both cases, I've seen that the first column has ALWAYS 192 as width dimension.
It seems that tbEditor.ColWidth("Word") assignment is not considered!

Info: I call FixScrollBars function from another module (the Main module), after calling the .Show method for the form that contains the table.
I do this because If I call FixScrollBars in the Show method of the form that has the table, I've noticed that the scrollbars are not detected, infact VScrollVisible is always False!

Someone has a solution?
 
Last edited:

IoSonoPiero

Active Member
Licensed User
Longtime User
Ok agraham, thanks.
I've thinked that the pixels was added for something about borders of the table.

But I've seen that if I add rows to table and dinamically calculate the first column width and set it in the frm_Show method, when the form is shown on the screen, the column width I set is ignored!
This happen only if there is a vertical scrollbar.

Someone can reproduce this issue?
 

IoSonoPiero

Active Member
Licensed User
Longtime User
I've attached an example.

You'll noticed that the condition:
B4X:
If VScrollVisible = True Then
will never be true because of the table is not showed on the screen (or at least, this is what I think).

How I can determine if VScrollVisible = True ?

So for now I've used a ForceFix sub,
to assign a forced value to column according to row number contained in the table.

I've added a button. If I press it when the form is visible, the columns are correctly calculated and set.
 
Last edited:

agraham

Expert
Licensed User
Longtime User
Firstly this fails when optimised compiled because of a bug - see http://www.b4x.com/forum/bug-reports/4645-comparison-fails-when-optimised-compiled.html. A workaround is

If VScrollVisible = True OR VScrollVisible = "True" Then


I think the main "problem" is probably that the visibility of the scroll bars is decided when the Listbox is painted and you are trying to determine this in the Forms' first Show event at which time I don't think the Listbox has been painted yet and so does not know whether it needs scroll bars or not.

You can, rather inelegantly, use a one shot Timer to get around this. The Timer code only runs when the Form and its controls are all finished painting and the flow of execution exits App_Start and starts to process messages in the Forms' message loop.
B4X:
Sub App_Start
   frmMain.Show
   Timer1.Interval = 1
   Timer1.Enabled = True
End Sub

Sub Timer1_Tick
   Timer1.Enabled = False
   FixScrollBars("tbEditor")
End Sub
 

IoSonoPiero

Active Member
Licensed User
Longtime User
Hello agraham, yesterday night I've solved the issue in the way you write here!

Thanks for your help,
bye,
Pierluigi
 

IoSonoPiero

Active Member
Licensed User
Longtime User
I've seen that compiling with Device (Auto Scale) Exe,
the Vertical Scrollbar Width is 13 on QVGA and 26 on VGA.

So, using the dynamic resize of column width, on VGA device my first column not fit exactly the space, so the second column has a 13 pixels distance from the vertical scrollbar!

If I (only as a test) to the FixScrollbars add 13 to the calculated width, the two columns have precise width.

There is a way to avoid this? :sign0085:
 

IoSonoPiero

Active Member
Licensed User
Longtime User
Thank you very much, agraham!
I've used ScreenScaleY to dinamically calculate positions.
Thanks.
 
Top