Go Premium for a chance to win a PS4. Enter to Win

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

Dragging interferes with OnClick

I have ListBox where I set in OnMouseDown event  a BeginDrag procedure.  There is also some procedure in OnClick event. But this OnClick procedure does´nt work. Its caused by BeginDrag. How can I make OnClick work?
0
JackKuti
Asked:
JackKuti
1 Solution
 
Russell LibbySoftware Engineer, Advisory Commented:

I am guessing this is a continuation from your last question... If so, then you will need to perform the mouse tracking manually (regardless of using manual or automatic drag mode). The example below demonstrates this by setting a limit of 10 pixels either N/S/E/W of when the mouse down occurred. If the mouse is then moved outside the limits (while captured), then BeginDrag will occur. This allows you to handle the click event while still allowing for dragging items out of the list box.

Regards,
Russell

---

type
  TForm1            =  class(TForm)
     Memo1:         TMemo;
     ListBox1:      TListBox;
     procedure      FormCreate(Sender: TObject);
     procedure      FormDestroy(Sender: TObject);
     procedure      ListBox1StartDrag(Sender: TObject; var DragObject: TDragObject);
     procedure      Memo1DragOver(Sender, Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean);
     procedure      Memo1DragDrop(Sender, Source: TObject; X, Y: Integer);
     procedure      ListBox1Click(Sender: TObject);
     procedure      ListBox1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
     procedure      ListBox1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
     procedure      ListBox1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  private
     //  Private declarations
     FDragObject:   TTextDrag;
     FCaptured:     Boolean;
     FCapture:      TPoint;
  public
     // Public declarations
  end;

var
  Form1: TForm1;

implementation
{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
var  dwIndex:       Integer;
begin

  // Set captured state
  FCaptured:=False;

  // Create drag object
  FDragObject:=TTextDrag.Create;

  // Update form's control style
  Self.ControlStyle:=Self.ControlStyle + [csDisplayDragImage];

  // Update control style for all controls
  for dwIndex:=0 to Pred(ControlCount) do Controls[dwIndex].ControlStyle:=Controls[dwIndex].ControlStyle + [csDisplayDragImage];

  // Lock listbox
  ListBox1.Items.BeginUpdate;

  // Resource protection
  try
     // Clear list
     ListBox1.Items.Clear;
     // Add font names to list
     for dwIndex:=0 to Pred(Screen.Fonts.Count) do ListBox1.Items.Add(Screen.Fonts[dwIndex]);
  finally
     // Unlock listbox
     ListBox1.Items.EndUpdate;
  end;

end;

procedure TForm1.FormDestroy(Sender: TObject);
begin

  // Free the drag object
  FDragObject.Free;

end;

procedure TForm1.ListBox1StartDrag(Sender: TObject; var DragObject: TDragObject);
var  fntDrag:    TFont;
begin

  // Create font to duplicate for dragging
  fntDrag:=TFont.Create;

  // Resource protection
  try
     // Update from control first
     fntDrag.Assign(ListBox1.Font);
     // Update the font name
     fntDrag.Name:=ListBox1.Items[ListBox1.ItemIndex];
     // Update the drag object
     FDragObject.UpdateDragImage(fntDrag, fntDrag.Name);
     // Set the drag object to use
     DragObject:=FDragObject;
  finally
     // Free the temp font
     fntDrag.Free;
  end;

end;

procedure TForm1.Memo1DragOver(Sender, Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean);
begin

  // Accept the drag
  Accept:=True;
 
end;

procedure TForm1.Memo1DragDrop(Sender, Source: TObject; X, Y: Integer);
begin

  // Check drop source
  if (Source is TTextDrag) then Memo1.Font.Name:=TTextDrag(Source).Text;

end;

procedure TForm1.ListBox1Click(Sender: TObject);
begin

  if (ListBox1.ItemIndex < 0) then
     Caption:=EmptyStr
  else
     Caption:=ListBox1.Items[ListBox1.ItemIndex];

end;

procedure TForm1.ListBox1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin

  FCaptured:=True;
  FCapture:=Point(X, Y);

end;

procedure TForm1.ListBox1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
const   Limit = 10;
begin

  if FCaptured and (ListBox1.ItemIndex >= 0) then
  begin
     if (X <= (FCapture.X - Limit)) or (X >= (FCapture.X + Limit)) or (Y <= (FCapture.Y - Limit)) or (Y >= (FCapture.Y + Limit)) then
     begin
        FCaptured:=False;
        ListBox1.BeginDrag(True);
     end;
  end;

end;

procedure TForm1.ListBox1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin

  FCaptured:=False;

end;
0
 
JackKutiAuthor Commented:
Yes, this question is a part of the previous problem. Thanks again!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

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