Mega Code Archive

 
Categories / Delphi / VCL
 

How to Drag & Drop items from a TListBox to a TRichEdit

Title: How to Drag & Drop items from a TListBox to a TRichEdit function RECharIndexByPos(RichEdit: TRichEdit; X, Y: Integer): Integer; var P: TPoint; begin P := Point(X, Y); Result := SendMessage(RichEdit.Handle, EM_CHARFROMPOS, 0, Longint(@P)); end; procedure TForm1.RichEdit1DragOver(Sender, Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean); var P: TPoint; begin // Check, if Source is ListBox1 Accept := Source = ListBox1; if GetCursorPos(P) then with RichEdit1 do begin // Get the Index from Mouse Position P := ScreenToClient(P); SelStart := RECharIndexByPos(RichEdit1, P.X, P.Y); SetFocus; end; end; procedure TForm1.RichEdit1DragDrop(Sender, Source: TObject; X, Y: Integer); begin // finally insert text at mouse position RichEdit1.SelText := ListBox1.Items[ListBox1.ItemIndex]; end;