Ensure that every node in a TTreeView is unique (Views: 300)
Problem/Question/Abstract: 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: unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ComCtrls; const UM_EDITNODE = WM_USER; type TUmEditNode = record msg: Cardinal; node: TTreenode; unused: Longint; result: Longint; end; TForm1 = class(TForm) Edit1: TEdit; Edit2: TEdit; TreeView1: TTreeView; procedure TreeView1Edited(Sender: TObject; Node: TTreeNode; var S: string); private { Private declarations } procedure UMEditNode(var msg: TUMeditNode); message UM_EDITNODE; public { Public declarations } end; var Form1: TForm1; implementation {$R *.DFM} procedure TForm1.TreeView1Edited(Sender: TObject; Node: TTreeNode; var S: string); var n: TTreenode; begin n := (Sender as TTreeview).Items[0]; while n <> nil do begin if n <> Node then begin if AnsiCompareText(n.Text, S) = 0 then begin ShowMessage('Item already exists, use different text'); S := node.text; PostMessage(handle, UM_EDITNODE, integer(node), 0); break; end; end; n := n.GetNext; end; end; procedure TForm1.UMEditNode(var msg: TUMeditNode); begin if assigned(msg.node) then msg.node.edittext; end; end. |