Board index » delphi » Editing in TTreeView

Editing in TTreeView


2003-10-10 01:02:32 PM
delphi201
Hi,
I was wondering if there is a way to use the editing capabilities of the
TTreeView the way windows explorer does. What I want to be able to do is
determine if the name entered is valid before the edit box disappears, and
if is not, show an error message, return the name to what it was before, and
set the node back to edit mode so the user can enter a different name.
I tried using the OnEdited event, but it gets called somewhere after the
edit mode is finished, and before the node name is actually changed, which
is a catch-22 situation, if I try, for instance, to call EditText from this
event, the instruction is overridden by whatever code is executed after the
OnEdited event, and the results are unpredictable.
Thanks,
Carlos Galavis
 
 

Re:Editing in TTreeView

Carlos E. Galavis writes:
Quote
I tried using the OnEdited event, but it gets called somewhere after the
edit mode is finished, and before the node name is actually changed, which
is a catch-22 situation, if I try, for instance, to call EditText from this
event, the instruction is overridden by whatever code is executed after the
OnEdited event, and the results are unpredictable.
Maybe it is a bit much to replace the whole thing just for such a limitation, but it is not the only one and you can improve your application a lot by using the right tool. Try Virtual Treeview, which supports what you would expect from editing support and does many things *much* better than TTreeView. See my homepage for details.
Mike
--
www.delphi-gems.com
www.delphi-unicode.net
www.lischke-online.de
 

Re:Editing in TTreeView

In article <3f863d68$XXXX@XXXXX.COM>, Carlos E. Galavis writes:
Quote
I was wondering if there is a way to use the editing capabilities of the
TTreeView the way windows explorer does. What I want to be able to do is
determine if the name entered is valid before the edit box disappears, and
if is not, show an error message, return the name to what it was before, and
set the node back to edit mode so the user can enter a different name.

I tried using the OnEdited event, but it gets called somewhere after the
edit mode is finished, and before the node name is actually changed, which
is a catch-22 situation, if I try, for instance, to call EditText from this
event, the instruction is overridden by whatever code is executed after the
OnEdited event, and the results are unpredictable.

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.
Peter Below (TeamB)
Use the newsgroup archives :
www.mers.com/searchsite.html
www.tamaracka.com/search.htm
groups.google.com
www.prolix.be
 

Re:Editing in TTreeView

Sounds like a good idea, I will check it out.
Thanks