/examples/listboxtest.pp

http://github.com/graemeg/lazarus · Puppet · 156 lines · 139 code · 17 blank · 0 comment · 6 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. }
  21. program ListBoxTest;
  22. {$mode objfpc}{$H+}
  23. uses
  24. Interfaces, Classes, Forms, StdCtrls, SysUtils, Controls, LazLogger;
  25. type
  26. TListBoxTestForm = class(TForm)
  27. public
  28. Button1, Button2, Button3, Button4: TButton;
  29. ListBox: TListBox;
  30. constructor Create(AOwner: TComponent); override;
  31. procedure Button1Click(Sender: TObject);
  32. procedure Button2Click(Sender: TObject);
  33. procedure Button3Click(Sender: TObject);
  34. procedure Button4Click(Sender: TObject);
  35. procedure FormResize(Sender: TObject);
  36. end;
  37. var
  38. ListBoxTestForm: TListBoxTestForm;
  39. {------------------------------------------------------------------------------}
  40. { TListBoxTestForm }
  41. {------------------------------------------------------------------------------}
  42. constructor TListBoxTestForm.Create(AOwner: TComponent);
  43. begin
  44. inherited CreateNew(AOwner, 1);
  45. Width := 300;
  46. Height := 200;
  47. Left := 200;
  48. Top := 200;
  49. // create children
  50. Button1 := TButton.Create(Self);
  51. Button1.OnClick := @Button1Click;
  52. Button1.Parent := Self;
  53. Button1.left := 40;
  54. Button1.top := 170;
  55. Button1.width := 50;
  56. Button1.height := 25;
  57. Button1.caption := 'New';
  58. Button1.Show;
  59. Button2 := TButton.Create(Self);
  60. Button2.OnClick := @Button2Click;
  61. Button2.Parent := Self;
  62. Button2.left := 95;
  63. Button2.top := 170;
  64. Button2.width := 50;
  65. Button2.height := 25;
  66. Button2.caption := 'Delete';
  67. Button2.Show;
  68. Button3 := TButton.Create(Self);
  69. Button3.OnClick := @Button3Click;
  70. Button3.Parent := Self;
  71. Button3.left := 150;
  72. Button3.top := 170;
  73. Button3.width := 50;
  74. Button3.height := 25;
  75. Button3.caption := 'Clear';
  76. Button3.Show;
  77. Button4 := TButton.Create(Self);
  78. Button4.OnClick := @button4click;
  79. Button4.Parent := Self;
  80. Button4.left := 205;
  81. Button4.top := 170;
  82. Button4.width := 50;
  83. Button4.height := 25;
  84. Button4.caption := 'Unused';
  85. Button4.Show;
  86. // ListBox := TCListBox.Create(Self);
  87. ListBox := TListBox.Create(Self);
  88. ListBox.Parent := Self;
  89. ListBox.Left := 10;
  90. ListBox.Top := 10;
  91. ListBox.Width := 280;
  92. ListBox.Height := 150;
  93. ListBox.Anchors := [akLeft, akRight, akTop];
  94. { ListBox.ExtendedSelect := true;
  95. ListBox.MultiSelect := true;
  96. } ListBox.Show;
  97. OnResize := @FormResize;
  98. end;
  99. procedure TListBoxTestForm.Button1Click(Sender: TObject);
  100. var
  101. Index: integer;
  102. begin
  103. Index := ListBox.ItemIndex;
  104. if Index = -1 then
  105. ListBox.Items.Add('Button 1 clicked')
  106. else
  107. ListBox.Items.Insert(Index, 'Button 1 clicked at '+IntToStr(Index));
  108. for Index := 0 to ListBox.Items.Count - 1 do
  109. ListBox.Items.Objects[Index] := TObject(PtrInt(Index));
  110. end;
  111. procedure TListBoxTestForm.Button2Click(Sender: TObject);
  112. var
  113. Index: integer;
  114. begin
  115. Index := ListBox.ItemIndex;
  116. if Index <> -1
  117. then ListBox.Items.Delete(Index);
  118. end;
  119. procedure TListBoxTestForm.Button3Click(Sender: TObject);
  120. begin
  121. ListBox.Items.Clear;
  122. end;
  123. procedure TListBoxTestForm.Button4Click(Sender: TObject);
  124. var
  125. X: PtrInt;
  126. begin
  127. if ListBox.ItemIndex < 0 then Exit;
  128. X := PtrInt(ListBox.Items.Objects[ListBox.ItemIndex]);
  129. DebugLn(['TListBoxTestForm.Button4Click ',X]);
  130. end;
  131. procedure TListBoxTestForm.FormResize(Sender: TObject);
  132. begin
  133. Caption := Format('%dx%d', [ListBox.Width, ListBox.Height]);
  134. end;
  135. begin
  136. Application.Initialize;
  137. Application.CreateForm(TListBoxTestForm, ListBoxTestForm);
  138. Application.Run;
  139. end.