Link to home
Create AccountLog in
Avatar of PeterdeB
PeterdeBFlag for Netherlands

asked on

Listbox or Listview?

HI folks,

I need to display the contents of a text file, in either a listbox or listview. The listbox of listview should have 2 columns, for every line of the textfile holds two values. Furthermore I want to be able to edit those values seperately.

As an example here is the textfile:

31 Paul
23 Frank
63 Sarah

Now suppose I select the first item in the listbox, being 31 Paul. I would have a dialog pop up with 2 editboxes. One to edit 31 and another to edit Paul. After editing I then save it to the text file.

Who can help me with some sample code and advice on whether to us e the listbox or listview for this purpose?
If you need more info, please ask.

Thanks in advance,

Peter
SOLUTION
Avatar of kretzschmar
kretzschmar
Flag of Germany image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
ASKER CERTIFIED SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of PeterdeB

ASKER

Hi Kretzschmar,

Currently I'm trying to get it working with a TListview but althought it seems to load the textfile, I cannot see any text. Regardless of what file I load. It's also terribly slow, but when I used another approach which was a lot faster, I bumped into the same problem of not seeing anything.

Attached is my load procedure Btw

Dweep


procedure LoadListviewStrings(listview: TLIstview; const filename: string);
var
  sl, lineelements: TStringlist;
  i: Integer;
  item: TLIstItem;
begin
  Assert(Assigned(listview));
  if listview.ViewStyle <> vsReport then
    raise Exception.CreateFmt(eInvalidViewstyle, ['LoadListviewStrings',
      listview.name]);
  sl := TStringlist.Create;
  try
    sl.LoadFromFile(filename);
    lineelements := Tstringlist.Create;
    try
      for i := 0 to sl.count - 1 do
      begin
        lineelements.Clear;
        SplitString(sl[i], #9, lineelements);
        if lineelements.Count > 0 then
        begin
          item := listview.Items.Add;
          item.Caption := lineelements[0];
          lineelements.Delete(0);
          item.SubItems.Assign(lineelements);
        end;
      end;
    finally
      lineelements.free;
    end;
  finally
    sl.free
  end;
end;

Open in new window

Hi Stevebay,

I tried your code but when I try to edit the file and use the edit button it pops up a list index out of bounds error. Also I still cant get it to display antyhing in the listview although it seems to load the data.

Dweep
You have to add the columns in order to see the items and access the subitems

Note this code in the FormShow Event
procedure TForm1.FormShow(Sender: TObject);
begin
     ListView1.ViewStyle := vsReport;
     ListView1.Columns.Add.Caption := 'Number';
     ListView1.Columns.Add.Caption := 'Name';
     ListView1.ShowColumnHeaders := True;
end;
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Thanks again my friends! ;-)