mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-08-11 14:37:48 +02:00
Examples: listview: added OwnerData mode, added property ReadOnly, AutoWidthLastColumn, fixed exceptions. issue #18898
git-svn-id: trunk@35268 -
This commit is contained in:
parent
9fba3d205f
commit
cd7931e012
@ -1,75 +1,75 @@
|
||||
object Form1: TForm1
|
||||
Left = 356
|
||||
Height = 299
|
||||
Height = 360
|
||||
Top = 150
|
||||
Width = 627
|
||||
Width = 620
|
||||
HorzScrollBar.Page = 626
|
||||
VertScrollBar.Page = 298
|
||||
ActiveControl = CheckBox3
|
||||
Caption = 'Listview test'
|
||||
ClientHeight = 299
|
||||
ClientWidth = 627
|
||||
ClientHeight = 360
|
||||
ClientWidth = 620
|
||||
LCLVersion = '0.9.31'
|
||||
object PageControl1: TPageControl
|
||||
Left = 433
|
||||
Height = 280
|
||||
Left = 399
|
||||
Height = 341
|
||||
Top = 8
|
||||
Width = 184
|
||||
Width = 211
|
||||
ActivePage = Page3
|
||||
Anchors = [akTop, akRight]
|
||||
TabIndex = 0
|
||||
TabOrder = 0
|
||||
object Page3: TTabSheet
|
||||
Caption = 'LV'
|
||||
ClientHeight = 254
|
||||
ClientWidth = 176
|
||||
ClientHeight = 307
|
||||
ClientWidth = 205
|
||||
object Label11: TLabel
|
||||
Left = 6
|
||||
Height = 14
|
||||
Height = 17
|
||||
Top = 60
|
||||
Width = 47
|
||||
Width = 59
|
||||
Caption = 'Scrollbars'
|
||||
ParentColor = False
|
||||
end
|
||||
object Label12: TLabel
|
||||
Left = 6
|
||||
Height = 14
|
||||
Height = 17
|
||||
Top = 122
|
||||
Width = 56
|
||||
Width = 69
|
||||
Caption = 'SortColumn'
|
||||
ParentColor = False
|
||||
end
|
||||
object Label13: TLabel
|
||||
Left = 6
|
||||
Height = 14
|
||||
Height = 17
|
||||
Top = 154
|
||||
Width = 45
|
||||
Width = 52
|
||||
Caption = 'SortType'
|
||||
ParentColor = False
|
||||
end
|
||||
object Label14: TLabel
|
||||
Left = 6
|
||||
Height = 14
|
||||
Height = 17
|
||||
Top = 186
|
||||
Width = 47
|
||||
Width = 59
|
||||
Caption = 'ViewStyle'
|
||||
ParentColor = False
|
||||
end
|
||||
object CheckBox3: TCheckBox
|
||||
Left = 6
|
||||
Height = 17
|
||||
Height = 22
|
||||
Top = 4
|
||||
Width = 68
|
||||
Width = 87
|
||||
Caption = 'Multiselect'
|
||||
OnClick = CheckBox3Click
|
||||
TabOrder = 0
|
||||
end
|
||||
object ComboBox2: TComboBox
|
||||
Left = 77
|
||||
Height = 21
|
||||
Height = 26
|
||||
Top = 52
|
||||
Width = 95
|
||||
ItemHeight = 13
|
||||
ItemHeight = 0
|
||||
ItemIndex = 6
|
||||
Items.Strings = (
|
||||
'ssNone'
|
||||
@ -87,7 +87,7 @@ object Form1: TForm1
|
||||
end
|
||||
object Edit11: TEdit
|
||||
Left = 78
|
||||
Height = 21
|
||||
Height = 22
|
||||
Top = 116
|
||||
Width = 80
|
||||
OnChange = Edit11Change
|
||||
@ -96,10 +96,10 @@ object Form1: TForm1
|
||||
end
|
||||
object ComboBox3: TComboBox
|
||||
Left = 77
|
||||
Height = 21
|
||||
Height = 26
|
||||
Top = 146
|
||||
Width = 95
|
||||
ItemHeight = 13
|
||||
ItemHeight = 0
|
||||
ItemIndex = 0
|
||||
Items.Strings = (
|
||||
'stNone'
|
||||
@ -114,10 +114,10 @@ object Form1: TForm1
|
||||
end
|
||||
object ComboBox4: TComboBox
|
||||
Left = 78
|
||||
Height = 21
|
||||
Height = 26
|
||||
Top = 178
|
||||
Width = 95
|
||||
ItemHeight = 13
|
||||
ItemHeight = 0
|
||||
ItemIndex = 3
|
||||
Items.Strings = (
|
||||
'vsIcon'
|
||||
@ -132,18 +132,18 @@ object Form1: TForm1
|
||||
end
|
||||
object CheckBox4: TCheckBox
|
||||
Left = 6
|
||||
Height = 17
|
||||
Height = 22
|
||||
Top = 28
|
||||
Width = 68
|
||||
Width = 86
|
||||
Caption = 'RowSelect'
|
||||
OnClick = CheckBox4Click
|
||||
TabOrder = 5
|
||||
end
|
||||
object CheckBox5: TCheckBox
|
||||
Left = 6
|
||||
Height = 17
|
||||
Height = 22
|
||||
Top = 84
|
||||
Width = 119
|
||||
Width = 148
|
||||
Caption = 'ShowColumnHeaders'
|
||||
Checked = True
|
||||
OnClick = CheckBox5Click
|
||||
@ -151,26 +151,75 @@ object Form1: TForm1
|
||||
TabOrder = 6
|
||||
end
|
||||
object HideSelection: TCheckBox
|
||||
Left = 91
|
||||
Height = 17
|
||||
Left = 97
|
||||
Height = 22
|
||||
Top = 4
|
||||
Width = 82
|
||||
Width = 104
|
||||
Caption = 'HideSelection'
|
||||
Checked = True
|
||||
OnChange = HideSelectionChange
|
||||
State = cbChecked
|
||||
TabOrder = 7
|
||||
end
|
||||
object ChBoxOwnerData: TCheckBox
|
||||
Left = 6
|
||||
Height = 22
|
||||
Top = 249
|
||||
Width = 88
|
||||
Caption = 'OwnerData'
|
||||
OnChange = ChBoxOwnerDataChange
|
||||
TabOrder = 8
|
||||
end
|
||||
object lblSetCount: TLabel
|
||||
Left = 10
|
||||
Height = 17
|
||||
Top = 281
|
||||
Width = 55
|
||||
Caption = 'SetCount'
|
||||
Enabled = False
|
||||
ParentColor = False
|
||||
end
|
||||
object OwnerDataCount: TSpinEdit
|
||||
Left = 88
|
||||
Height = 22
|
||||
Top = 276
|
||||
Width = 84
|
||||
Enabled = False
|
||||
MaxValue = 10000000
|
||||
OnChange = OwnerDataCountChange
|
||||
TabOrder = 9
|
||||
Value = 2
|
||||
end
|
||||
object CheckBox6: TCheckBox
|
||||
Left = 6
|
||||
Height = 22
|
||||
Top = 217
|
||||
Width = 154
|
||||
Caption = 'AutoWidthLastColumn'
|
||||
OnChange = CheckBox6Change
|
||||
TabOrder = 10
|
||||
end
|
||||
object CheckBox7: TCheckBox
|
||||
Left = 97
|
||||
Height = 22
|
||||
Top = 28
|
||||
Width = 81
|
||||
Caption = 'ReadOnly'
|
||||
Checked = True
|
||||
OnChange = CheckBox7Change
|
||||
State = cbChecked
|
||||
TabOrder = 11
|
||||
end
|
||||
end
|
||||
object Page1: TTabSheet
|
||||
Caption = 'Items'
|
||||
ClientHeight = 0
|
||||
ClientWidth = 0
|
||||
ClientHeight = 307
|
||||
ClientWidth = 205
|
||||
object Label6: TLabel
|
||||
Left = 6
|
||||
Height = 17
|
||||
Top = 4
|
||||
Width = 65
|
||||
Width = 32
|
||||
Caption = 'Index'
|
||||
ParentColor = False
|
||||
end
|
||||
@ -178,7 +227,7 @@ object Form1: TForm1
|
||||
Left = 94
|
||||
Height = 17
|
||||
Top = 4
|
||||
Width = 65
|
||||
Width = 55
|
||||
Caption = 'SubIndex'
|
||||
ParentColor = False
|
||||
end
|
||||
@ -186,7 +235,7 @@ object Form1: TForm1
|
||||
Left = 6
|
||||
Height = 17
|
||||
Top = 116
|
||||
Width = 65
|
||||
Width = 25
|
||||
Caption = 'Text'
|
||||
ParentColor = False
|
||||
end
|
||||
@ -194,7 +243,7 @@ object Form1: TForm1
|
||||
Left = 94
|
||||
Height = 17
|
||||
Top = 116
|
||||
Width = 65
|
||||
Width = 67
|
||||
Caption = 'ImageIndex'
|
||||
ParentColor = False
|
||||
end
|
||||
@ -240,7 +289,7 @@ object Form1: TForm1
|
||||
end
|
||||
object Edit1: TEdit
|
||||
Left = 6
|
||||
Height = 23
|
||||
Height = 22
|
||||
Top = 20
|
||||
Width = 80
|
||||
OnChange = Edit1Change
|
||||
@ -249,7 +298,7 @@ object Form1: TForm1
|
||||
end
|
||||
object Edit4: TEdit
|
||||
Left = 6
|
||||
Height = 23
|
||||
Height = 22
|
||||
Top = 132
|
||||
Width = 80
|
||||
OnChange = Edit4Change
|
||||
@ -258,7 +307,7 @@ object Form1: TForm1
|
||||
end
|
||||
object Edit5: TEdit
|
||||
Left = 94
|
||||
Height = 23
|
||||
Height = 22
|
||||
Top = 20
|
||||
Width = 80
|
||||
OnChange = Edit5Change
|
||||
@ -267,7 +316,7 @@ object Form1: TForm1
|
||||
end
|
||||
object Edit10: TEdit
|
||||
Left = 94
|
||||
Height = 23
|
||||
Height = 22
|
||||
Top = 132
|
||||
Width = 80
|
||||
OnChange = Edit10Change
|
||||
@ -277,8 +326,8 @@ object Form1: TForm1
|
||||
end
|
||||
object Page2: TTabSheet
|
||||
Caption = 'Columns'
|
||||
ClientHeight = 0
|
||||
ClientWidth = 0
|
||||
ClientHeight = 307
|
||||
ClientWidth = 205
|
||||
object Label1: TLabel
|
||||
Left = 6
|
||||
Height = 17
|
||||
@ -414,7 +463,7 @@ object Form1: TForm1
|
||||
Height = 21
|
||||
Top = 191
|
||||
Width = 80
|
||||
ItemHeight = 13
|
||||
ItemHeight = 0
|
||||
Items.Strings = (
|
||||
'taLeftJustify'
|
||||
'taRightJustify'
|
||||
@ -436,28 +485,32 @@ object Form1: TForm1
|
||||
end
|
||||
object ListView1: TListView
|
||||
Left = 8
|
||||
Height = 280
|
||||
Height = 341
|
||||
Top = 8
|
||||
Width = 417
|
||||
Width = 383
|
||||
Anchors = [akTop, akLeft, akRight, akBottom]
|
||||
BorderWidth = 2
|
||||
Columns = <
|
||||
item
|
||||
Caption = 'One'
|
||||
Width = 80
|
||||
end
|
||||
item
|
||||
Caption = 'Two'
|
||||
Width = 301
|
||||
end>
|
||||
Items.LazData = {
|
||||
540000000200000000000000FFFFFFFFFFFFFFFF02000000050000004E657720
|
||||
31050000005375622031050000005375622032FFFFFFFFFFFFFFFFFFFFFFFF00
|
||||
000000050000004E65772032FFFFFFFFFFFFFFFF
|
||||
}
|
||||
ReadOnly = True
|
||||
ScrollBars = ssAutoBoth
|
||||
SmallImages = ImageList1
|
||||
TabOrder = 1
|
||||
ViewStyle = vsReport
|
||||
OnColumnClick = ListView1ColumnClick
|
||||
OnData = ListView1Data
|
||||
OnSelectItem = ListView1SelectItem
|
||||
end
|
||||
object ImageList1: TImageList
|
||||
|
@ -6,7 +6,7 @@ interface
|
||||
|
||||
uses
|
||||
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ComCtrls,
|
||||
Buttons, StdCtrls, ExtCtrls;
|
||||
Buttons, StdCtrls, ExtCtrls, Spin;
|
||||
|
||||
type
|
||||
|
||||
@ -24,6 +24,9 @@ type
|
||||
CheckBox3: TCheckBox;
|
||||
CheckBox4: TCheckBox;
|
||||
CheckBox5: TCheckBox;
|
||||
ChBoxOwnerData: TCheckBox;
|
||||
CheckBox6: TCheckBox;
|
||||
CheckBox7: TCheckBox;
|
||||
HideSelection: TCheckBox;
|
||||
ComboBox1: TComboBox;
|
||||
ComboBox2: TComboBox;
|
||||
@ -47,6 +50,7 @@ type
|
||||
Label12: TLabel;
|
||||
Label13: TLabel;
|
||||
Label14: TLabel;
|
||||
lblSetCount: TLabel;
|
||||
Label2: TLabel;
|
||||
Label3: TLabel;
|
||||
Label4: TLabel;
|
||||
@ -60,17 +64,21 @@ type
|
||||
Page1: TTabSheet;
|
||||
Page2: TTabSheet;
|
||||
Page3: TTabSheet;
|
||||
OwnerDataCount: TSpinEdit;
|
||||
procedure Button1Click(Sender: TObject);
|
||||
procedure Button2Click(Sender: TObject);
|
||||
procedure Button3Click(Sender: TObject);
|
||||
procedure Button4Click(Sender: TObject);
|
||||
procedure Button5Click(Sender: TObject);
|
||||
procedure Button6Click(Sender: TObject);
|
||||
procedure ChBoxOwnerDataChange(Sender: TObject);
|
||||
procedure CheckBox1Click (Sender: TObject );
|
||||
procedure CheckBox2Click (Sender: TObject );
|
||||
procedure CheckBox3Click(Sender: TObject);
|
||||
procedure CheckBox4Click(Sender: TObject);
|
||||
procedure CheckBox5Click(Sender: TObject);
|
||||
procedure CheckBox6Change(Sender: TObject);
|
||||
procedure CheckBox7Change(Sender: TObject);
|
||||
procedure ComboBox1Change (Sender: TObject );
|
||||
procedure ComboBox2Change(Sender: TObject);
|
||||
procedure ComboBox3Change(Sender: TObject);
|
||||
@ -87,7 +95,9 @@ type
|
||||
procedure Edit8Change(Sender: TObject);
|
||||
procedure HideSelectionChange(Sender: TObject);
|
||||
procedure ListView1ColumnClick(Sender: TObject; Column: TListColumn);
|
||||
procedure ListView1Data(Sender: TObject; Item: TListItem);
|
||||
procedure ListView1SelectItem(Sender: TObject; Item: TListItem; Selected: Boolean);
|
||||
procedure OwnerDataCountChange(Sender: TObject);
|
||||
private
|
||||
procedure ShowItemData;
|
||||
procedure ShowColumnData;
|
||||
@ -109,16 +119,31 @@ procedure TForm1.Button1Click(Sender: TObject);
|
||||
var
|
||||
Item: TListItem;
|
||||
begin
|
||||
Item := ListView1.Items.Insert(StrToIntDef(Edit1.Text, 0));
|
||||
Item.Caption := Format('Item %d', [ListView1.Items.Count]);
|
||||
if ListView1.OwnerData then
|
||||
// OwnerDataCount.OnChange() triggers new items count.
|
||||
OwnerDataCount.Value := ListView1.Items.Count + 1
|
||||
else
|
||||
begin
|
||||
Item := ListView1.Items.Insert(StrToIntDef(Edit1.Text, 0));
|
||||
Item.Caption := Format('Item %d', [ListView1.Items.Count]);
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TForm1.Button2Click(Sender: TObject);
|
||||
var
|
||||
Item: TListItem;
|
||||
Idx: Integer;
|
||||
begin
|
||||
Item := ListView1.Items[StrToIntDef(Edit1.Text, 0)];
|
||||
Item.Free;
|
||||
if ListView1.OwnerData then
|
||||
OwnerDataCount.Value := ListView1.Items.Count - 1
|
||||
else
|
||||
begin
|
||||
Idx := StrToIntDef(Edit1.Text, 0);
|
||||
if (Idx < 0) or (Idx > ListView1.Items.Count - 1) then
|
||||
exit;
|
||||
Item := ListView1.Items[Idx];
|
||||
Item.Free;
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TForm1.Button3Click(Sender: TObject);
|
||||
@ -141,19 +166,65 @@ end;
|
||||
procedure TForm1.Button5Click(Sender: TObject);
|
||||
var
|
||||
Item: TListItem;
|
||||
Idx: Integer;
|
||||
begin
|
||||
Item := ListView1.Items[StrToIntDef(Edit1.Text, 0)];
|
||||
Idx := StrToIntDef(Edit1.Text, 0);
|
||||
if (Idx < 0) or (Idx > ListView1.Items.Count - 1) then
|
||||
exit;
|
||||
Item := ListView1.Items[Idx];
|
||||
Item.Selected := True;
|
||||
end;
|
||||
|
||||
procedure TForm1.Button6Click(Sender: TObject);
|
||||
var
|
||||
Item: TListItem;
|
||||
Idx: Integer;
|
||||
begin
|
||||
Item := ListView1.Items[StrToIntDef(Edit1.Text, 0)];
|
||||
Idx := StrToIntDef(Edit1.Text, 0);
|
||||
if (Idx < 0) or (Idx > ListView1.Items.Count - 1) then
|
||||
exit;
|
||||
Item := ListView1.Items[Idx];
|
||||
Item.Focused := True;
|
||||
end;
|
||||
|
||||
procedure TForm1.ChBoxOwnerDataChange(Sender: TObject);
|
||||
var
|
||||
B: Boolean;
|
||||
AItem: TListItem;
|
||||
begin
|
||||
B := TCheckBox(Sender).Checked;
|
||||
lblSetCount.Enabled := B;
|
||||
OwnerDataCount.Enabled := B;
|
||||
ListView1.BeginUpdate;
|
||||
try
|
||||
ListView1.Clear; // first clear all items because virtual and normal items are different classes
|
||||
finally
|
||||
ListView1.EndUpdate;
|
||||
end;
|
||||
ListView1.OwnerData := B;
|
||||
ListView1.BeginUpdate;
|
||||
try
|
||||
if B then
|
||||
ListView1.Items.Count := OwnerDataCount.Value
|
||||
else
|
||||
begin
|
||||
with ListView1 do
|
||||
begin
|
||||
AItem := Items.Add;
|
||||
AItem.Caption := 'New 1';
|
||||
AItem.ImageIndex := 0;
|
||||
AItem.StateIndex := 0;
|
||||
AItem.SubItems.Add('Sub 1');
|
||||
|
||||
AItem := Items.Add;
|
||||
AItem.Caption := 'New 2';
|
||||
end;
|
||||
end;
|
||||
finally
|
||||
ListView1.EndUpdate;
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TForm1.CheckBox1Click(Sender: TObject);
|
||||
var
|
||||
Column: TListColumn;
|
||||
@ -185,6 +256,16 @@ begin
|
||||
ListView1.ShowColumnHeaders := CheckBox5.Checked;
|
||||
end;
|
||||
|
||||
procedure TForm1.CheckBox6Change(Sender: TObject);
|
||||
begin
|
||||
ListView1.AutoWidthLastColumn := TCheckBox(Sender).Checked;
|
||||
end;
|
||||
|
||||
procedure TForm1.CheckBox7Change(Sender: TObject);
|
||||
begin
|
||||
ListView1.ReadOnly := TCheckBox(Sender).Checked;
|
||||
end;
|
||||
|
||||
procedure TForm1.ComboBox1Change (Sender: TObject );
|
||||
var
|
||||
Column: TListColumn;
|
||||
@ -214,7 +295,10 @@ var
|
||||
n, idx: Integer;
|
||||
|
||||
begin
|
||||
Item := ListView1.Items[StrToIntDef(Edit1.Text, 0)];
|
||||
idx := StrToIntDef(Edit1.Text, 0);
|
||||
if (idx < 0) or (idx > ListView1.Items.Count - 1) then
|
||||
exit;
|
||||
Item := ListView1.Items[idx];
|
||||
|
||||
for n := Item.SubItems.Count to ListView1.Columns.Count - 2 do
|
||||
Item.Subitems.Add('');
|
||||
@ -222,10 +306,10 @@ begin
|
||||
n := StrToIntDef(Edit5.Text, 0);
|
||||
if n >= ListView1.Columns.Count then Exit;
|
||||
idx := StrToIntDef(Edit10.Text, -1);
|
||||
if n = 0
|
||||
then Item.ImageIndex := idx
|
||||
else Item.SubitemImages[n - 1] := idx;
|
||||
|
||||
if n = 0 then
|
||||
Item.ImageIndex := idx
|
||||
else
|
||||
Item.SubitemImages[n - 1] := idx;
|
||||
end;
|
||||
|
||||
procedure TForm1.Edit11Change(Sender: TObject);
|
||||
@ -256,7 +340,10 @@ var
|
||||
Item: TListItem;
|
||||
n: Integer;
|
||||
begin
|
||||
Item := ListView1.Items[StrToIntDef(Edit1.Text, 0)];
|
||||
n := StrToIntDef(Edit1.Text, 0);
|
||||
if (n < 0) or (n > ListView1.Items.Count - 1) then
|
||||
exit;
|
||||
Item := ListView1.Items[n];
|
||||
for n := Item.SubItems.Count to ListView1.Columns.Count - 2 do
|
||||
Item.Subitems.Add('');
|
||||
n := StrToIntDef(Edit5.Text, 0);
|
||||
@ -306,6 +393,21 @@ begin
|
||||
Edit5.Text := IntToStr(Column.Index);
|
||||
end;
|
||||
|
||||
procedure TForm1.ListView1Data(Sender: TObject; Item: TListItem);
|
||||
var
|
||||
i: Integer;
|
||||
begin
|
||||
if (ListView1.ColumnCount > 0) then
|
||||
begin
|
||||
Item.Caption := 'OwnerData item '+IntToStr(Item.Index);
|
||||
Item.ImageIndex := -1;
|
||||
Item.StateIndex := -1;
|
||||
for i := 1 to ListView1.ColumnCount - 1 do
|
||||
Item.SubItems.Add('OwnerData subitem ['+IntToStr(Item.Index)+','+IntToStr(i)+']');
|
||||
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TForm1.ListView1SelectItem(Sender: TObject; Item: TListItem; Selected: Boolean);
|
||||
var
|
||||
subIdx: Integer;
|
||||
@ -314,12 +416,24 @@ begin
|
||||
Edit1.Text := IntToStr(Item.Index);
|
||||
end;
|
||||
|
||||
procedure TForm1.OwnerDataCountChange(Sender: TObject);
|
||||
begin
|
||||
if not ListView1.OwnerData then
|
||||
exit;
|
||||
ListView1.BeginUpdate;
|
||||
ListView1.Items.Count := TSpinEdit(Sender).Value;
|
||||
ListView1.EndUpdate;
|
||||
end;
|
||||
|
||||
procedure TForm1.ShowItemData;
|
||||
var
|
||||
Item: TListItem;
|
||||
idx: Integer;
|
||||
begin
|
||||
Item := ListView1.Items[StrToIntDef(Edit1.Text, 0)];
|
||||
idx := StrToIntDef(Edit1.Text, 0);
|
||||
if (idx < 0) or (idx > ListView1.Items.Count - 1) then
|
||||
exit;
|
||||
Item := ListView1.Items[idx];
|
||||
|
||||
idx := StrToIntDef(Edit5.Text, 0);
|
||||
if idx = 0
|
||||
|
Loading…
Reference in New Issue
Block a user