unit MainUnit;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ComCtrls, uDMDB;

type
  TMainForm = class(TForm)
    TreeView1: TTreeView;
    bInsert: TButton;
    bDelete: TButton;
    bUpdate: TButton;
    bEdit: TButton;
    bSelected: TButton;
    bClearSelection: TButton;
    procedure FormShow(Sender: TObject);
    procedure bInsertClick(Sender: TObject);
    procedure bEditClick(Sender: TObject);
    procedure bUpdateClick(Sender: TObject);
    procedure bDeleteClick(Sender: TObject);
    procedure bSelectedClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure bClearSelectionClick(Sender: TObject);


  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  MainForm: TMainForm;
  DMDB:TDMDB;


implementation

{$R *.dfm}

procedure TMainForm.bDeleteClick(Sender: TObject);
begin
DMDB.DeleteNode(Self,TreeView1);
end;

procedure TMainForm.bEditClick(Sender: TObject);
begin
DMDB.EditNode(Self,TreeView1);
end;

procedure TMainForm.bInsertClick(Sender: TObject);
begin
DMDB.InsertNodeInTree(Self,TreeView1);
end;

procedure TMainForm.bUpdateClick(Sender: TObject);
var i:integer;
begin

DMDB.UpdateTree(Self,TreeView1);


// Разворачиваем все узлы
TreeView1.Items.BeginUpdate;
for i := 0 to TreeView1.Items.Count-1 do
  begin
    TreeView1.Items.Item[i].Expanded:=true;
  end;

TreeView1.Items.EndUpdate;

end;



procedure TMainForm.bSelectedClick(Sender: TObject);
begin
// if TreeView1.Selected<>nil then
// showmessage(TreeView1.Selected.Text);

 TreeView1.SetFocus;
 TreeView1.Items.Item[1].Selected:=true; // << Установка фокуса на элементе

end;


procedure TMainForm.bClearSelectionClick(Sender: TObject);
begin
TreeView1.Selected.Selected:=false;
end;

procedure TMainForm.FormShow(Sender: TObject);
begin

DMDB:=TDMDB.Create(Self);

DMDB.Connect(Self);
if DMDB.FDConnection.Connected then Self.Caption:=Self.Caption+' Connected to DB';

Self.bUpdate.Click;

end;

procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
 DMDB.ClearTree(TreeView1);
end;

end.
