PageRenderTime 26ms CodeModel.GetById 7ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/listboxtest.pp

http://github.com/graemeg/lazarus
Pascal | 156 lines | 111 code | 17 blank | 28 comment | 5 complexity | 2a2e1a49dec098fc9b928f9dcb0b595a MD5 | raw file
  1{
  2 ***************************************************************************
  3 *                                                                         *
  4 *   This source is free software; you can redistribute it and/or modify   *
  5 *   it under the terms of the GNU General Public License as published by  *
  6 *   the Free Software Foundation; either version 2 of the License, or     *
  7 *   (at your option) any later version.                                   *
  8 *                                                                         *
  9 *   This code is distributed in the hope that it will be useful, but      *
 10 *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
 11 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
 12 *   General Public License for more details.                              *
 13 *                                                                         *
 14 *   A copy of the GNU General Public License is available on the World    *
 15 *   Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also      *
 16 *   obtain it by writing to the Free Software Foundation,                 *
 17 *   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.        *
 18 *                                                                         *
 19 ***************************************************************************
 20}
 21program ListBoxTest;
 22
 23{$mode objfpc}{$H+}
 24
 25uses
 26  Interfaces, Classes, Forms, StdCtrls, SysUtils, Controls, LazLogger;
 27
 28type
 29  TListBoxTestForm = class(TForm)
 30  public
 31    Button1, Button2, Button3, Button4:   TButton;
 32    ListBox:  TListBox;
 33    constructor Create(AOwner: TComponent); override;
 34    procedure Button1Click(Sender: TObject);
 35    procedure Button2Click(Sender: TObject);
 36    procedure Button3Click(Sender: TObject);
 37    procedure Button4Click(Sender: TObject);
 38    procedure FormResize(Sender: TObject);
 39  end;
 40
 41var
 42  ListBoxTestForm: TListBoxTestForm;
 43
 44{------------------------------------------------------------------------------}
 45{  TListBoxTestForm                                          }
 46{------------------------------------------------------------------------------}
 47constructor TListBoxTestForm.Create(AOwner: TComponent);
 48begin
 49  inherited CreateNew(AOwner, 1);
 50  Width := 300;
 51  Height := 200;
 52  Left := 200;
 53  Top := 200;
 54
 55  // create children
 56  Button1 := TButton.Create(Self);
 57  Button1.OnClick := @Button1Click;
 58  Button1.Parent := Self;
 59  Button1.left := 40;
 60  Button1.top :=  170;
 61  Button1.width := 50;
 62  Button1.height := 25;
 63  Button1.caption := 'New';
 64  Button1.Show;
 65
 66  Button2 := TButton.Create(Self);
 67  Button2.OnClick := @Button2Click;
 68  Button2.Parent := Self;
 69  Button2.left := 95;
 70  Button2.top := 170;
 71  Button2.width := 50;
 72  Button2.height := 25;
 73  Button2.caption := 'Delete';
 74  Button2.Show;
 75
 76  Button3 := TButton.Create(Self);
 77  Button3.OnClick := @Button3Click;
 78  Button3.Parent := Self;
 79  Button3.left := 150;
 80  Button3.top := 170;
 81  Button3.width := 50;
 82  Button3.height := 25;
 83  Button3.caption := 'Clear';
 84  Button3.Show;
 85
 86  Button4 := TButton.Create(Self);
 87  Button4.OnClick := @button4click;
 88  Button4.Parent := Self;
 89  Button4.left := 205;
 90  Button4.top := 170;
 91  Button4.width := 50;
 92  Button4.height := 25;
 93  Button4.caption := 'Unused';
 94  Button4.Show;
 95
 96//  ListBox := TCListBox.Create(Self);
 97  ListBox := TListBox.Create(Self);
 98  ListBox.Parent := Self;
 99  ListBox.Left := 10;
100  ListBox.Top := 10;
101  ListBox.Width := 280;
102  ListBox.Height := 150;
103  ListBox.Anchors := [akLeft, akRight, akTop];
104{  ListBox.ExtendedSelect := true;
105  ListBox.MultiSelect := true;
106 } ListBox.Show;
107 
108  OnResize := @FormResize;
109end;
110
111procedure TListBoxTestForm.Button1Click(Sender: TObject);
112var
113  Index: integer;
114begin
115  Index := ListBox.ItemIndex;
116  if Index = -1 then
117    ListBox.Items.Add('Button 1 clicked')
118  else 
119    ListBox.Items.Insert(Index, 'Button 1 clicked at '+IntToStr(Index));
120  for Index := 0 to ListBox.Items.Count - 1 do 
121    ListBox.Items.Objects[Index] := TObject(PtrInt(Index));
122end;
123
124procedure TListBoxTestForm.Button2Click(Sender: TObject);
125var
126  Index: integer;
127begin
128  Index := ListBox.ItemIndex;
129  if Index <> -1
130  then ListBox.Items.Delete(Index);
131end;
132
133procedure TListBoxTestForm.Button3Click(Sender: TObject);
134begin
135  ListBox.Items.Clear;
136end;
137
138procedure TListBoxTestForm.Button4Click(Sender: TObject);
139var
140  X: PtrInt;
141begin
142  if ListBox.ItemIndex < 0 then Exit;
143  X := PtrInt(ListBox.Items.Objects[ListBox.ItemIndex]);
144  DebugLn(['TListBoxTestForm.Button4Click ',X]);
145end;
146
147procedure TListBoxTestForm.FormResize(Sender: TObject);
148begin                      
149  Caption := Format('%dx%d', [ListBox.Width, ListBox.Height]);
150end;
151
152begin
153   Application.Initialize;
154   Application.CreateForm(TListBoxTestForm, ListBoxTestForm);
155   Application.Run;
156end.