TListview component in Delphi
Using TListview component in Delphi.
TListview
RemoveDefaultInfoTips(ListView1);
end;
Implement Listview.onmousemove event to show formated hint as per our requirement.
procedure TForm1.ListView1MouseMove(Sender: TObject;
Shift: TShiftState; X, Y: Integer);
var
ptPos: TPoint;
lvItem : TListItem;
lvHitInfo: TLVHitTestInfo;
sHint: string;
begin
if ListView1.ViewStyle=vsReport then
begin
ptPos := ListView1.ScreenToClient(Mouse.CursorPos) ;
lvItem := ListView1.GetItemAt(ptPos.X, ptPos.Y) ;
if assigned(lvItem) then
begin
ListView1.Hint := '';
Application.CancelHint;
end
else // To show multiline hint for Listview subitems //
begin
FillChar(lvHitInfo, SizeOf(lvHitInfo), 0) ;
lvHitInfo.pt := ptPos;
if -1 <> ListView1.Perform(LVM_SUBITEMHITTEST, 0, LParam(@lvHitInfo)) then
begin
sHint:=Trim(ListView1.Items[lvHitInfo.iItem].SubItems[lvHitInfo.iSubItem-1]);
ListView1.Hint := WrapText(sHint, 60);
Application.ActivateHint(Mouse.CursorPos) ;
end
else
begin
ListView1.Hint := '';
Application.CancelHint;
end;
end;
end;
end;
TListview
TListView component is used to hold multiple items of various type. We can use TListview component to show Items in different view like items in Windows Explorer. When you want to show information in table-like manner, you might want to consider using TListView in vsReport style.
The items in the list view can have one or more of the following appearance features:
- A caption or detail text (for example, using the Item.Text bindable member of TListView)
- An associated image (for example, using the Item.Bitmap bindable member of TListView)
Defining Columns
After changing the style to vsReport we need to define columns of our "table". In design time we use Columns Editor like shown below.
or
In runtime, you can add column using code like below.
var
vNewColumn: TListColumn;
..
begin
...
vNewColumn := ListView1.Columns.Add;
vNewColumn.Caption := 'Items';
vNewColumn.Width := 100;
...
vNewColumn := ListView1.Columns.Add;
vNewColumn.Caption := 'SubItem1';
vNewColumn.Width := 100;
...
vNewColumn := ListView1.Columns.Add;
vNewColumn.Caption := 'SubItem2';
vNewColumn.Width := 100;
end;
To delete n-th column:
ListView1.Columns.Delete(n);
Adding Items and Subitems to a Listview
Each item correspond with one row. The items are of TListItem class. In this case we are interested in its Caption and SubItems property. TListItem's Caption will be displayed in the first column. Subsequent columns content came from strings contained in SubItems property.
Here is a code example that shows how to add items to a TListView:
// To achieve the best performance use BeginUpdate and EndUpdate.
var
LItem: TListViewItem;
I: Integer;
begin
ListView1.BeginUpdate;
try
for I := 1 to 10 do
begin
LItem := ListView1.Items.Add;
LItem.Caption := 'Item '+IntToStr(I);
LItem.SubItems.Add('SubItem 1');
LItem.SubItems.Add('SubItem 2);
end;
finally
ListView1.EndUpdate;
end;
end;
To delete n-th item:
ListView1.Items.Delete(N);
Some of interesting stuffs we can do with Listview component.
Setting colors of Listview Items as per User setting...
Changing color of Listview row as per Odd/Even row.
procedure TForm1.Listview1AdvancedCustomDrawItem( Sender: TCustomListView; Item: TListItem; State: TCustomDrawState; Stage: TCustomDrawStage; var DefaultDraw: Boolean) ;
begin
if Item=nil then Exit;
with Sender.Canvas do
begin
if Odd(Sender.ItemIndex ) then
begin
Brush.Color := clNavy;
Font.Color := clWhite;
end
else
begin
csUnknown : Brush.Color := clSilver;
Font.Color := clWindowText;
end;
end;
end;
Displaying hints for Listview items and subitems when that is too wide for its column the text gets truncated to fit.
Listview control helpfully displays a hint containing the full text when you hover the mouse over the item only. Usually that is a default behavior of Listview. But how can we show hints when we move mouse over Subitems then following are some tricks.
The list view control displays the info tip if it has the LVS_EX_INFOTIP extended style flag set. To inhibit the tips we need to remove the flag. Here is a little routine to do the job:
uses
CommCtrl;
.....
procedure TForm1.RemoveDefaultInfoTips(const LV: TListView);
begin
ListView_SetExtendedListViewStyle(
LV.Handle,
ListView_GetExtendedListViewStyle(LV.Handle) and not LVS_EX_INFOTIP
);
end;
Note* that this method has no effect when the list view style is vsIcon: the tool tip is still displayed. To use the routine just call it from somewhere convenient, say a form's OnCreate event handler, as pass it a reference to the list view control where you want to disabled the hints, e.g.:
procedure TForm1.FormCreate(Sender: TObject);
beginRemoveDefaultInfoTips(ListView1);
end;
Implement Listview.onmousemove event to show formated hint as per our requirement.
procedure TForm1.ListView1MouseMove(Sender: TObject;
Shift: TShiftState; X, Y: Integer);
var
ptPos: TPoint;
lvItem : TListItem;
lvHitInfo: TLVHitTestInfo;
sHint: string;
begin
if ListView1.ViewStyle=vsReport then
begin
ptPos := ListView1.ScreenToClient(Mouse.CursorPos) ;
lvItem := ListView1.GetItemAt(ptPos.X, ptPos.Y) ;
if assigned(lvItem) then
begin
ListView1.Hint := '';
Application.CancelHint;
end
else // To show multiline hint for Listview subitems //
begin
FillChar(lvHitInfo, SizeOf(lvHitInfo), 0) ;
lvHitInfo.pt := ptPos;
if -1 <> ListView1.Perform(LVM_SUBITEMHITTEST, 0, LParam(@lvHitInfo)) then
begin
sHint:=Trim(ListView1.Items[lvHitInfo.iItem].SubItems[lvHitInfo.iSubItem-1]);
ListView1.Hint := WrapText(sHint, 60);
Application.ActivateHint(Mouse.CursorPos) ;
end
else
begin
ListView1.Hint := '';
Application.CancelHint;
end;
end;
end;
end;
how can I get the sum of a listview column...
ReplyDelete