Solved

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

Posted on 2001-07-01
3
411 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 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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Mobile Keyboard covers the display of the TMemo 3 86
creating threads in delphi 1 104
SUM 2 INTEGER ARRAYS INTO 1 10 100
TEMBEDDEDWB how can i change its user agent ? 8 58
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…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

770 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