Skip to main content

How to Create a List of MDI Child Windows

  • 4 minutes to read

This example illustrates how you can create a list of MDI child windows with the help of TdxBarListItem. When creating a child window, its caption is added to the item list. Upon selecting a window from the list, the dxBarListWindowsClick procedure (the OnClick event handler) is called. The OnGetData event handler is used to mark the current active child window within the list. To display check marks within the list, set the TdxBarListItem.ShowCheck property to True.

unit Unit1;
interface
uses
  Classes, Forms, dxBar;
type
  TMainForm = class(TForm)
    dxBarManager: TdxBarManager;
    dxBarButtonNewWindow: TdxBarButton;
    dxBarButtonArrangeAll: TdxBarButton;
    dxBarButtonNextWindow: TdxBarButton;
    dxBarButtonPreviousWindow: TdxBarButton;
    dxBarListWindows: TdxBarListItem;
    dxBarSubItemWindow: TdxBarSubItem;
    procedure dxBarButtonNewWindowClick(Sender: TObject);
    procedure dxBarButtonArrangeAllClick(Sender: TObject);
    procedure dxBarButtonNextWindowClick(Sender: TObject);
    procedure dxBarButtonPreviousWindowClick(Sender: TObject);
    procedure dxBarListWindowsGetData(Sender: TObject);
    procedure dxBarListWindowsClick(Sender: TObject);
  public
    CreatedMDICount: Integer;
  end;
var
  MainForm: TMainForm;
implementation
{$R *.DFM}
uses
  Unit2;
// Creates a new child form
procedure TMainForm.dxBarButtonNewWindowClick(Sender: TObject);
begin
  TChildForm.Create(Application);
end;
procedure TMainForm.dxBarButtonArrangeAllClick(Sender: TObject);
begin
  Tile;
end;
procedure TMainForm.dxBarButtonNextWindowClick(Sender: TObject);
begin
  Next;
end;
procedure TMainForm.dxBarButtonPreviousWindowClick(Sender: TObject);
begin
  Previous;
end;
// The OnGetData event handler of TdxBarListItem
procedure TMainForm.dxBarListWindowsGetData(Sender: TObject);
begin
  with dxBarListWindows do
    ItemIndex := Items.IndexOfObject(ActiveMDIChild);
end;
// The OnClick event handler of TdxBarListItem
procedure TMainForm.dxBarListWindowsClick(Sender: TObject);
begin
  with dxBarListWindows do
    TCustomForm(Items.Objects[ItemIndex]).Show;
end;
end.
// The child form unit
unit Unit2;
interface
uses
  Classes, Forms;
type
  TChildForm = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  end;
implementation
{$R *.DFM}
uses
  SysUtils, Unit1;
// Adds a child window to the child window list
procedure TChildForm.FormCreate(Sender: TObject);
begin
  Inc(MainForm.CreatedMDICount);
  Caption := 'Document' + IntToStr(MainForm.CreatedMDICount);
  MainForm.dxBarListWindows.Items.AddObject(Caption, Self);
end;
// Removes a child window from the child window list
procedure TChildForm.FormDestroy(Sender: TObject);
begin
  with MainForm.dxBarListWindows.Items do
    Delete(IndexOfObject(Self));
end;
procedure TChildForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;
end.