TTMSFMXGrid - trackbar edit

In one of the TTMSFMXGrid demos, the cell string is set with a trackbar. The cell string is normally visible and the trackbar is invisible. When the cell is clicked, the trackbar appears. When the mouse releases the trackbar, the new cell value appears and the trackbar disappears.


What I should like to do, is have the trackbar permanently visible and have it update the value in the neighboring cell. Is this possible?

Hi, 


The following code sample can be used to insert a TTrackBar as a cell and use it to update another cell. You will need to declare a protected class to access a particular function but we will set this function public in the next release:

type
  TTMSFMXGridProtected = class(TTMSFMXGrid);

implementation

procedure TForm1.FormCreate(Sender: TObject);
begin
  TMSFMXGrid1.Options.Rendering.Mode := rmAddAsRealCell;
end;

procedure TForm1.TMSFMXGrid1GetCellClass(Sender: TObject; ACol, ARow: Integer;
  var CellClassType: TFmxObjectClass);
begin
  if (ACol = 1) and (ARow > 0) then
    CellClassType := TTrackBar;
end;

procedure TForm1.TMSFMXGrid1GetCellProperties(Sender: TObject; ACol,
  ARow: Integer; Cell: TFmxObject);
begin
  if (ACol = 1) and (ARow > 0) then
    (Cell as TTrackBar).OnChange := TrackBarChanged;
end;

procedure TForm1.TrackBarChanged(Sender: TObject);
var
  cl: TCell;
begin
  cl := TTMSFMXGridProtected(TMSFMXGrid1).GetCellByObject(Sender as TFMXObject);
  TMSFMXGrid1.Cells[cl.Col + 1, cl.Row] := FloatToStr((Sender as TTrackBar).Value);
end;

Kind Regards, 
Pieter

Pieter Scheldeman2015-10-16 10:27:03


Pieter,

Your code worked well (please see screenshot). Two further questions please. (1) How can I create borders around the cells containing the trackbars? (2) How can I update the trackbar positions if the user types values into the numeric cells instead of dragging the trackbars? Thank you!


To overcome the borders issue, you could return a custom cell which then creates the TTrackBar:


type
  TTrackBarCell = class(TTMSFMXGridCell)
  private
    FTrackBar: TTrackBar;
    FOnChange: TNotifyEvent;
  protected
    procedure TrackBarChanged(Sender: TObject);
  public
    constructor Create(AOwner: TComponent); override;
    property OnChange: TNotifyEvent read FOnChange write FOnChange;
    property TrackBar: TTrackBar read FTrackBar;
  end;

implementation

procedure TForm1.FormCreate(Sender: TObject);
begin
  TMSFMXGrid1.Options.Rendering.Mode := rmAddAsRealCell;
end;

{ TTrackBarCell }

constructor TTrackBarCell.Create(AOwner: TComponent);
begin
  inherited;
  FTrackBar := TTrackBar.Create(Self);
  FTrackBar.Align := TAlignLayout.Client;
  FTrackBar.Margins.Left := 3;
  FTrackBar.Margins.Top := 3;
  FTrackBar.Margins.Right := 3;
  FTrackBar.Margins.Bottom := 3;
  FTrackBar.Parent := Self;
  FTrackBar.Stored := False;
  FTrackBar.OnChange := TrackBarChanged;
end;

procedure TTrackBarCell.TrackBarChanged(Sender: TObject);
begin
  if Assigned(FOnChange) then
    FOnChange(Self);
end;

procedure TForm1.TMSFMXGrid1GetCellClass(Sender: TObject; ACol, ARow: Integer;
  var CellClassType: TFmxObjectClass);
begin
  if (ACol = 1) and (ARow > 0) then
    CellClassType := TTrackBarCell;
end;

procedure TForm1.TMSFMXGrid1GetCellProperties(Sender: TObject; ACol,
  ARow: Integer; Cell: TFmxObject);
begin
  if (ACol = 1) and (ARow > 0) then
    (Cell as TTrackBarCell).OnChange := TrackBarChanged;
end;

procedure TForm1.TrackBarChanged(Sender: TObject);
var
  cl: TCell;
begin
  cl := TMSFMXGrid1.GetCellByObject(Sender as TFMXObject);
  TMSFMXGrid1.Cells[cl.Col + 1, cl.Row] := FloatToStr((Sender as TTrackBarCell).TrackBar.Value);
end;

Thank you, I have implemented your solution to obtain the borders, but you overlooked the second part of my question. How can I update the trackbar if the user edits the numeric value in the cell to its right? I have puzzled over it for hours and can't figure out how to do it.

Hi, 


When changing a value, the OnCellEditDone is called, which contains the col and row parameter and the TMSFMXGrid1.GetCellObject can be used to retrieve the TTrackBarCell control. After retrieving the cell control, you can then use the TrackBar property to update the value by using the TMSFMXGrid1.Floats property or converting the TMSFMXGrid1.Cells property to a float.

Kind Regards, 
Pieter