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.

jpmonjaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.