Solved

listview columns layout / save layout and load it as well

Posted on 2004-03-22
10
387 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

910 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now