Skip to main content

TCustomdxBarCombo, TCustomdxBarCombo.OnGetDropDownWindow, TdxBarManager.HideAll Example

  • 2 minutes to read

This example creates a dropdown window with a grid. The grid cells contain numbers. The sum of the selected cells is added to the TCustomdxBarCombo. The TdxBarManager.HideAll method is used to hide the dropdown grid.

{ TForm1 is a class of the form that contains the bar manager, toolbars and items; CustomdxBarCombo1  is an item of the TCustomdxBarCombo type; DropDownGrid1  is a dropdown window with a grid}
interface
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, dxBar, StdCtrls, Grids;
type
{Definiton of the form}
TForm1 = class(TForm)
   private
   //...
{Definition of the dropdown grid class }
TDropDownGrid = class(TStringGrid)
private
  FCustomCombo : TCustomdxBarCombo;
public
  constructor Create(AOwner:TComponent); override;
  procedure CreateParams(var Params: TCreateParams); override;
  procedure CreateWnd; override;
  { MyMouseUp is an OnMouseUp event handler of the TDropDownGrid object }
  procedure MyMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  property CustomCombo : TCustomdxBarCombo read FCustomCombo write FCustomCombo;
end;
var
  DropDownGrid1 : TDropDownGrid;
implementation
constructor TDropDownGrid.Create(AOwner: TComponent);
var
  i, j : Integer;
begin
  inherited;
  RowCount := 5;
  ColCount := 5;
  FixedCols := 0;
  FixedRows := 0;
  DefaultColWidth := 20;
  DefaultRowHeight := 20;
  Width := DefaultColWidth*ColCount+9;
  Height := DefaultRowHeight*RowCount+9;
  ScrollBars := ssNone;
  {Setting the OnGetDropDownWindow event handler}
  OnMouseUp := MyMouseUp;
  for j := 0 to RowCount -1 do
    for i := 0 to ColCount -1 do
      Cells[i, j] := IntToStr(j*RowCount+i+1);
  Visible := False;
end;
procedure TDropDownGrid.CreateParams(var Params: TCreateParams);
begin
  inherited;
  with Params do
    ExStyle := ExStyle or WS_EX_TOOLWINDOW or WS_EX_TOPMOST;
end;
procedure TDropDownGrid.CreateWnd;
begin
  inherited;
  Windows.SetParent(Handle, 0);
  CallWindowProc(DefWndProc, Handle, WM_SETFOCUS, 0, 0);
end;
procedure TDropDownGrid.MyMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  i, j, Sum, CellValue, ZeroCode: Integer;
begin
{Calculates the sum of cell numbers}
  Sum := 0;
  with DropDownGrid1.Selection do
  for I := Left to Right do
    for j :=Top to Bottom do
    begin
      Val(DropDownGrid1.Cells[i,j], CellValue, ZeroCode);
      Sum := Sum + CellValue;
    end;
  {Adds the sum to the TCustomdxBarCombo object}
  CustomCombo.Text := IntToStr(Sum);
  {Hides the dropdown grid}
  CustomCombo.BarManager.HideAll;
end;
constructor TForm1.Create(AOwner: Tcomponent);
begin
  inherited;
  DropDownGrid1 := TDropDownGrid.Create(Form1);
  DropDownGrid1.Parent := Self;
  DropDownGrid1.CustomCombo := CustomdxBarCombo1;
end;
destructor TForm1.Destroy;
begin
  DropDownGrid1.Free;
  inherited;
end;
{This procedure is the TCustomdxBarCombo OnGetDropDown event handler}
procedure TForm1.CustomdxBarCombo1GetDropDownWindow(
  Sender: TCustomdxBarCombo; var Window: HWND);
begin
  {The dropdown window handle}
  Window := DropDownGrid1.Handle;
end;