We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

TreeView Sorting ?

alim_maze
alim_maze asked
on
Medium Priority
624 Views
Last Modified: 2010-04-05
Hi,
How to sort TreeView nodes by text or data?
please, support with source code example.

Regards,
Alim
Comment
Watch Question

Commented:
try this
 TreeView1.SortType:=stText; Or  TreeView1.SortType:=stData;
Sorry FarajLy I dont think thats right, the SortType determines when the data is sorted, not how it is sorted.

try this

******************UNIT CODE*********************
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    TreeView1: TTreeView;
    Button1: TButton;
    Button2: TButton;
    rbText: TRadioButton;
    rbData: TRadioButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure TreeView1Compare(Sender: TObject; Node1, Node2: TTreeNode;
      Data: Integer; var Compare: Integer);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
      TreeView1.Items.AddObject(nil,'Iiiiii',pointer(1));
      TreeView1.Items.AddObject(nil,'Eeeeee',pointer(5));
      TreeView1.Items.AddObject(nil,'Dddddd',pointer(6));
      TreeView1.Items.AddObject(nil,'Bbbbbb',pointer(8));
      TreeView1.Items.AddObject(nil,'Gggggg',pointer(3));
       TreeView1.Items.AddObject(nil,'Ffffff',pointer(4));
      TreeView1.Items.AddObject(nil,'Hhhhhh',pointer(2));
      TreeView1.Items.AddObject(nil,'Aaaaaa',pointer(9));
      TreeView1.Items.AddObject(nil,'Cccccc',pointer(7));
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
      TreeView1.Alphasort;
end;

procedure TForm1.TreeView1Compare(Sender: TObject; Node1, Node2: TTreeNode;
  Data: Integer; var Compare: Integer);
begin
      if rbText.Checked then
  begin
        if Node1.Text<Node2.Text then
          Compare:=-1
    else
          Compare:=1
  end
  else
        Compare:=integer(Node1.Data)-integer(Node2.data);
end;

end.


*************************FORM CODE*******************
object Form1: TForm1
  Left = 229
  Top = 173
  Width = 696
  Height = 480
  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 TreeView1: TTreeView
    Left = 20
    Top = 20
    Width = 245
    Height = 369
    Indent = 19
    SortType = stBoth
    TabOrder = 0
    OnCompare = TreeView1Compare
  end
  object Button1: TButton
    Left = 320
    Top = 36
    Width = 75
    Height = 25
    Caption = 'Load'
    TabOrder = 1
    OnClick = Button1Click
  end
  object Button2: TButton
    Left = 324
    Top = 120
    Width = 75
    Height = 25
    Caption = 'Sort'
    TabOrder = 2
    OnClick = Button2Click
  end
  object rbText: TRadioButton
    Left = 320
    Top = 68
    Width = 113
    Height = 17
    Caption = 'Text'
    Checked = True
    TabOrder = 3
    TabStop = True
  end
  object rbData: TRadioButton
    Left = 320
    Top = 88
    Width = 113
    Height = 17
    Caption = 'Data'
    TabOrder = 4
  end
end

**********************************************

regards

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
thank you pcsentinel for your extended answer ...
all I needed actually was this line : TreeView1.Alphasort; or  TreeView1.Items.AlphaSort(true);

I tried it, and it's ok.

Regards,
Alim
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.