ian-smith
asked on
Moving TStringGrid Rows
I have created a TStringGrid object, and can edit etc..
I wish to be able to move the rows up and down, as appropriate, yet it doesn't matter whether I set the goRowMoving flag in the Options property, I cannot move rows. I have created the grid at run time, and I have set options to allow editing etc, but it seems just this one command is problematical. Neither the manual nor the on-line help is helpful, so I hope you may be able tohelp.
Many thanks.
I wish to be able to move the rows up and down, as appropriate, yet it doesn't matter whether I set the goRowMoving flag in the Options property, I cannot move rows. I have created the grid at run time, and I have set options to allow editing etc, but it seems just this one command is problematical. Neither the manual nor the on-line help is helpful, so I hope you may be able tohelp.
Many thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Couldn't find an easy way to do exactly what you want, so I
had to write some "excessive" code ;-)
It does what you want (I think) and you only need to paste
it into your own code to make it work.
To be able to drag a row, press shift while you grab
any cell (nonfixed) with the mouse. Then just drag it where
you want.
I might have forgotten something, but it worked fine when I
tested it.
.
.
.
private
.
.
.
MovingRow : Boolean;
LastRow : Integer;
procedure SMD(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure SMM(Sender: TObject;
Shift: TShiftState; X, Y: Integer);
procedure SMU(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure DrawMove(Sender: TObject;
MoveIndex, MovePos: Integer);
end;
.
.
.
procedure TMainForm.SMD(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
with (Sender as TStringGrid) do begin
{ Select shiftstate sensibility below }
if (ssShift in Shift) and (Row>=FixedRows) then begin
Update;
DrawMove(Sender, Row, Row);
MovingRow := True;
LastRow := Row;
end;
try
inherited MouseDown(Button, Shift, X, Y);
except
if MovingRow then DrawMove(Sender, Row, Row);
MovingRow := False;
end;
end;
end;
procedure TMainForm.SMM(Sender: TObject;
Shift: TShiftState; X, Y: Integer);
var
ACol, ARow: LongInt;
begin
if MovingRow then with (Sender as TStringGrid) do begin
MouseToCell(X, Y, ACol, ARow);
if (LastRow<>ARow) and (ARow>=FixedRows) then begin
DrawMove(Sender, Row, LastRow);
DrawMove(Sender, Row, ARow);
LastRow := ARow;
end;
end;
end;
procedure TMainForm.SMU(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
ACol: Integer;
begin
if MovingRow then
with (Sender as TStringGrid) do begin
DrawMove(Sender, Row, LastRow);
for ACol := 0 to ColCount-1 do begin
Cols[ACol].ExChange(Row, LastRow);
end;
Row := LastRow;
end;
MovingRow := False;
end;
procedure TMainForm.DrawMove(Sender: TObject;
MoveIndex, MovePos: Integer);
var
OldPen: TPen;
Pos : Integer;
R : TRect;
begin
with (Sender as TStringGrid) do begin
OldPen := TPen.Create;
try
with Canvas do
begin
OldPen.Assign(Pen);
try
Pen.Style := psDot;
Pen.Mode := pmXor;
Pen.Width := 5;
R := CellRect(0, MovePos);
if MovePos > MoveIndex then
Pos := R.Bottom else
Pos := R.Top;
MoveTo(0, Pos);
LineTo(ClientWidth, Pos);
finally
Canvas.Pen := OldPen;
end;
end;
finally
OldPen.Free;
end;
end;
end;
procedure TMainForm.FormCreate(Sende r: TObject);
.
.
.
with TStringGrid.Create(Mainfor m) do begin
Name := 'StringGrid1';
Parent := Mainform;
Left := 100;
Top := 300;
Width := 300;
Height := 200;
FixedCols := 0;
OnMouseDown := SMD;
OnMouseMove := SMM;
OnMouseUp := SMU;
Options := Options + [goEditing,goRowMoving];
end;
.
.
.
end;
Enjoy!
/// John
had to write some "excessive" code ;-)
It does what you want (I think) and you only need to paste
it into your own code to make it work.
To be able to drag a row, press shift while you grab
any cell (nonfixed) with the mouse. Then just drag it where
you want.
I might have forgotten something, but it worked fine when I
tested it.
.
.
.
private
.
.
.
MovingRow : Boolean;
LastRow : Integer;
procedure SMD(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure SMM(Sender: TObject;
Shift: TShiftState; X, Y: Integer);
procedure SMU(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure DrawMove(Sender: TObject;
MoveIndex, MovePos: Integer);
end;
.
.
.
procedure TMainForm.SMD(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
with (Sender as TStringGrid) do begin
{ Select shiftstate sensibility below }
if (ssShift in Shift) and (Row>=FixedRows) then begin
Update;
DrawMove(Sender, Row, Row);
MovingRow := True;
LastRow := Row;
end;
try
inherited MouseDown(Button, Shift, X, Y);
except
if MovingRow then DrawMove(Sender, Row, Row);
MovingRow := False;
end;
end;
end;
procedure TMainForm.SMM(Sender: TObject;
Shift: TShiftState; X, Y: Integer);
var
ACol, ARow: LongInt;
begin
if MovingRow then with (Sender as TStringGrid) do begin
MouseToCell(X, Y, ACol, ARow);
if (LastRow<>ARow) and (ARow>=FixedRows) then begin
DrawMove(Sender, Row, LastRow);
DrawMove(Sender, Row, ARow);
LastRow := ARow;
end;
end;
end;
procedure TMainForm.SMU(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
ACol: Integer;
begin
if MovingRow then
with (Sender as TStringGrid) do begin
DrawMove(Sender, Row, LastRow);
for ACol := 0 to ColCount-1 do begin
Cols[ACol].ExChange(Row, LastRow);
end;
Row := LastRow;
end;
MovingRow := False;
end;
procedure TMainForm.DrawMove(Sender:
MoveIndex, MovePos: Integer);
var
OldPen: TPen;
Pos : Integer;
R : TRect;
begin
with (Sender as TStringGrid) do begin
OldPen := TPen.Create;
try
with Canvas do
begin
OldPen.Assign(Pen);
try
Pen.Style := psDot;
Pen.Mode := pmXor;
Pen.Width := 5;
R := CellRect(0, MovePos);
if MovePos > MoveIndex then
Pos := R.Bottom else
Pos := R.Top;
MoveTo(0, Pos);
LineTo(ClientWidth, Pos);
finally
Canvas.Pen := OldPen;
end;
end;
finally
OldPen.Free;
end;
end;
end;
procedure TMainForm.FormCreate(Sende
.
.
.
with TStringGrid.Create(Mainfor
Name := 'StringGrid1';
Parent := Mainform;
Left := 100;
Top := 300;
Width := 300;
Height := 200;
FixedCols := 0;
OnMouseDown := SMD;
OnMouseMove := SMM;
OnMouseUp := SMU;
Options := Options + [goEditing,goRowMoving];
end;
.
.
.
end;
Enjoy!
/// John
ASKER
If there is a way to move the rows without having a fixed column and without excessive programming, I will be most happy.
Thanks for your help.