Solved

Make a Sub Item within listview

Posted on 2009-05-14
29
1,201 Views
Last Modified: 2012-05-07
Hi, I'd like to be able to Add a Sub Item under that of the parent Item.

   example
-------------
Parent Item |
+-Sub Item  |
Parent Item |
0
Comment
Question by:eNarc
  • 15
  • 9
  • 3
  • +1
29 Comments
 
LVL 18

Expert Comment

by:Johnjces
ID: 24388674
If you want to do sub items, have you tried a TreeView?

Much cleaner and a better visual way to do such.

john
0
 
LVL 1

Assisted Solution

by:KillerCode
KillerCode earned 200 total points
ID: 24388732
Virtual Treeview is the Best Solution i guess
http://www.soft-gems.net/index.php?option=com_content&task=view&id=30&Itemid=35
and don't forget "Virtual Treeview contributions" which contains Really good samples
The One You are Looking for is called "Minimal", Found Under
"VT Contributions\Examples\Delphi\Minimal"

or u can use this API code
http://www.swissdelphicenter.com/en/showcode.php?id=1782
which will make a good looking header as ur parent Item, but cant Collapse or Expand :(
0
 
LVL 5

Author Comment

by:eNarc
ID: 24388816
thought treeview is just uses 1 caption? does it have subitems too?
0
 
LVL 1

Expert Comment

by:KillerCode
ID: 24388974
Thats Just a name, its actually both TreeView and ListView at the same time, works as both, but Mainly looks like a TreeView with SubItems
0
 
LVL 18

Expert Comment

by:Johnjces
ID: 24389216
0
 
LVL 1

Expert Comment

by:KillerCode
ID: 24389324
i dont think eNarc meant this
she Meant SubItems and not TreeNodes
0
 
LVL 18

Expert Comment

by:Johnjces
ID: 24389364
I know... I am suggesting a treeview which has TreeNodes which can be or view subitems, sort of  like the example the asker provider.

It depends upon how the asker wants to view/show/use the data.

0
 
LVL 5

Author Comment

by:eNarc
ID: 24389572
yea like a listview with a listtree aspect :)

like this

+ Caption1 | SubItem[0] | SubItem[1] | SubItem[2] |
       Caption2 | SubItem[0] | SubItem[1] | SubItem[2] |


just like whats on this website :)

http://blog.sina.com.cn/s/blog_49e882430100cplh.html

u see them photos? just like that news reader :)
0
 
LVL 5

Author Comment

by:eNarc
ID: 24389593
0
 
LVL 1

Expert Comment

by:KillerCode
ID: 24389653
huh??
thats the link i gave u on my first post

its much better than using api, check the example i pointed to, its exactly what u need in ur other topic
0
 
LVL 1

Expert Comment

by:KillerCode
ID: 24390063
im sorry, but thats the best i can do
let me know if u like it and ill code u a simple exmaple
KC.PNG
0
 
LVL 5

Author Comment

by:eNarc
ID: 24390134
hi KillerCode, yea I know you gave me that link, I was just pointing out that might be something :)

and on that :)

I installed it and ran the code, and you know it looks so complicated even for a component standards.

now if only delphi we could allow a child in listview :) would be great :)

the API you speak of in groups, yep thats what I'm up to now, I think I've made another question on that how to imploment that group with the drag and drog.

http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_24410094.html
0
 
LVL 5

Author Comment

by:eNarc
ID: 24390147
Hi KillerCode, whats that image of? did you make that? that looks really nice :D how did you do that?
0
 
LVL 5

Author Comment

by:eNarc
ID: 24390158
I like it :D would you code me a simple example :) thankyou :)
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.

 
LVL 1

Expert Comment

by:KillerCode
ID: 24390195
sure, its 12:47 AM now, and im already working on ur other topic, so please ease abit on me
0
 
LVL 1

Expert Comment

by:KillerCode
ID: 24390206
Y cant i edit comments,
anyway, i was just kidding :D

just give me a bit more time thats all
0
 
LVL 5

Author Comment

by:eNarc
ID: 24390391
awwwww hehe ;) its 23:05 right now :) you are up late ;)
0
 
LVL 5

Author Comment

by:eNarc
ID: 24390401
I know it's a shame we can't edit our comments, though its all good :)
0
 
LVL 1

Expert Comment

by:KillerCode
ID: 24390598
sorry, i couldn't do this sorting thing, but i still can do u this other example tomorrow if u like it (the one i uploaded image of)
0
 
LVL 5

Author Comment

by:eNarc
ID: 24390755
ok :) that be great :D
0
 
LVL 37

Assisted Solution

by:Geert Gruwez
Geert Gruwez earned 300 total points
ID: 24392346
there is also the devexpress treeview / quatumgrid (commercial)
www.devexpress.com
0
 
LVL 5

Author Comment

by:eNarc
ID: 24400195
Hi Geert, that link looks good, where is the package located?
0
 
LVL 5

Author Comment

by:eNarc
ID: 24400203
Hi KillerCode, I'd love to see that example of that image :)
0
 
LVL 1

Assisted Solution

by:KillerCode
KillerCode earned 200 total points
ID: 24401388
Couldn't Upload Here, and Sorry for The Delay
http://www.plunder.com/Example-zip-download-5f067ea78a.htm
0
 
LVL 5

Author Comment

by:eNarc
ID: 24403482
wow that is so kewl :D
0
 
LVL 37

Accepted Solution

by:
Geert Gruwez earned 300 total points
ID: 24412228
0
 
LVL 5

Author Comment

by:eNarc
ID: 33271761
0
 
LVL 5

Author Comment

by:eNarc
ID: 33271778
Unit1.dfm
object Form1: TForm1

  Left = 0

  Top = 0

  Caption = 'ListView Groups'

  ClientHeight = 173

  ClientWidth = 192

  Color = clBtnFace

  Font.Charset = DEFAULT_CHARSET

  Font.Color = clWindowText

  Font.Height = -11

  Font.Name = 'Tahoma'

  Font.Style = []

  OldCreateOrder = False

  OnCreate = FormCreate

  OnDestroy = FormDestroy

  PixelsPerInch = 96

  TextHeight = 13

  object lvGroups: TListView

    AlignWithMargins = True

    Left = 1

    Top = 1

    Width = 190

    Height = 171

    Hint = 'Double click to expand / collapse sections'

    Margins.Left = 1

    Margins.Top = 1

    Margins.Right = 1

    Margins.Bottom = 1

    Align = alClient

    BevelOuter = bvNone

    BevelKind = bkTile

    BorderStyle = bsNone

    Columns = <

      item

        Caption = 'Title'

        Width = 75

      end

      item

        Caption = 'Value'

        Width = 75

      end>

    ColumnClick = False

    Font.Charset = DEFAULT_CHARSET

    Font.Color = clWindowText

    Font.Height = -11

    Font.Name = 'Tahoma'

    Font.Style = []

    ReadOnly = True

    RowSelect = True

    ParentFont = False

    ParentShowHint = False

    ShowHint = True

    SmallImages = listViewImages

    TabOrder = 0

    ViewStyle = vsReport

    OnAdvancedCustomDrawItem = lvGroupsAdvancedCustomDrawItem

    OnDblClick = lvGroupsDblClick

  end

  object listViewImages: TImageList

    Left = 96

    Top = 64

    Bitmap = {

      494C010102000400040010001000FFFFFFFFFF10FFFFFFFFFFFFFFFF424D3600

      0000000000003600000028000000400000001000000001002000000000000010

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000008080

      8000808080008080800080808000808080008080800080808000808080008080

      8000000000000000000000000000000000000000000000000000000000008080

      8000808080008080800080808000808080008080800080808000808080008080

      8000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000008080

      8000000000000000000000000000000000000000000000000000000000008080

      8000000000000000000000000000000000000000000000000000000000008080

      8000000000000000000000000000000000000000000000000000000000008080

      8000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000008080

      8000000000000000000000000000000000000000000000000000000000008080

      8000000000000000000000000000000000000000000000000000000000008080

      8000000000000000000000000000000000000000000000000000000000008080

      8000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000008080

      8000000000000000000000000000000000000000000000000000000000008080

      8000000000000000000000000000000000000000000000000000000000008080

      8000000000000000000000000000000000000000000000000000000000008080

      8000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000008080

      8000000000000000000000000000000000000000000000000000000000008080

      8000000000000000000000000000000000000000000000000000000000008080

      8000000000000000000000000000000000000000000000000000000000008080

      8000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000008080

      8000000000000000000000000000000000000000000000000000000000008080

      8000000000000000000000000000000000000000000000000000000000008080

      8000000000000000000000000000000000000000000000000000000000008080

      8000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000008080

      8000000000000000000000000000000000000000000000000000000000008080

      8000000000000000000000000000000000000000000000000000000000008080

      8000000000000000000000000000000000000000000000000000000000008080

      8000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000008080

      8000000000000000000000000000000000000000000000000000000000008080

      8000000000000000000000000000000000000000000000000000000000008080

      8000000000000000000000000000000000000000000000000000000000008080

      8000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000008080

      8000808080008080800080808000808080008080800080808000808080008080

      8000000000000000000000000000000000000000000000000000000000008080

      8000808080008080800080808000808080008080800080808000808080008080

      8000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      0000000000000000000000000000000000000000000000000000000000000000

      000000000000000000000000000000000000424D3E000000000000003E000000

      2800000040000000100000000100010000000000800000000000000000000000

      000000000000000000000000FFFFFF00FFFFFFFF00000000FFFFFFFF00000000

      FFFFFFFF00000000FFFFFFFF00000000E00FE00F00000000EFEFEFEF00000000

      EFEFEEEF00000000EFEFEEEF00000000E82FE82F00000000EFEFEEEF00000000

      EFEFEEEF00000000EFEFEFEF00000000E00FE00F00000000FFFFFFFF00000000

      FFFFFFFF00000000FFFFFFFF0000000000000000000000000000000000000000

      000000000000}

  end

end

Open in new window

0
 
LVL 5

Author Comment

by:eNarc
ID: 33271779
Unit1.pas
unit Unit1;



interface



{



Group Items in Delphi's TListView Control



Mimicking List View Item Grouping with Expanding and Collapsing



http://delphi.about.com/od/vclusing/a/delphi-list-view-groups-expand-collapse-items.htm



}





uses

  Contnrs,

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, ImgList, ComCtrls;



type

  TGroupItem = class

  private

    fItems : TObjectList;

    fCaption: string;

    fListItem: TListItem;

    fExpanded: boolean;

    function GetItems: TObjectList;

  public

    constructor Create(const caption : string; const numberOfSubItems : integer);

    destructor Destroy; override;



    procedure Expand;

    procedure Collapse;



    property Expanded : boolean read fExpanded;

    property Caption : string read fCaption;

    property Items : TObjectList read GetItems;

    property ListItem : TListItem read fListItem write fListItem;

  end;



  TItem = class

  private

    fTitle: string;

    fValue: string;

  public

    constructor Create(const title, value : string);

    property Title: string read fTitle;

    property Value : string read fValue;

  end;





  TForm1 = class(TForm)

    lvGroups: TListView;

    listViewImages: TImageList;

    procedure FormCreate(Sender: TObject);

    procedure FormDestroy(Sender: TObject);

    procedure lvGroupsAdvancedCustomDrawItem(Sender: TCustomListView;

      Item: TListItem; State: TCustomDrawState; Stage: TCustomDrawStage;

      var DefaultDraw: Boolean);

    procedure lvGroupsDblClick(Sender: TObject);

  private

    procedure ClearListViewGroups;

    procedure FillListViewGroups;

  public

    { Public declarations }

  end;



var

  Form1: TForm1;



implementation



{$R *.dfm}

procedure TForm1.ClearListViewGroups;

var

  li : TListItem;

  qng : TGroupItem;

begin

  for li in lvGroups.Items do

  begin

    if TObject(li.Data) is TGroupItem then

    begin

      qng := TGroupItem(li.Data);

      FreeAndNil(qng);

    end;

  end;

  lvGroups.Clear;

end;



procedure TForm1.FillListViewGroups;



  procedure AddGroupItem(gi : TGroupItem);

  var

    li : TListItem;

  begin

    li := lvGroups.Items.Add;



    li.Caption := gi.Caption;

    li.ImageIndex := 1; //collapsed



    li.Data := gi;

    gi.ListItem := li; //link "back"

  end;

begin

  ClearListViewGroups;



  AddGroupItem(TGroupItem.Create('Group A', 3));

  AddGroupItem(TGroupItem.Create('Group B', 1));

  AddGroupItem(TGroupItem.Create('Group C', 4));

  AddGroupItem(TGroupItem.Create('Group D', 5));

end;



procedure TForm1.FormCreate(Sender: TObject);

begin

  FillListViewGroups;

end;



procedure TForm1.FormDestroy(Sender: TObject);

begin

  ClearListViewGroups;

end;



procedure TForm1.lvGroupsAdvancedCustomDrawItem(Sender: TCustomListView;

  Item: TListItem; State: TCustomDrawState; Stage: TCustomDrawStage;

  var DefaultDraw: Boolean);

begin

  //bold group items

  if TObject(item.Data) is TGroupItem then

  begin

    lvGroups.Canvas.Font.Style := lvGroups.Canvas.Font.Style + [fsBold];

  end;

end;



//handles TListView OnDblClick even

procedure TForm1.lvGroupsDblClick(Sender: TObject);

var

  hts : THitTests;

  gi : TGroupItem;

begin

  inherited;



  hts := lvGroups.GetHitTestInfoAt(lvGroups.ScreenToClient(Mouse.CursorPos).X, lvGroups.ScreenToClient(Mouse.CursorPos).y);



  if (lvGroups.Selected <> nil) then

  begin

    if TObject(lvGroups.Selected.Data) is (TGroupItem) then

    begin

      gi := TGroupItem(lvGroups.Selected.Data);



      if NOT gi.Expanded then

        gi.Expand

      else

        gi.Collapse;

    end;

  end;

end;





{$region 'TGroupItem'}



procedure TGroupItem.Collapse;

var

  li : TListItem;

begin

  if NOT Expanded then Exit;



  ListItem.ImageIndex := 1;

  fExpanded := false;



  li := TListView(ListItem.ListView).Items[ListItem.Index + 1];

  while (li <> nil) AND (TObject(li.Data) is TItem) do

  begin

    TListView(ListItem.ListView).Items.Delete(li.Index);

    li := TListView(ListItem.ListView).Items[ListItem.Index + 1];

  end;

end;



constructor TGroupItem.Create(const caption: string; const numberOfSubItems : integer);

var

  cnt : integer;

begin

  fCaption := caption;



  for cnt := 1 to numberOfSubItems do

  begin

    Items.Add(TItem.Create(caption + ' item ' + IntToStr(cnt), IntToStr(cnt)));

  end;

end;



destructor TGroupItem.Destroy;

begin

  FreeAndNil(fItems);

  inherited;

end;



procedure TGroupItem.Expand;

var

  cnt : integer;

  item : TItem;

begin

  if Expanded then Exit;



  ListItem.ImageIndex := 0;

  fExpanded := true;



  for cnt := 0 to -1 + Items.Count do

  begin

    item := TItem(Items[cnt]);

    with TListView(ListItem.ListView).Items.Insert(1 + cnt + ListItem.Index) do

    begin

      Caption := item.Title;

      SubItems.Add(item.Value);

      Data := item;

      ImageIndex := -1;

    end;

  end;

end;



function TGroupItem.GetItems: TObjectList;

begin

  if fItems = nil then fItems := TObjectList.Create(true);

  result := fItems;

end;

{$endregion}



{$region 'TItem' }



constructor TItem.Create(const title, value: string);

begin

  fTitle := title;

  fValue := value;

end;

{$endregion}



end.

Open in new window

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

919 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

12 Experts available now in Live!

Get 1:1 Help Now