FMX ResponsiveFiremonkeyStringGrid

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,199
Credits
4,009
ResponsiveFiremonkeyStringGrid

How to make a string grid in a Firemonkey desktop application "responsive" when using Livebindings

Making the column widths of a Firemonkey string grid requires only a few lines of code, although the exact steps to achieve this are not well-documented as far as I was able to find. This is a demo of the programming needed to achieve "responsive" column widths in a FireMonkey desktop application that uses LiveBindings.

1626716874728.png

The code is straightforward, but not limiting. In the demo, the column widths are adjusted and minimim widths and maximum widths are applied. Additional kinds of adjustments can also be applied depending on the needs of the individual application and inventiveness of the developer.

Для просмотра ссылки Войди или Зарегистрируйся
 

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,199
Credits
4,009
FireMonkey String Grid Responsive Columns
Monday, July 19, 2021 by Milan Vydareny

Overview​

I have a FireMonkey application with the following characteristics:
  • The form layout is generally responsive to changes in size
  • The form contains a string grid; I wanted the column widths to also behave in a responsive manner, e.g. change size predictably when the grid is resized
  • LiveBindings are used to populate the UI fields
1626716993230.png

Sample Project​

There is a sample project for this blog post available for download at Для просмотра ссылки Войди или Зарегистрируйся

Note that the sample project is quite simple but the code for string grid column resizing is heavily commented. The sample code with comments is the best source of how the Responsive Columns were implemented.

General Form Layout​

Ways to make a FireMonkey form responsive are generally well-known. In this case, my standard practice is to use one or more TGridPanelLayout components to divide the form into the desired regions. Inside each cell of the TGridPanelLayout I placed a TLayout to enable grouping several visual components in a single TGridPanelLayout cell. In one case I nested another TGridPanelLayout in the target cell to permit further responsive behavior.

The TStringGrid is simply handled as a child of one of the TLayouts.

At this point the form will behave in a responsive manner, observing the various specifications for absolute or percentage sizes as the form itself is resized. What remains is the handling of the column resizing, which cannot be specified directly.

Calculating Column Widths​

The routine CalcColumnWidths in the main form does the actual work of calculating and specifying the column widths for display. It uses a two-dimensional array of constants that specifies the parameters for each column:
  • Minimum column width
  • Maximum column width
  • Proportion of total space available in the string grid for each column
The column widths are set as follows:
  • The calculation starts by summing all of the proportion values for the column array. This sum is used as the denominator for individual column calculations.
  • Next, each column is handled by first calculating the proportional value of the available space using the column's proportion specification as the numerator and the previously calculated denominator.
  • Finally, the calculated value for the column is constrained by the specified minimum and maximum values for the column.
The value finally determined is used to set the column width.

Invoking the Column Width Calculation​

Column width calculation is invoked in two places:
  • By the OnResized event of the string grid itself.
  • By the OnActivated event of the LiveBinding for the String Grid. (This insures that the columns are correctly set immediately after the dataset is activated.)

Conclusion​

This solution requires a bit of code but seems to be flexible, reliable and relatively easy to implement. Since the developer provides the code (and hence determines the algorithm used for sizing) it can easily be modified to suit any need or preference.

Thanks for reading. As always, comments welcomed.