Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If a listview row is empty, how do i skip and go to next entry?

Status
Not open for further replies.

simmo09

Programmer
Apr 22, 2009
82
GB
Hi, im trying to read the values of subitems[6] in a listview, if the value is empty how do i ignore reading it and just go the next subitem[6] that does contain a value??!

here is what i have so far, its to populate a teechart based on the value of the listview, im working with these values for test purposes, but it only reads the first 2 rows and stops after the last value:

procedure TfrmMain.UpdateWeightSeries;
var
i,j: Integer;
aStringList: TStringList;
begin
try
aStringList:= TStringList.Create;

with WeightSeries do
begin
Clear; //clear previous entries otherwise it will keep adding

{loop through each entry in the fitness table and:
-> get the value entered in all weight boxes
-> add to the fitness chart the number of weeks based on if weight has a value}
for i:= 0 to lvwFitnessTable.Items.Count do
begin
if lvwFitnessTable.Items.SubItems[6] <> '' then
begin
{add weight values to stringlist}
aStringList.Add(lvwFitnessTable.Items.SubItems[6]);
ShowMessage(lvwFitnessTable.Items.SubItems[6]); //doesnt read past 15.9 - it should skip the gap and read 15.8

{add to the chart, we can now count actual weeks via aStringList.Count}
Add(StrToFloat(lvwFitnessTable.Items.SubItems[6]), IntToStr(aStringList.Count), clNone);
end;
end;
end;
finally
aStringList.Free;
end;
end;
 
forgot to add, the test values are:

16
15.9
(empty)
15.8
(empty)
15.6
 
Try:
Code:
for i:= 0 to lvwFitnessTable.Items.Count [red]-1 [/red]do

Roo
Delphi Rules!
 
that wont work, its the same loop but 1 less value, i just tried it.

another way is if i can remove blank strings from a stringlist, i can work off that. how would i do that, trim() or something???

Thanks!!
 
Items.Count is 0 based. IE: If you have 4 items, then count = 4. They are: 0, 1, 2 & 3. There is no Item[4].

Therefore "Items.Count -1" is required, not optional.

I can't provide any further information without knowledge of what "WeightSeries" and "lvwFitnessTable" is.

Perhaps the problem is that the same mistake exists where "lvwFitnessTable" is being populated?

TIP: Delphi will trap these errors for you if Range checking is turned "On". {$R+}

"Range checking - Checks that array and string subscripts are within bounds. Corresponds to {$R}.
HTH

Roo
Delphi Rules!
 
To answer your question:
Code:
aStringList.Delete(2);
Will delete the second line. REMEMBER: That is Item[1]! Because the first one is "0".

From Delphi Help:
Delphi syntax:

procedure Delete(Index: Integer); override;

Description

Call Delete to remove a single string from the list. If an object is associated with the string, the reference to the object is removed as well. Index gives the position of the string, where 0 is the first string, 1 is the second string, and so on.

Roo
Delphi Rules!
 
Also, if you're deleting, then the Count will decrement

for example

for i := 0 to list.count-1 do
if <cond> then
list.delete(i);

This would soon cause an error. If deleting, run through the list backwards first to clean it up, eg

for i := list.count-1 downto 0 do
if <cond> then
list.delete(i);


www.radbmx.co.uk
 
Sorry my last post isn't completely correct. You would actually miss records if you're deleting and going forwards through the loop.

www.radbmx.co.uk
 
to correct lucielastic's solution:

Code:
 index := List.Count;
 while index > 0 do
  begin
   Dec(index);
   if <cond> then
    list.delete(index);
  end;

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
now to answer the OP's question:

it seems to me that the empty lv lines don't have subitems so the line "if lvwFitnessTable.Items.SubItems[6] <> '' " will bomb out with an av.

may it's better to first check IF there are subitems

for example:

Code:
if lvwFitnessTable.Items[i].SubItems.Count > 6 then
 begin
  if lvwFitnessTable.Items[i].SubItems[6] <> '' then

clear?

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
for syntax:
for counter:= initialValue to finalValue do statement
or
for counter:= initialValue downto finalValue do statement

where
[ul][li]counter is a local variable (declared in the block containing the for statement) of ordinal type, without any qualifiers.[/li]
[li]initialValue and finalValue are expressions that are assignment-compatible with counter.[/li]
[li]statement is a simple or structured statement that does not change the value of counter.[/li][/ul]
...meaning don't do anything within a for loop that will alter the value of finalValue.



The A correct method (in a loop) would be:
Code:
  i:= 0;
  repeat
    if aStringList[i] = '(empty)' then
      aStringList.Delete(i)
    else
      inc(i);
  until (i = aStringList.Count) or (aStringList.Count = 0);
Tested code: D7E w/ WXP-SP3

The above previous post (as quoted) syntax for "TStringList.Delete" from Delphi Help appears to be a bit misleading.



Roo
Delphi Rules!
 
you got it spot on whos your daddy, much appreciated ;)

as for what the program is, im just writing a little tool to monitor progress for weight loss/weight gain, muscle building etc and show it on the chart.

Thanks everyone, and thanks again whos your daddy!
 
roo,
I can't help it but I do find my while do syntox so much cleaner :)

simmo09,
glad I could help!

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
oh about my nickname,

it is in fact: who's her daddy? (tribute to my daughter)

Cheers!

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top