Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 528
  • Last Modified:

TreeView Sorting ?

Hi,
How to sort TreeView nodes by text or data?
please, support with source code example.

Regards,
Alim
0
alim_maze
Asked:
alim_maze
1 Solution
 
FarajLYCommented:
try this
 TreeView1.SortType:=stText; Or  TreeView1.SortType:=stData;
0
 
pcsentinelCommented:
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
0
 
alim_mazeAuthor 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
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now