Mega Code Archive

 
Categories / Delphi / VCL
 

Drag Drop from Stringgrid to Listbox

Title: Drag Drop from Stringgrid to Listbox Question: How to drag a whole column from a StringGrid to a Listbox. Answer: Drop a Listbox and a StringGrid on the Form. Set the dragmode property of the StringGrid to dmAutomatic in the object inspector. Listbox Events : OnDragOver - Accept:=Source is TStringGrid; This so the Listbox would accept only data from a TStringGrid. OnDragDrop - procedure TForm1.ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer); var i : integer; ColumVal,CurrentCol : Integer; begin if Source is TStringGrid then begin //Calculate in which column we are. ColumVal:=0; CurrentCol:=0; for i:=0 to TStringGrid(Source).ColCount-1 do begin ColumVal:=ColumVal+TStringGrid(Source).ColWidths[i]; if XMouseCord begin CurrentCol:=i; break; end; end; //Make sure we are not in the first column which has no data if CurrentCol0 then begin for i:=1 to TStringGrid(Source).RowCount-1 do begin ListBox1.items.Add(TStringGrid(Source).Cells[CurrentCol,i]); end; end; StartDrag:=True; end; end; First we see if the source is from a StringGrid. Then we need to calculate from which column on the Stringgrid it comes from using the mouse coordinate. (This take in account that every column is a different size.) If the column isn't the first one ,which usually have no data, then we put the whole column, one row at a time into the Listbox. StringGrid Events : OnDragOver - if StartDrag then begin XMouseCord := X; StartDrag:=False; end; TheStartDrag is a Boolean to stop the event from recording every mouse x coordinate after dragging started. OnDragOver - StartDrag:=True; This is so in the case we drop it on the StringGrid and not the Listbox. Here is the entire source for it to work. type TForm1 = class(TForm) StringGrid1: TStringGrid; ListBox1: TListBox; procedure ListBox1DragOver(Sender, Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean); procedure ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer); procedure FormCreate(Sender: TObject); procedure StringGrid1DragOver(Sender, Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean); procedure StringGrid1DragDrop(Sender, Source: TObject; X, Y: Integer); private { Private declarations } XMouseCord : Integer; StartDrag : Boolean; public { Public declarations } end; var Form1: TForm1; implementation {$R *.DFM} procedure TForm1.ListBox1DragOver(Sender, Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean); begin Accept:=Source is TStringGrid; end; procedure TForm1.ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer); var i : integer; ColumVal,CurrentCol : Integer; begin if Source is TStringGrid then begin //Calculate in which column we are. ColumVal:=0; CurrentCol:=0; for i:=0 to TStringGrid(Source).ColCount-1 do begin ColumVal:=ColumVal+TStringGrid(Source).ColWidths[i]; if XMouseCord begin CurrentCol:=i; break; end; end; //Make sure we are not in the first column which has no data if CurrentCol0 then begin for i:=1 to TStringGrid(Source).RowCount-1 do begin ListBox1.items.Add(TStringGrid(Source).Cells[CurrentCol,i]); end; end; StartDrag:=True; end; end; procedure TForm1.FormCreate(Sender: TObject); begin //For Demo purpose with StringGrid1 do begin Cells[1,1]:='T1'; Cells[1,2]:='T2'; Cells[1,3]:='T3'; Cells[1,4]:='T4'; Cells[2,1]:='T5'; Cells[2,2]:='T6'; Cells[2,3]:='T7'; Cells[2,4]:='T8'; Cells[3,1]:='T9'; Cells[3,2]:='T10'; Cells[3,3]:='T11'; Cells[3,4]:='T12'; Cells[4,1]:='T13'; Cells[4,2]:='T14'; Cells[4,3]:='T15'; Cells[4,4]:='T16'; end; StartDrag:=True; end; procedure TForm1.StringGrid1DragOver(Sender, Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean); begin //Store which column we are on when the dragging started. if StartDrag then begin XMouseCord := X; StartDrag:=False; end; end; procedure TForm1.StringGrid1DragDrop(Sender, Source: TObject; X, Y: Integer); begin //Just in case we drop it on the grid StartDrag:=True; end; This was my first tutorial, so I hope it helps someone.