关闭

TreeView、TreeNode练习,使用指针

8023人阅读 评论(0) 收藏 举报

DFM文件:

object Encodefrm: TEncodefrm
  Left = 240
  Top = 189
  Width = 373
  Height = 263
  Caption = #32534#30721
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  OnShow = FormShow
  PixelsPerInch = 96
  TextHeight = 13
  object lblEncode: TLabel
    Left = 152
    Top = 32
    Width = 201
    Height = 25
    AutoSize = False
    Caption = #35831#36755#20837#32534#30721
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -20
    Font.Name = 'MS Sans Serif'
    Font.Style = []
    ParentFont = False
  end
  object tvEncode: TTreeView
    Left = 8
    Top = 8
    Width = 129
    Height = 209
    Indent = 19
    TabOrder = 0
  end
  object edtEncode: TEdit
    Left = 152
    Top = 76
    Width = 201
    Height = 21
    ImeName = #20013#25991' ('#31616#20307') - '#25628#29399#25340#38899#36755#20837#27861
    TabOrder = 1
  end
  object grpEncodeControl: TGroupBox
    Left = 152
    Top = 129
    Width = 201
    Height = 73
    Caption = #25511#21046#25353#38062#36873#39033
    TabOrder = 2
    object btnEncodeAdd: TButton
      Left = 16
      Top = 24
      Width = 75
      Height = 25
      Caption = #28155#21152
      TabOrder = 0
      OnClick = btnEncodeAddClick
    end
    object btnEncodeDelete: TButton
      Left = 104
      Top = 24
      Width = 75
      Height = 25
      Caption = #21024#38500
      TabOrder = 1
      OnClick = btnEncodeDeleteClick
    end
  end
end

PAS文件:

unit frmEncode;

interface

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

type
  TEncodefrm = class(TForm)
    tvEncode: TTreeView;
    lblEncode: TLabel;
    edtEncode: TEdit;
    grpEncodeControl: TGroupBox;
    btnEncodeAdd: TButton;
    btnEncodeDelete: TButton;
    procedure btnEncodeAddClick(Sender: TObject);
    procedure btnEncodeDeleteClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Encodefrm: TEncodefrm;

implementation

{$R *.dfm}

//“添加节点”按钮点击事件处理过程

procedure TEncodefrm.btnEncodeAddClick(Sender: TObject);
var
  PTvn: ^TTreeNode;
begin
  //判断是否输入了要添加节点的内容
  if Trim(edtEncode.Text) = '' then
  begin
    ShowMessage('请输入要添加节点的内容');
    Exit;
  end;
  New(PTvn);
  PTvn^ := tvEncode.Selected;
  //添加子节点
  tvEncode.Items.AddChild(PTvn^,edtEncode.Text);
  Dispose(PTvn);
end;

//“删除节点”按钮点击事件处理过程

procedure TEncodefrm.btnEncodeDeleteClick(Sender: TObject);
var
  PTvn: ^TTreeNode;
begin
  New(PTvn);
  PTvn^ := tvEncode.Selected;
  //没有选中节点
  if PTvn^ = nil then
  begin
    ShowMessage('请选择要删除的节点');
    Dispose(PTvn);
    Exit;
  end;
  //选中的节点包含子节点
  if PTvn^.HasChildren then
  begin
    if MessageBox(Handle, '该节点包含子节点,确实要删除吗?', '提示信息',MB_YESNO) = idyes then
    begin
      //删除选中节点
      PTvn^.Delete;
    end;
  end
    //选中的节点不包含子节点
  else
  begin
    PTvn^.Delete;
  end;
  Dispose(PTvn);
end;

procedure TEncodefrm.FormCreate(Sender: TObject);
begin
  tvEncode.ReadOnly := True;
  tvEncode.HideSelection := False;
end;

procedure TEncodefrm.FormShow(Sender: TObject);
var
  CurrentNode: TTreeNode;
  PTvn: ^TTreeNode;
  i,j,k: Integer;
begin
  New(PTvn);
  //添加根节点
  CurrentNode := tvEncode.Items.AddChild(nil, '编码');
  PTvn^ := CurrentNode;
  //添加第一层节点
  for i := 1 to 5 do
  begin
    PTvn^ := tvEncode.Items.AddChild(PTvn^, Format('%s:%d', ['编码', i]));
    //添加第二层节点
    for j := 1 to i + 1 do
    begin
      PTvn^ := tvEncode.Items.AddChild(PTvn^, Format('%s:%d-%d', ['编码', i, j]));
      //添加第三层节点
      for k := 1 to j + 1 do
      begin
        tvEncode.Items.AddChild(PTvn^, Format('%s:%d-%d-%d', ['编码', i, j, k]));
      end;
      PTvn^ := PTvn^.Parent;
    end;
    PTvn^ := PTvn^.Parent;
  end;
  Dispose(PTvn);
end;

end.

截图:


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:89439次
    • 积分:1709
    • 等级:
    • 排名:千里之外
    • 原创:59篇
    • 转载:2篇
    • 译文:36篇
    • 评论:12条
    文章分类
    最新评论