Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Dragging interferes with OnClick

Posted on 2006-06-19
2
Medium Priority
?
333 Views
Last Modified: 2010-04-05
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
Comment
Question by:JackKuti
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 26

Accepted Solution

by:
Russell Libby earned 500 total points
ID: 16936859

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
 

Author Comment

by:JackKuti
ID: 16937089
Yes, this question is a part of the previous problem. Thanks again!
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question