File: tv_add_remove_u1.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (114 lines) | stat: -rw-r--r-- 2,662 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
unit TV_Add_Remove_U1;

{
This demo was written by Andre .v.d. Merwe and marked public domain.
Quickly converted to Lazarus and FPC by Tom Lisjac <vlx@users.sourceforge.net>

The original source and an *excellent* tutorial on the TTreeview
component can be found here:
  http://users.iafrica.com/d/da/dart/Delphi/TTreeView/TreeView.html
}

interface 

{$mode objfpc} {$H+}

uses
  SysUtils, LResources, Classes, LCLProc, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ComCtrls, Buttons;

type
  TForm1 = class(TForm)
    tv_eg1: TTreeView;
    but_Add: TButton;
    but_Remove: TButton;
    procedure but_AddClick(Sender: TObject);
    procedure but_RemoveClick(Sender: TObject);
  private
  public
    constructor Create(TheOwner: TComponent); override;
  end;

var
  Form1: TForm1;

implementation

procedure TForm1.but_AddClick(Sender: TObject);
var
   sText : ansistring;
begin
      {If nothing is selected}
   if(  tv_eg1.Selected = nil  ) then
   begin
         {Does a root node already exist?}
      if(  tv_eg1.Items.Count = 0  ) then
      begin
            {Add the root node}
         with tv_eg1.Items.AddFirst(  nil,  'Root'  ) do
         begin
            Selected := true;
            debugln('tv_eg1.Selected=',DbgS(tv_eg1.Selected));
         end;
      end
      else begin
            {There is a root, so user must first select a node}
         //MessageBeep(  -1  );
         ShowMessage(  'Select a parent node'  );
         Exit;
      end;
   end
   else begin
         {Get a name for the new node}
      sText := 'New node';
      InputQuery(  'New Node',  'Caption ?', sText  );

         {Add the node as a child of the selected node}
      with tv_eg1.Items.AddChild(  tv_eg1.Selected,  sText  ) do
      begin
         MakeVisible;
      end;
   end;
end;



procedure TForm1.but_RemoveClick(Sender: TObject);
begin
      {Make sure somthing is selected, before trying to
        delete it}
   if(  tv_eg1.Selected = nil  ) then
   begin
      //MessageBeep(  -1  );
      ShowMessage(  'Nothing selected'  );
      Exit;
   end;

      {Dont allow user to delete the root node}
   if(  tv_eg1.Selected.Level = 0  ) then
   begin
      //MessageBeep(  -1  );
      ShowMessage(  'Cant delete the root node'  );
      Exit;
   end;


      {Delete the node}
   tv_eg1.Selected.Delete;
end;

constructor TForm1.Create(TheOwner: TComponent);
var
  RootNode: TTreeNode;
begin
  inherited Create(TheOwner);
  RootNode:=tv_eg1.Items.AddFirst(nil,'Root');
  tv_eg1.Items.AddChild(RootNode,'Node1');
  tv_eg1.Items.AddChild(RootNode,'Node2');
  tv_eg1.Items.AddChild(RootNode,'Node3');
  RootNode.Expanded:=true;
end;

{$R *.lfm}

end.