fabyola
asked on
Using ListView
I have a ListView component with it´s ViwStyle = vsReport. In it I have 3 Coloumns. Example:
With some values on only two columns, like:
Description | Value | Total
Keyboard 245
Mouse 130
CPU 10
I want to search the list view for a certain description and edit the column "Total".
For Example I need to search Mouse and put it´s total to 400.
Description | Value | Total
Keyboard 245
Mouse 130 400
CPU 10
Is there a way of doing this ? Or how can I solve it ? It can be with a diferent component.
With some values on only two columns, like:
Description | Value | Total
Keyboard 245
Mouse 130
CPU 10
I want to search the list view for a certain description and edit the column "Total".
For Example I need to search Mouse and put it´s total to 400.
Description | Value | Total
Keyboard 245
Mouse 130 400
CPU 10
Is there a way of doing this ? Or how can I solve it ? It can be with a diferent component.
Does it mean that you want to search for multiple rows with Description Mouse ? ( because 130 is not equal of 400 )
procedure TForm1.Button1Click(Sender : TObject);
var
i: integer;
begin
for i := 0 to ListView1.Items.Count - 1 do
begin
if ListView1.Items[i].Caption = 'Mouse' then
begin
if ListView1.Items[i].SubItem s.Count = 1 then
ListView1.Items[i].SubItem s.Add('400 ')
else if ListView1.Items[i].SubItem s.Count > 1 then
ListView1.Items[i].SubItem s[1] := '700';
end;
end;
end;
var
i: integer;
begin
for i := 0 to ListView1.Items.Count - 1 do
begin
if ListView1.Items[i].Caption
begin
if ListView1.Items[i].SubItem
ListView1.Items[i].SubItem
else if ListView1.Items[i].SubItem
ListView1.Items[i].SubItem
end;
end;
end;
unit Unit1_Q_21090603;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls;
type
TForm1 = class(TForm)
ListView: TListView;
btnTotal: TButton;
ComboBox: TComboBox;
Memo: TMemo;
btnClearMemo: TButton;
procedure btnTotalClick(Sender: TObject);
procedure btnClearMemoClick(Sender: TObject);
private { Private declarations }
public { Public declarations }
function Get_Total(Descr: string): Boolean;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function TForm1.Get_Total(Descr: string): Boolean;
var
Ok: Boolean;
I: Integer;
J: Integer;
K: Integer;
L: Integer;
M: Integer;
N: Integer;
S: string;
begin
Ok := True;
try
try
J := 0;
K := -1;
N := 0;
for I := 0 to ListView.Items.Count-1 do
begin
if (ListView.Items[I].Caption = Descr) then
begin
Inc(J);
if (J=1) then
K := I;
S := ListView.Items[I].SubItems [0];
Val(S, M, L);
if (L=0) then
N := N + M;
end;
end;
S := IntToStr(N);
if (K>=0) then
ListView.Items[K].SubItems .Add(S);
except
Ok := False;
end;
finally
Result := Ok;
end;
end;
procedure TForm1.btnTotalClick(Sende r: TObject);
var
S: string;
begin
if Get_Total(ComboBox.Text) then
S := 'Total about ' + ComboBox.Text + ' is OK.'
else
S := 'Total about ' + ComboBox.Text + ' failed';
Memo.Lines.Add(S);
end;
procedure TForm1.btnClearMemoClick(S ender: TObject);
begin
Memo.Clear;
end;
end.
//........
object Form1: TForm1
Left = 224
Top = 128
BorderIcons = [biSystemMenu, biMinimize]
BorderStyle = bsSingle
Caption = 'Get Total'
ClientHeight = 446
ClientWidth = 504
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
Position = poDesktopCenter
PixelsPerInch = 96
TextHeight = 13
object ListView: TListView
Left = 16
Top = 32
Width = 304
Height = 400
Columns = <
item
Caption = 'Description'
Width = 128
end
item
Caption = 'Value'
Width = 64
end
item
Caption = 'Total'
Width = 80
end>
GridLines = True
Items.Data = {
C50000000600000000000000FF FFFFFFFFFF FFFF010000 0000000000 084B6579
626F6172640332343500000000 FFFFFFFFFF FFFFFF0100 0000000000 00054D6F
7573650331333000000000FFFF FFFFFFFFFF FF01000000 0000000003 43505502
313000000000FFFFFFFFFFFFFF FF01000000 0000000005 4D6F757365 03313435
00000000FFFFFFFFFFFFFFFF01 0000000000 0000054D6F 7573650331 32350000
0000FFFFFFFFFFFFFFFF010000 0000000000 0343505502 3338FFFFFF FFFFFFFF
FFFFFFFFFF}
TabOrder = 0
ViewStyle = vsReport
end
object btnTotal: TButton
Left = 208
Top = 4
Width = 80
Height = 25
Caption = 'Total'
TabOrder = 1
OnClick = btnTotalClick
end
object ComboBox: TComboBox
Left = 16
Top = 6
Width = 152
Height = 21
Style = csDropDownList
ItemHeight = 13
ItemIndex = 0
TabOrder = 2
Text = 'Keyboard'
Items.Strings = (
'Keyboard'
'Mouse'
'CPU')
end
object Memo: TMemo
Left = 320
Top = 32
Width = 160
Height = 400
ReadOnly = True
TabOrder = 3
end
object btnClearMemo: TButton
Left = 352
Top = 4
Width = 75
Height = 25
Caption = 'Clear'
TabOrder = 4
OnClick = btnClearMemoClick
end
end
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls;
type
TForm1 = class(TForm)
ListView: TListView;
btnTotal: TButton;
ComboBox: TComboBox;
Memo: TMemo;
btnClearMemo: TButton;
procedure btnTotalClick(Sender: TObject);
procedure btnClearMemoClick(Sender: TObject);
private { Private declarations }
public { Public declarations }
function Get_Total(Descr: string): Boolean;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function TForm1.Get_Total(Descr: string): Boolean;
var
Ok: Boolean;
I: Integer;
J: Integer;
K: Integer;
L: Integer;
M: Integer;
N: Integer;
S: string;
begin
Ok := True;
try
try
J := 0;
K := -1;
N := 0;
for I := 0 to ListView.Items.Count-1 do
begin
if (ListView.Items[I].Caption
begin
Inc(J);
if (J=1) then
K := I;
S := ListView.Items[I].SubItems
Val(S, M, L);
if (L=0) then
N := N + M;
end;
end;
S := IntToStr(N);
if (K>=0) then
ListView.Items[K].SubItems
except
Ok := False;
end;
finally
Result := Ok;
end;
end;
procedure TForm1.btnTotalClick(Sende
var
S: string;
begin
if Get_Total(ComboBox.Text) then
S := 'Total about ' + ComboBox.Text + ' is OK.'
else
S := 'Total about ' + ComboBox.Text + ' failed';
Memo.Lines.Add(S);
end;
procedure TForm1.btnClearMemoClick(S
begin
Memo.Clear;
end;
end.
//........
object Form1: TForm1
Left = 224
Top = 128
BorderIcons = [biSystemMenu, biMinimize]
BorderStyle = bsSingle
Caption = 'Get Total'
ClientHeight = 446
ClientWidth = 504
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
Position = poDesktopCenter
PixelsPerInch = 96
TextHeight = 13
object ListView: TListView
Left = 16
Top = 32
Width = 304
Height = 400
Columns = <
item
Caption = 'Description'
Width = 128
end
item
Caption = 'Value'
Width = 64
end
item
Caption = 'Total'
Width = 80
end>
GridLines = True
Items.Data = {
C50000000600000000000000FF
626F6172640332343500000000
7573650331333000000000FFFF
313000000000FFFFFFFFFFFFFF
00000000FFFFFFFFFFFFFFFF01
0000FFFFFFFFFFFFFFFF010000
FFFFFFFFFF}
TabOrder = 0
ViewStyle = vsReport
end
object btnTotal: TButton
Left = 208
Top = 4
Width = 80
Height = 25
Caption = 'Total'
TabOrder = 1
OnClick = btnTotalClick
end
object ComboBox: TComboBox
Left = 16
Top = 6
Width = 152
Height = 21
Style = csDropDownList
ItemHeight = 13
ItemIndex = 0
TabOrder = 2
Text = 'Keyboard'
Items.Strings = (
'Keyboard'
'Mouse'
'CPU')
end
object Memo: TMemo
Left = 320
Top = 32
Width = 160
Height = 400
ReadOnly = True
TabOrder = 3
end
object btnClearMemo: TButton
Left = 352
Top = 4
Width = 75
Height = 25
Caption = 'Clear'
TabOrder = 4
OnClick = btnClearMemoClick
end
end
procedure TForm1.FormCreate(Sender: TObject);
var ListItem : TListItem;
begin
// fill the list view
ListItem := TListItem.Create(ListView1 .Items);
// fill element 1
ListItem := ListView1.Items.Add;
ListItem.Caption := 'Keyboard';
ListItem.SubItems.Add('245 ');
ListItem.SubItems.Add('');
// element 2
ListItem := ListView1.Items.Add;
ListItem.Caption := 'Mouse';
ListItem.SubItems.Add('130 ');
ListItem.SubItems.Add('');
// element 3
ListItem := ListView1.Items.Add;
ListItem.Caption := 'CPU';
ListItem.SubItems.Add('10' );
ListItem.SubItems.Add('');
end;
procedure TForm1.Button1Click(Sender : TObject);
var
counter : Integer;
current_item : TListItem;
begin
for counter := 0 to ListView1.Items.Count - 1 do
begin
current_item := ListView1.Items[counter];
if UpperCase(current_item.Cap tion) = 'MOUSE' then
current_item.SubItems[1] := '400';
end;
end;
var ListItem : TListItem;
begin
// fill the list view
ListItem := TListItem.Create(ListView1
// fill element 1
ListItem := ListView1.Items.Add;
ListItem.Caption := 'Keyboard';
ListItem.SubItems.Add('245
ListItem.SubItems.Add('');
// element 2
ListItem := ListView1.Items.Add;
ListItem.Caption := 'Mouse';
ListItem.SubItems.Add('130
ListItem.SubItems.Add('');
// element 3
ListItem := ListView1.Items.Add;
ListItem.Caption := 'CPU';
ListItem.SubItems.Add('10'
ListItem.SubItems.Add('');
end;
procedure TForm1.Button1Click(Sender
var
counter : Integer;
current_item : TListItem;
begin
for counter := 0 to ListView1.Items.Count - 1 do
begin
current_item := ListView1.Items[counter];
if UpperCase(current_item.Cap
current_item.SubItems[1] := '400';
end;
end;
ASKER
But now when I try to read it´s value to do a sum, it reads with trash. For example: I found the Caption 'MOUSE' and added 400 to it. But when I return to it to sum 200 (to become 600) I need to read the value then do the addition. But when I read it it comes with trash like: '400' #$D#$A#$D. How can I take this trash out ?
you read the sum like this:
var
sum : Real;
begin
sum := FloatToStr(ListView1.Items [counter]. SubItems[2 ]);
var
sum : Real;
begin
sum := FloatToStr(ListView1.Items
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.