Author: Tomas Rutkauskas
I want to make sure that every node in my TreeView is unique - like making a new
folder in Windows Explorer.
Answer:
It is a timing issue, it looks like you cannot set the node back to edit mode in
the onEdited event. So post a user message to the form and set the node to edit
mode in the handler for this message:
1 unit Unit1;
2
3 interface
4
5 uses
6 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
7 StdCtrls, ComCtrls;
8
9 const
10 UM_EDITNODE = WM_USER;
11 type
12 TUmEditNode = record
13 msg: Cardinal;
14 node: TTreenode;
15 unused: Longint;
16 result: Longint;
17 end;
18
19 TForm1 = class(TForm)
20 Edit1: TEdit;
21 Edit2: TEdit;
22 TreeView1: TTreeView;
23 procedure TreeView1Edited(Sender: TObject; Node: TTreeNode; var S: string);
24 private
25 { Private declarations }
26 procedure UMEditNode(var msg: TUMeditNode); message UM_EDITNODE;
27 public
28 { Public declarations }
29 end;
30
31 var
32 Form1: TForm1;
33
34 implementation
35
36 {$R *.DFM}
37
38 procedure TForm1.TreeView1Edited(Sender: TObject; Node: TTreeNode; var S: string);
39 var
40 n: TTreenode;
41 begin
42 n := (Sender as TTreeview).Items[0];
43 while n <> nil do
44 begin
45 if n <> Node then
46 begin
47 if AnsiCompareText(n.Text, S) = 0 then
48 begin
49 ShowMessage('Item already exists, use different text');
50 S := node.text;
51 PostMessage(handle, UM_EDITNODE, integer(node), 0);
52 break;
53 end;
54 end;
55 n := n.GetNext;
56 end;
57 end;
58
59 procedure TForm1.UMEditNode(var msg: TUMeditNode);
60 begin
61 if assigned(msg.node) then
62 msg.node.edittext;
63 end;
64
65 end.
|