8

我,(更多时间),当我单击树视图的项目时尝试执行操作,请参阅:

procedure TForm1.TreeView1Click(Sender: TObject);
begin

  if treeview1.Selected.AbsoluteIndex=1 then
  begin
    showmessage('selecionado');
  end; 

end;

如果用户单击树视图的索引 1,此代码将显示一条消息,问题如下:如果用户选择索引 1,将显示该消息,但之后,用户单击列表视图的空白区域该消息仍然执行,因为该项目仍处于选中状态。仅当用户单击相应项目时,如何使事件运行?

4

3 回答 3

9

不要使用OnClick,只要单击 就会发生TTreeView(不仅是单击节点时)。相反,使用TTreeView.OnChange事件,它传递给你节点:

procedure TForm3.TreeView1Change(Sender: TObject; Node: TTreeNode);
begin
  if Assigned(Node) then
    if Node.AbsoluteIndex = 1 then
      ShowMessage('selecionado');
end;
于 2013-05-25T22:51:29.007 回答
4
procedure TfClerks.tvHqClick(Sender: TObject);
var
  Node: TTreeNode;
begin
  with tvHq.ScreenToClient(Mouse.CursorPos) do
    Node := tvHq.GetNodeAt(X, Y);
  if Node = nil then
    Exit;
  // do something
end;
于 2016-06-11T13:38:06.447 回答
0
procedure TForm1.TreeView1Click(Sender: TObject);
begin
    if Assigned(TreeView1.Selected) then
    begin
        if SameText(TreeView1.Selected.Text, 'show form1') then
            ShowMessage('selecionado')
        else
           if SameText(TreeView1.Selected.Text, 'show form2') then
               ShowMessage('s22');
    end
end;
于 2014-11-06T13:07:19.650 回答