Solved

listview columns layout / save layout and load it as well

Posted on 2004-03-22
10
383 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This video discusses moving either the default database or any database to a new volume.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

706 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

20 Experts available now in Live!

Get 1:1 Help Now