?
Solved

Read Listbox items (Urgent)

Posted on 2005-03-08
15
Medium Priority
?
417 Views
Last Modified: 2010-04-05
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

0
Comment
Question by:sfern
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
15 Comments
 
LVL 11

Expert Comment

by:calinutz
ID: 13486245
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?.

0
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 13486550
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;
0
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 13486557
I could be wrong  :o)
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 27

Expert Comment

by:kretzschmar
ID: 13486589
?

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 ;-)
0
 

Author Comment

by:sfern
ID: 13486610
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.
0
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 13486693
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;
0
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 13486709
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
0
 
LVL 15

Accepted Solution

by:
mikelittlewood earned 800 total points
ID: 13486940
Ok seems you might prefer to use a listbox, so you need some code just to read the last value in a listbox.
Seems you have a total of 8 columns to work with, so the first 7 need to be discarded
var
  iLB, iSC: Integer;
  sWrkStr: String;
begin
  For iLB:= 0 to ListBox1.Items.Count - 1 do
  begin
    // current string
    sWrkStr := ListBox1.Items[iLB];
    // strip off the first 7 columns
    for iSC := 1 to 7 do
      // going through string stripping off the text you dont want to check
      sWrkStr := Copy(sWrkStr, Pos(';', sWrkStr)+1, Length(sWrkStr));
    // grab the last value of the current string
    sWrkStr := Copy(sWrkStr, 0, Length(sWrkStr)-1);
    // check the value (assuming float)
    if StrToFloat(sWrkStr) >= StrToFloat(Edit1.Text) then
      // copy string to second listbox
      ListBox2.Items.Add( ListBox1.Items[iLB] );
  end;
end;
0
 
LVL 17

Expert Comment

by:mokule
ID: 13486969
0
 

Author Comment

by:sfern
ID: 13487558
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)
0
 

Author Comment

by:sfern
ID: 13487671
Even without the line i added ListBox1.Items.Delete(0);

i get index oout of bounds :o(

0
 

Author Comment

by:sfern
ID: 13487705
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

0
 

Author Comment

by:sfern
ID: 13487769
Working now.

Thanks a lot for your help :o)

Sfern.
0
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 13487857
Glad I could help
0
 
LVL 34

Expert Comment

by:Slick812
ID: 13488665
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
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses
Course of the Month8 days, 8 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question