Solved

How can I make a ComboBox change selection on double-click?

Posted on 2001-07-01
3
421 Views
Last Modified: 2010-04-06
Hi,

I have a Combobox with Style=DropDownList.  I would like to make it so that the user can double-click on the combobox and have it change selection.  The OnDblClick event does not fire if the Style=DropDownList

How can I do this?

It's nice to be able to double-click a combobox and have it toggle the selection.

Thanks!
MP
0
Comment
Question by:mikepj
[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
3 Comments
 
LVL 2

Accepted Solution

by:
bugroger earned 150 total points
ID: 6243150
Hi,

Try this TComboBox component.
It's like the normal TComboBox but
the onDlbClick event also will be fired if the
 "Style" is set to "csDropDownList".

GL
 Bug

unit CBDblClick;



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

TYPE
  TComboBoxDblClick = class(TCustomComboBox)
  Private
  protected
    procedure WndProc(var Message: TMessage); override;
    function  MouseOnBtn : Boolean;
  published
    property Style; {Must be published before Items}
    property Anchors;
    property BiDiMode;
    property Color;
    property Constraints;
    property Ctl3D;
    property DragCursor;
    property DragKind;
    property DragMode;
    property DropDownCount;
    property Enabled;
    property Font;
    property ImeMode;
    property ImeName;
    property ItemHeight;
    property MaxLength;
    property ParentBiDiMode;
    property ParentColor;
    property ParentCtl3D;
    property ParentFont;
    property ParentShowHint;
    property PopupMenu;
    property ShowHint;
    property Sorted;
    property TabOrder;
    property TabStop;
    property Text;
    property Visible;
    property OnChange;
    property OnClick;
    property OnContextPopup;
    property OnDblClick;
    property OnDragDrop;
    property OnDragOver;
    property OnDrawItem;
    property OnDropDown;
    property OnEndDock;
    property OnEndDrag;
    property OnEnter;
    property OnExit;
    property OnKeyDown;
    property OnKeyPress;
    property OnKeyUp;
    property OnMeasureItem;
    property OnStartDock;
    property OnStartDrag;
    property Items; { Must be published after OnMeasureItem }
  end;
  Procedure Register;
implementation

Procedure Register;
Begin
 RegisterComponents('Eigene', [TComboBoxDblClick]);
End;

Function  TComboBoxDblClick.MouseOnBtn : Boolean;
Var
 mx, my : Integer;
 cp     : TPoint;

Begin
 Result := FALSE;

 mx := Mouse.CursorPos.x;
 my := Mouse.CursorPos.y;

 cp.x := Self.Left;
 cp.y := self.Top;
 cp := Parent.ClientToScreen(cp);

 cp.x := cp.x + (Self.Width - 18);
 cp.y := cp.y +2;

 IF ( (my >= cp.y) and (my <= cp.y + (Self.ClientHeight -5) ) )AND
    ( (mx >= cp.x) and (mx <= cp.x + 15) )THEN Result := TRUE;
End;

procedure TComboBoxDblClick.WndProc(var Message: TMessage);
Var
 s : string;
begin
  with Message do
    case Msg of
      WM_SIZE:
        { Prevent TWinControl from handling WM_SIZE when adjusting drop-down
          listbox size. }
        if DroppedDown then
        begin
          DefaultHandler(Message);
          Exit;
        end;
      WM_CTLCOLORMSGBOX..WM_CTLCOLORSTATIC:
        begin
          SetTextColor(WParam, ColorToRGB(Font.Color));
          SetBkColor(WParam, ColorToRGB(Brush.Color));
          Result := Brush.Handle;
          Exit;
        end;
      CN_CTLCOLORMSGBOX..CN_CTLCOLORSTATIC:
        if not NewStyleControls and (Style < csDropDownList) then
        begin
          Result := Parent.Brush.Handle;
          Exit;
        end;
      WM_CHAR:
        begin
          if DoKeyPress(TWMKey(Message)) then Exit;
          if ((TWMKey(Message).CharCode = VK_RETURN) or
            (TWMKey(Message).CharCode = VK_ESCAPE)) and DroppedDown then
          begin
            DroppedDown := False;
            Exit;
          end;
        end;
    end;
 if not (csDesigning in ComponentState) then
 Begin
  IF (Message.Msg = WM_LBUTTONDBLCLK)and (not MouseOnBtn) then Self.OnDblClick(Owner);
  IF ((Message.Msg <> WM_LBUTTONDOWN) and (Message.Msg <> WM_LBUTTONDBLCLK))or(MouseOnBtn) then
   inherited WndProc(Message);
 end else inherited WndProc(Message);
end;

end.
0
 

Expert Comment

by:d32coder
ID: 6243210
Sounds like you only have 2 options in the list, if so, I recommend this..

Use an Editbox instead...
When it is clicked, the caption will toggle.

If your two options were 'On' and 'Off' ...
In the OnClick or OnDblClick method


procedure Edit1DblClick(sender: TObject);
begin
 if Edit1.Text = 'On' then
   Edit1.Text := 'Off'
 else Edit1.Text := 'On';
end;

Simple.
If you have more than 2 options, let me and I'll show how to skip to the next one without any dropdown,

Don
0
 
LVL 2

Author Comment

by:mikepj
ID: 6243617
bugroger!

That is a beautiful piece of work & it works great!

Thanks a lot!  (I increased the points to 150 as I see this was much more involved than I had hoped)

MP
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

696 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