Recent

Author Topic: StringGrid with one fixed column and one non-moveable, editable column  (Read 1946 times)

Craig

  • Newbie
  • Posts: 2
Hi All,

I am trying to create a table with one fixed column (i.e. FixedCols:=1) in position 0, one drop-down column in position 1, and several "data" columns after that. I would like the "Data" columns to be movable (i.e. goColMoving := true) but do not want the drop-down column to be moved from position 1.

I have tried to catch when column 1 is moved as such:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.StringGrid1ColRowMoved(Sender: TObject; IsColumn: Boolean;
  2.                 sIndex, tIndex: Integer);
  3. begin
  4.    if (sIndex=1) or (tIndex=1) then
  5.       begin
  6.          //ShowMessage('sIndex: '+IntToStr(sIndex)+'    tIndex: '+IntToStr(tIndex));
  7.          if not HasSwapped then
  8.             begin
  9.                HasSwapped:=true;
  10.                StringGrid1.MoveColRow(IsColumn,sIndex,tIndex);
  11.             end
  12.          else
  13.             HasSwapped:=false;
  14.       end;
  15. end;
  16.  

The StringGrid1.MoveColRow triggered the event again (causing an infinite loop) hence the requirement for the "HasSwapped" flag.
The above code does not work, I believe because the event is triggered before the columns are swapped.

Does anyone have any suggestions for me, or is this beyond the capability of s StringGrid?

Thanks!
Craig

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
@ Does anyone have any suggestions for me, or is this beyond the capability of s StringGrid?

IMO beyond the capability. Maybe workaround would be to place two buttons by the grid with arrows <- and ->. Those buttons will be enabled only when column >=2 is selected and will move the column to the left/right by one.

EDIT: Or movable columns will have popup menu with items for moving, just like tabs of Lazarus source editor.
« Last Edit: May 21, 2018, 04:20:33 pm by Blaazen »
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

wp

  • Hero Member
  • *****
  • Posts: 11830
This is a bit "hacky" but seems to work: Simply turn off goColMoving if the mouse is pressed or moved above the header of column 1, and call Abort to shut down the current dragging action

Code: Pascal  [Select][+][-]
  1. procedure TForm1.StringGrid1MouseMove(Sender: TObject; Shift: TShiftState; X,
  2.   Y: Integer);
  3. var
  4.   cell: TPoint;
  5. begin
  6.   cell := StringGrid1.MouseToCell(Point(X, Y));
  7.   if (cell.Y = 0) and (cell.X = 1) then begin
  8.     StringGrid1.Options := StringGrid1.Options - [goColMoving];
  9.     Abort;
  10.   end else
  11.     StringGrid1.Options := StringGrid1.Options + [goColMoving];
  12. end;
This is to be hooked into the OnMouseDown and OnMouseMove events of the grid.

Craig

  • Newbie
  • Posts: 2
Thank you both for your help.

Or movable columns will have popup menu with items for moving, just like tabs of Lazarus source editor.
Blaazen, I was just about to implement this suggestion  :)

This is a bit "hacky" but seems to work: Simply turn off goColMoving if the mouse is pressed or moved above the header of column 1, and call Abort to shut down the current dragging action

WP, thank you for the brilliant suggestion. It seems to work very well. I would never have thought of trying that.  :D

 

TinyPortal © 2005-2018