一个Treeview中给节点修改标题的问题,谢谢!(100分)

  • 主题发起人 主题发起人 balloy
  • 开始时间 开始时间
B

balloy

Unregistered / Unconfirmed
GUEST, unregistred user!
我有一个TreeView,我希望用户可以直接修改其中每个节点的名称,
但,为保持名称的有效性,规定名称不能为空,所以我在OnEdited事件中判断了节点名称的有效性,
但如果真为空时,我可以弹出一个对话框,并恢复原值,但却做不到将当前节点再次改为编辑状态。
可能说得有些不清楚,各位试试我的程序,将节点标题改为空,再试试Windows的资源管理器,
就知道我的意思了。源代码如下:

//Unit1.pas :
unit Unit1;

interface

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

type
TForm1 = class(TForm)
TreeView1: TTreeView;
procedure TreeView1Edited(Sender: TObject; Node: TTreeNode;
var S: String);
procedure TreeView1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.TreeView1Edited(Sender: TObject; Node: TTreeNode;
var S: String);
begin
S := Trim(S);
if ( S = '' ) then begin
MessageBox( Handle, '对不起,节点标题不能为空!', '警告', MB_OK or MB_ICONWARNING );
S := Node.Text;

// Node.EditText; //我以为可以这样写,却发现不行
end;
end;

procedure TForm1.TreeView1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if ( not TreeView1.Focused ) or TreeView1.IsEditing then
exit;

//快捷键表
case Key of
VK_F2:
TreeView1.Selected.EditText;
end;
end;

end.



//unit1.dfm:
object Form1: TForm1
Left = 358
Top = 183
Width = 238
Height = 480
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object TreeView1: TTreeView
Left = 0
Top = 0
Width = 230
Height = 453
Align = alClient
Indent = 19
TabOrder = 0
OnEdited = TreeView1Edited
OnKeyDown = TreeView1KeyDown
Items.Data = {
020000001B0000000000000000000000FFFFFFFFFFFFFFFF0000000000000000
0261311B0000000000000000000000FFFFFFFFFFFFFFFF000000000100000002
61321C0000000000000000000000FFFFFFFFFFFFFFFF00000000000000000361
3231}
end
end



谢谢!
 
好象不太好办,可能因为 OnEdited 是 EditText 方法中的一个事件。
应该从其它事件中进入编辑状态。

procedure TForm1.TreeView1Edited(Sender: TObject; Node: TTreeNode;
var S: String);
begin
S := Trim(S);
if ( S = '' ) then abort;
end;
 
[:D][:D]

我给你修改了,OK了,

给分哦:)

//程序如下 :

unit Unit1;

interface

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

type
TForm1 = class(TForm)
TreeView1: TTreeView;
procedure TreeView1Edited(Sender: TObject; Node: TTreeNode;
var S: string);
procedure TreeView1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.TreeView1Edited(Sender: TObject; Node: TTreeNode;
var S: string);
begin
S := Trim(S);
if (S = '') then
begin
MessageBox(Handle, '对不起,节点标题不能为空!', '警告', MB_OK or MB_ICONWARNING);
S := Node.Text;
KEYBD_EVENT(VK_F2, 0, 0, 0); //加上这句话就OK了,:)
//Node.EditText; //我以为可以这样写,却发现不行
end;
end;

procedure TForm1.TreeView1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if (not TreeView1.Focused) or TreeView1.IsEditing then
exit;
//快捷键表
case Key of
VK_F2:
TreeView1.Selected.EditText;
end;
end;

end.
[8D]
 
哈哈,真的可以了。你解决这个问题的方法很巧啊。
多谢!
 
呵呵:)
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
686
import
I
I
回复
0
查看
532
import
I
后退
顶部