Solved

MouseDown on TDBComboBox

Posted on 2001-08-01
2
528 Views
Last Modified: 2010-04-06
I want to trap the OnMouseDown on a TDBComboBox. I want to clear the box if right-mouse button is clicked on it.

There is no event for mousedown... how do I trap it?

thanks for the help.
0
Comment
Question by:espeterson
  • 2
2 Comments
 
LVL 5

Expert Comment

by:alanwhincup
Comment Utility
Try this:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, DBCtrls;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure MyDBComboBoxMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

type
  TMyDBComboBox = class(TDBComboBox)
  published
    property OnMouseDown;
  end;

var
  Form1 : TForm1;
  MyDBComboBox : TMyDBComboBox;

implementation

{$R *.DFM}

procedure TForm1.MyDBComboBoxMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if ssRight in Shift then
  begin
    ShowMessage('Right Mouse Button has been Clicked...')
    // Do whatever when right mouse button is clicked...
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  MyDBComboBox := TMyDBComboBox.Create(self);
  with MyDBComboBox do
  begin
    Parent := Form1;
    Left := 10;
    Top := 10;
    Width := 145;
    Height := 21;
    // Define any other properties...
  end;
  MyDBCombobox.OnMouseDown := MyDBComboBoxMouseDown;
end;

end.

Cheers,

Alan
0
 
LVL 5

Accepted Solution

by:
alanwhincup earned 100 total points
Comment Utility
Just realised that my above example did'nt totaly work so i redid it:

to make it work you just have to have a Popupmenu on the form and use the below code:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, DBCtrls, Menus;

type
  TForm1 = class(TForm)
    PopupMenu1: TPopupMenu;
    procedure FormCreate(Sender: TObject);
    procedure MyDBComboBoxMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure PopupMenu1Popup(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

type
  TMyDBComboBox = class(TDBComboBox)
  published
    property OnMouseDown;
  end;

var
  Form1 : TForm1;
  MyDBComboBox : TMyDBComboBox;

implementation

{$R *.DFM}

procedure TForm1.MyDBComboBoxMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if ssRight in Shift then
    MyDBComboBox.Clear;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  MyDBComboBox := TMyDBComboBox.Create(self);
  with MyDBComboBox do
  begin
    Parent := Form1;
    Left := 10;
    Top := 10;
    Width := 145;
    Height := 21;
    PopupMenu := PopupMenu1;
    MyDBComboBox.Text := 'Test';
    OnMouseDown := MyDBComboBoxMouseDown;
    // Define any other properties...
  end;
end;

procedure TForm1.PopupMenu1Popup(Sender: TObject);
begin
  MyDBComboBox.Clear;
end;

end.

If you dont want to use all this code then you can just put down a DBComboBox and a PopupMenu on a form, and then Assign the PopupMenu's name value to PopupMenu property in DBComboBoxs properties. After youve done that use the below code in the PopupMenu's Popup event:

procedure TForm1.PopupMenu1Popup(Sender: TObject);
begin
  MyDBComboBox.Clear;
end;

This will work when you right click on the editable part of the dbcombobox but not anywhee else.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now