?
Solved

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

Posted on 2001-07-01
3
Medium Priority
?
437 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
3 Comments
 
LVL 2

Accepted Solution

by:
bugroger earned 600 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

850 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