Link to home
Start Free TrialLog in
Avatar of sfern
sfern

asked on

Read Listbox items (Urgent)

Hi all,

I how a listbox that reads data from a log file.

The log file looks something like this: peter;football;match;05/09/05;13:00:02;Arsenal;10/11;200.00;

My problem is that i need the loop to be able to read the text from any of the different columns or subitems.

Example: If i wanted to check if the last subitem had a number equal or greater than then example provided , which is 200.00 i need to be able to do a showmessage(last Subitem+whatever iit is+ is equal or higher than the amount specified)

Basically i need to be able to read and display items and subitems, bit not the integer value, i need to display the text on it.

Thanks

SFern

Avatar of calinutz
calinutz
Flag of Romania image

So your problem is reading the file?
>>My problem is that i need the loop to be able to read the text from any of the different columns or subitems
Or reading a ListBox?
>>Question Title: Read Listbox items (Urgent)


Could you rephrase your question and be more explicit? Maybe a little more organized. Specify the steps that your application is crossing and what is it doing in every step and *where do you need help?.

I think he means a listview as he talked about coumns or subitems no?
If it is a listview then the code below will loop through all columns and items of a listview

var
  iRow, iCol: Integer;
begin
  For iRow := 0 to ListView.Items.Count - 1 do
    For iCol := 0 to ListView.Columns.Count - 1 do
    begin
      if iCol = 0 then
        Value := ListView.Items[iRow].Caption
      else
        Value := ListView.Items[iRow].SubItems[iCol-1]
    end
end;
I could be wrong  :o)
Avatar of kretzschmar
?

you can load one line into a stringlist via the commtext- or delimitedtext-property,
just place the delimeter ';' into the associated property before

this stringlist you can then assign to your listbox (if needed)

you can compare values via index

is this for what you are after?

meikl ;-)
Avatar of sfern
sfern

ASKER

OK sorry about that,

My program has 3 listboxes.

Listbox1: reads a log file.

Example Log file contents: peter;football;match;05/09/05;13:00:02;Arsenal;10/11;200.00;

There will be more than Line in the log file.

So Listbox1 loads the contents of the log file.

--------

There is a setting in say Edit1 - when contains the maximum currency allowed, which is contained on the last subitem of the listbox.

So the loops automatically reads and checks if the number is equal or higher and if it is it will perform and action and copy itself the Listbox2 if not then the line is deleted and it will check the number line if any.

I WILL UPGRADE MY POINTS FOR A WORKING EXAMPLE.

Thanks

SFern.
You could load your log file into a listview instead which would be a lot easier to work with.
You could load the file into a stringlist and populate a listview from there.
The code below assumes that the last character in the string is a (;)

var
  iLoop, iIdx, iCol: Integer;
  sWrkStr: String;
  lsti: TListItem;
begin
  StringList.LoadFromFile('c:\YourFile');

  For iLoop := 0 to StringList.Count - 1 do
  begin
    // reset
    iCol := 0;
    // add a listview item
    lsti := TListView.Items.Add
    // grab a string to work with
    sWrkStr := StringList[iLoop];
    // with the listitem
    with lsti do
    begin
      // loop through the string
      while Trim(sWrkStr) <> '' do
      begin
        // get index of semi colon
        iIdx := Pos(';', sWrkStr);
        // check what column dealing with
        if iCol = 0 then
          // get first semi colon (;) for the caption of the item
          lsti.Caption := Copy(sWrkStr, 0, iIdx-1)
        else
          // its a subitem
          lsti.Subitems.Add( Copy(sWrkStr, 0, iIdx-1) );
        // strip off the word we just used
        sWrkStr := Copy(sWrkStr, iIdx+1, Length(sWrkStr);
        // move column
        Inc(iCol)
      end;
    end;
  end;
The above example would be much easier to reference column values.
If you need more advice on using a listview instead and how to reference values and how to set up the properties of the listview then please ask
ASKER CERTIFIED SOLUTION
Avatar of Mike Littlewood
Mike Littlewood
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sfern

ASKER

hi mikelittlewood,

Just getting one problem,

I'm talking about your second example.

The lines not send to Listbox2 and not deleted, so i tried:

ListBox1.Items.Delete(0); but i get Index out of Bounds error.

BTW: Poing have been upgraded :o)
Avatar of sfern

ASKER

Even without the line i added ListBox1.Items.Delete(0);

i get index oout of bounds :o(

Avatar of sfern

ASKER

REF: Even without the line i added ListBox1.Items.Delete(0);
i get index oout of bounds :o(

FORGET THAT LAST COMMENT!


It's working now, just going to try to clear the listbox1

Avatar of sfern

ASKER

Working now.

Thanks a lot for your help :o)

Sfern.
Glad I could help
looks like I'm late on this one, , , LOL
anyway here's some code that will get each item from the text of a line in the list box in the TextSubItem( ) function -


type
  TItemID = (iiName,iiSport,iiMatch,iiDate,iiTime,iiArse,iiOdds,iiCash);
  // one ItemID for all of the 8 items

  TForm3 = class(TForm)




function TextSubItem(Item: TItemID; const Text: String): String;
var
i: TItemID;
sPos: Integer;
pPos: PChar;
begin
Result := ';'; // ERROR result
// this can return an empty string
pPos := PChar(Text);
for i := iiSport to Item do
  begin
  pPos := StrScan(pPos, ';');
  if pPos = nil then Break;
  Inc(pPos);
  end;
if pPos <> nil then
  begin
  sPos := pPos-PChar(Text)+1;
  pPos := StrScan(pPos, ';');
  if pPos = nil then Exit;
  Result := Copy(Text, sPos, (pPos-PChar(Text)+1)-sPos);
  end;

end;

procedure TForm3.sbut_LoadFileClick(Sender: TObject);
var
ReS: String;

  procedure AddListBox;
  begin
  if ReS <> ';' then
  ListBox2.Items.Add(ReS)
  else
  ListBox2.Items.Add('ERROR');
  end;


begin
ListBox1.Items.LoadFromFile('E:\sports.log');
if ListBox1.Items.Count > 2 then
  begin
  ReS := TextSubItem(iiCash, ListBox1.Items[0]);
  AddListBox;
  ReS := TextSubItem(iiName, ListBox1.Items[1]);
  AddListBox;
  ReS := TextSubItem(iiDate, ListBox1.Items[2]);
  AddListBox;
  end;
end;


 - - - - - - - - - - - - - - - - - - - - -

hope this can give you some ideas and help