Solved

listview columns layout / save layout and load it as well

Posted on 2004-03-22
10
393 Views
Last Modified: 2010-04-05
Hi Experts

I have a listview with 6 static columns.
I want to add dynamic columns during runtime and then to save the new layout as the user name and then to call it again by clicking on the user name. so what i need is to customize the listview up to each user using the application

hope i made my self clear.

one more thing how can i export the listview items to an excel and vice versa

thanks all in advance
0
Comment
Question by:ibrobar
  • 3
  • 2
  • 2
10 Comments
 
LVL 17

Expert Comment

by:mokule
ID: 10652432
Hi
For Excel automation look there
http://www.djpate.freeserve.co.uk/AutoExcl.htm
0
 
LVL 17

Expert Comment

by:mokule
ID: 10652460
Store appropriate parameters in an INI file. For example sections for each user.
TIniFile
0
 

Expert Comment

by:rafamvc
ID: 10652658
"I want to add dynamic columns during runtime"

ListView1.Columns.Add;

" and then to save the new layout as the user name"
save to a file? db?

Maybe this help:
procedure TForm1.Button1Click(Sender: TObject);
var i,j: Integer;
    TempColumn : TListColumn;
begin
   //TempColumn := ListView1.Columns.Add;
   //TempColumn.Caption := 'New Column';
   //ListView1.Columns.Count;
   for i:=0 to ListView1.Columns.Count -1 do
   begin
     //Save where you want the columns captions
     ShowMessage(ListView1.Column[i].Caption);
   end;
   for i:=0 to ListView1.Items.Count -1 do
   begin
     ShowMessage(ListView1.Items.Item[i].Caption);
     for j:=0 to ListView1.Columns.Count -2 do
     //Caution: -2 because first column is the item itself
        ShowMessage(ListView1.Items.Item[i].SubItems[j]);
   end;
end;

"and then to call it again by clicking on the user name."
Explain a little more about where you will save and then we make the second part

" so what i need is to customize the listview up to each user using the application"
ok.

[]´s
Rafael Cardoso - Brazil
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Expert Comment

by:rafamvc
ID: 10652672
  //TempColumn := ListView1.Columns.Add;
   //TempColumn.Caption := 'New Column';
   //ListView1.Columns.Count;

ops forgot to explain:

this 3 lines set up new column.
0
 
LVL 22

Accepted Solution

by:
Ferruccio Accalai earned 125 total points
ID: 10652777
Ok my version:

To save and reload the listviews you could use the readcomponent and writecomponent procedures that are really useful...

To export to excel create a oleobject and fill it reading the listview

my example as follows:

{unit1.pas}

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls,comobj;

type
  TForm1 = class(TForm)
    ListView1: TListView;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  MemStr : TMemoryStream;
begin
  MemStr := TMemoryStream.Create;
  try
    {if you need to save just the structure without the content simply add
    ListView1.Items.Clear}
    MemStr.WriteComponent(ListView1);
    memSTr.SaveToFile('c:\ThisUserListView.dat');
  finally
    MemStr.Free;
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
MemStr : TMemoryStream;
begin
MemStr:= TMemoryStream.Create;
  try
    MemStr.LoadFromFile('c:\ThisUserListView.dat');
    MemStr.ReadComponent(ListView1);
  finally
    MemStr.Free;
  end;
end;

procedure TForm1.Button3Click(Sender: TObject);
var
Excel: Variant;
row,col:Integer;
begin
  try
    excel:=CreateOleObject('Excel.Application');
  except
    ShowMEssage('Excel not available');
    exit;
  end;
    excel.Visible:=False;
    Row:=1;
    excel.Workbooks.Add;
    for Col := 0 to ListView1.Columns.Count-1 do begin
      excel.Cells[row,Col + 1].Value:= ListView1.Columns.Items[col].Caption;
      excel.cells[row,Col+1].ColumnWidth := ListView1.Columns.Items[col].Width/5;
      end;
    try
    for row := 2 to ListView1.items.count+1 do
      for Col := 0 to ListView1.Columns.Count-1 do begin
        If Col = 0 then
          excel.Cells[row,Col + 1].Value:= (ListView1.Items[Row-2].caption)
        else
          excel.Cells[row,Col + 1].Value:= (ListView1.Items[Row-2].subitems[col-1]);
    end;
    Except
         MessageBox(0,'Warning, there was an error during the operation. Exportation aborted.','',0);
         excel.quit;
    end;
    excel.visible := true;
    excel := Unassigned;
end;

end.

{Unit1.dfm}

object Form1: TForm1
  Left = 192
  Top = 107
  Width = 507
  Height = 291
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object ListView1: TListView
    Left = 130
    Top = 72
    Width = 250
    Height = 150
    Columns = <
      item
        Caption = 'Title1'
      end
      item
        Caption = 'Title2'
      end
      item
        Caption = 'Title3'
      end
      item
        Caption = 'Title4'
      end>
    Items.Data = {
      910000000400000000000000FFFFFFFFFFFFFFFF030000000000000001310132
      0133013400000000FFFFFFFFFFFFFFFF03000000000000000132013501360137
      00000000FFFFFFFFFFFFFFFF0300000000000000013301380139023130000000
      00FFFFFFFFFFFFFFFF03000000000000000134013101320133FFFFFFFFFFFFFF
      FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF}
    TabOrder = 0
    ViewStyle = vsReport
  end
  object Button1: TButton
    Left = 138
    Top = 20
    Width = 75
    Height = 25
    Caption = 'Save List'
    TabOrder = 1
    OnClick = Button1Click
  end
  object Button2: TButton
    Left = 292
    Top = 20
    Width = 75
    Height = 25
    Caption = 'Load List'
    TabOrder = 2
    OnClick = Button2Click
  end
  object Button3: TButton
    Left = 390
    Top = 194
    Width = 75
    Height = 25
    Caption = 'To Excel'
    TabOrder = 3
    OnClick = Button3Click
  end
end
0
 

Expert Comment

by:rafamvc
ID: 10652907
wow, realy nice your code! ;) Solved another problem of mine ;)
tnks Ferruccio68!
[]´s
Rafael Cardoso
0
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 10652935
Rafael, just for curiosity: what was your problem? About the excel export or the component streaming? :)))
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
code issue 8 154
How to renew a Delphi rad-studio licence? 5 69
CheckListBox usage 3 71
FMX TEdit KeyUp handler detecting  "enter" key 4 13
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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

839 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