views:

660

answers:

2

How do I save a Tlistviews layout in Delphi 2007?

I have been asked to write some code to allow users to re-order columns in a TListview (well all TListviews in our application), I have the code working (by manipulating the columns index and setting width to zero to hide columns not needed) but now I need a way to save the state of the view when to form exits.

What is the best way to do this? I thought about serialization, but I dont need the data or sort order so that seamed a bit overkill to me.

Some things to ponder It needs to be on a per user basis It needs to be flexible, in-case we add a new column in the middle of the listview There is no garantee that the Column headding will be unique The listview name may not be unique across the application

Any ideas?

+1  A: 

If you only want to save and load a certain part of the data you can store it n an ini or xml file. General data can be written to the file. Columns is another problem. You need to find an unique identification for each column. The ini could be something like:

[Settings]

[Col_1]
position=1
width=500
title=hello world
align=left
sort=ascending

.. etc for more fields and more columns.

If you uses a listview helper class, you only need to write the code once:

TListviewHelper = class helper for TListView;
public
  procedure SaveToFile(const AFilename: string);
  procedure LoadFromFile(const AFileName: string);
end;

procedure TListviewHelper.SaveToFile(const AFilename: string);
var
  ini : TIniFile;
begin
  ini := TIniFile.Create(AFileName);
  try
    // Save to ini file
  finally
    ini.Free;
  end;
end;

procedure TListviewHelper.LoadFromFile(const AFileName: string);
var
  ini : TIniFile;
begin
  ini := TIniFile.Create(AFileName);
  try
    // Load from ini file
  finally
    ini.Free;
  end;
end;

If TListviewHelper is within scope, you have access to the extra methods.

Gamecat
A: 

I suggest you inherit from Tlistview (or is there a TCustomListView) to create your own component, class helpers are nice but unofficial.

Osama ALASSIRY