[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 671
  • Last Modified:

Move Cell Values In the same StringGrid

Hi,

      I'm havin a Drag'nDrop Problem in my StringGrid. I need to "move" the value from a selected cell(s) in the stringgrid to another place in the same Stringgrid. I'm doing the following :

StringGrid1.DragMode := dmAutomatic;
StringGrid.Options.goRangeSelect := True;

procedure Form1.Stg1DragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
   If Source Is TStringGrid Then
      Accept := True;
end;

procedure Form1.Stg1DragDrop(Sender, Source: TObject; X, Y: Integer);
var
  C, R: Integer;
  k : INteger;
  GRect : TGridRect;
begin
   GRect := Stg1.Selection;
   if (Source is TStringGrid) then
   begin
      StG1.MouseToCell(X, Y, C, R);
      For R := 1 to Stg1.RowCount-1 Do
          If Stg1.Cells[C,R] = '' Then
             break;
      For k := GRect.Top to GRect.Bottom Do
      Begin
          If R > Stg1.RowCount-1 Then
             Stg1.RowCount := Stg1.RowCount + 1;
          Stg1.Cells[C,R] := Stg1.Cells[GRect.Left,k];
          Stg1.Cells[GRect.Left,k] := '';
          R := R +1;
      End;
   end;
end;

But my problem is that i cannot Select a cell with the mouse because whn i press the left button of the mouse over the StringGrid the DragOver event takes control and the application does not make that cell "selected" So i cannot make it like MS Excel.

0
jpmonja
Asked:
jpmonja
  • 2
2 Solutions
 
ewildeCommented:
general instructions:

you should inherit TStringGrid and override WM_LBUTTONDOWN,WM_LBUTTONUP and WM_MOUSEMOVE. in the mousedown-overriden-procedure check whetther the mouse position is over a selected area. if not - do inherited, if yes raise a flag. in the mousemove-overriden-procedure check your flag, if not - do inherited, if yes set mouse cursor to crDrag. in the mouseup-overriden-procedure check you flag, if not - do inherited, if yes, check whether the mouse position is over a selected area, if yes - set cursor to crDefault, if not "move" the values of the cell(s) as you described and set the cursor to crDefault


TMyStringGrid = class(TStringGrid)
  MyFlag : boolean;
  procedure WMLBUTTONDOWN(var message : TMessage); message WM_LBUTTONDOWN;
end;

implementation

procedure TMyStringGrid.WMLBUTTONDOWN(var message : TMessage);
begin
  if OverSelectedArea then MyFlag:=True
  else inherited;
end;

and so on .........


i wish you luck,
ewilde
0
 
Scay7Commented:
This isnt a drag&drop but rather a pick&place solution... :P very round but it does take a value in stringgrid1 and places it anywhere in the same stringgrid...

var
  setc : string;
  stored : string;
  copy, copysec : string;

procedure TForm1.FormCreate(Sender: TObject);
begin
setc := 'true';
end;

procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean);
label 1;
begin

if setc = 'false' then                 //paste mode
begin
    if stringgrid1.Cells[acol, arow] <> '' then
    begin
    copysec := stringgrid1.Cells[acol,arow];
    stringgrid1.Cells[acol, arow] := copy;
    copy := copysec;
    goto 1;
    end;
  stringgrid1.Cells[acol, arow] := copy;
  setc := 'true';
  statictext1.Caption := 'Currently Stored : [ NIL ]';
  goto 1;
end;


if setc = 'true' then                      //copy mode
begin
  copy := stringgrid1.Cells[acol,arow];
    if copy = '' then
    begin
    setc := 'true';
    goto 1;
    end;
  stringgrid1.Cells[acol, arow] := '';
  setc := 'false';
end;
                                      1:
end;


Peace Scay7
0
 
Scay7Commented:
Id like the accepted answer, due to the fact that ive given a
full example, which works to what the question was directed at...

A split is fine, id just like the accepted answer :P

Peace Scay7
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now