Reputation: 10886
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?
Upvotes: 2
Views: 1620
Reputation: 115
Perhaps the easiest way to store the order of the columns would be to define a ID for each as a meaningfull string, and store the list in the right order in the registry. For instance, let's suppose your columns were ordered like:
Name | First name | Age | Job title
Then the stored string in the registry could be:
"Name,FName,Age,JTitle"
To be stored in the appropriate registry entry, under the appropriate key (typically HCKU\SOFTWARE\MyApplication
, under the key ColumnOrder
for instance)
Upvotes: 0
Reputation: 5695
I suggest you inherit from Tlistview (or is there a TCustomListView) to create your own component, class helpers are nice but unofficial.
Upvotes: 0
Reputation: 53366
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.
Upvotes: 3