Title: inline-editing with a TTreeView
Question: how to do inline-editing with a ttreeview
Answer:
It is much easier than you might think.
I give you the code, the dfm and the dpr....
Omer Yasar Can omercan@home.nl
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, ExtCtrls, StdCtrls, Menus;
type
TForm1 = class(TForm)
Panel1: TPanel;
TreeView1: TTreeView;
StatusBar1: TStatusBar;
Edit1: TEdit;
procedure TreeView1Editing(Sender: TObject; Node: TTreeNode;
var AllowEdit: Boolean);
procedure Edit1KeyPress(Sender: TObject; var Key: Char);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
Treeview1.FullExpand; //optional
end;
{------------------------------------------------------------------------------}
procedure TForm1.TreeView1Editing(Sender: TObject; Node: TTreeNode;
var AllowEdit: Boolean);
var
NodeRect:TRect;
begin
with Node do begin
NodeRect:=DisplayRect(True);
Edit1.Top := NodeRect.Top; Edit1.Left := NodeRect.Left;
Edit1.text := Text;
Edit1.Visible := true;
Edit1.SetFocus;
end;
end;
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if NOT ( key in [ 'A'..'Z', 'a'..'z', '0'..'9', ' ', chr(13), #8 ] )
//here's the advantage: you define what the user should enter!
then key := #0
else if key = chr(13) then begin
TreeView1.Selected.Text := Edit1.Text;
Edit1.visible := false;
TreeView1.SetFocus; //optional
end;
end;
end.
the dfm-file:
object Form1: TForm1
Left = 468
Top = 363
Width = 244
Height = 161
Caption = 'Exampe fuo TTreeView'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object Panel1: TPanel
Left = 0
Top = 0
Width = 236
Height = 115
Align = alClient
BevelInner = bvLowered
Caption = 'Panel1'
TabOrder = 0
object TreeView1: TTreeView
Left = 2
Top = 2
Width = 232
Height = 111
Align = alClient
Indent = 19
TabOrder = 0
OnEditing = TreeView1Editing
Items.Data = {
02000000270000000000000000000000FFFFFFFFFFFFFFFF0000000002000000
0E496E6C696E652065646974696E671F0000000000000000000000FFFFFFFFFF
FFFFFF000000000000000006537562315F611F0000000000000000000000FFFF
FFFFFFFFFFFF000000000000000006537562315F621E00000000000000000000
00FFFFFFFFFFFFFFFF000000000200000005526F6F74321F0000000000000000
000000FFFFFFFFFFFFFFFF000000000000000006537562325F611F0000000000
000000000000FFFFFFFFFFFFFFFF000000000000000006537562325F62}
end
object Edit1: TEdit
Left = 96
Top = 24
Width = 65
Height = 21
Color = clYellow
TabOrder = 1
Text = 'Edit1'
Visible = False
OnKeyPress = Edit1KeyPress
end
end
object StatusBar1: TStatusBar
Left = 0
Top = 115
Width = 236
Height = 19
Panels = item
Text = 'Dir:\TreeView_InlineEditing'
Width = 50
end
SimplePanel = False
end
end
the dpr-file:
program TreeView_InlineEditing;
uses
Forms,
Unit1 in 'Unit1.pas' {Form1};
{$R *.res}
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.