treeview 查找节点_如何通过文本查找TreeView节点

灰太狼 2022-12-06 15:44 480阅读 0赞

treeview 查找节点

While developing Delphi applications using the TreeView component, you may bump into a situation where you need to search for a tree node given by only the text of the node.

在使用TreeView组件开发Delphi应用程序时,您可能会遇到这样的情况,即需要搜索仅由节点文本给出的树节点 。

In this article we’ll present you with one quick and easy function to get TreeView node by text.

在本文中,我们将为您提供一个快速简便的功能,以按文本获取TreeView节点。

一个Delphi的例子 ( A Delphi Example )

First, we’ll build a simple Delphi form containing a TreeView, a Button, CheckBox and an Edit component—leave all the default component names.

首先,我们将构建一个简单的Delphi表单,其中包含TreeView ,Button,CheckBox和Edit组件-保留所有默认的组件名称。

As you might imagine, the code will work something like: if GetNodeByText given by Edit1.Text returns a node and MakeVisible (CheckBox1) is true then select node.

就像您想象的那样,代码将类似于:如果Edit1.Text给出的GetNodeByText返回一个节点,而MakeVisible(CheckBox1)为true,则选择节点。

The most important part is the GetNodeByText function.

最重要的部分是GetNodeByText函数。

This function simply iterates through all the nodes inside the ATree TreeView starting from the first node (ATree.Items[0]). The iteration uses the GetNext method of the TTreeView class to look for the next node in the ATree (looks inside all nodes of all child nodes). If the Node with text (label) given by AValue is found (case insensitive) the function returns the node. The boolean variable AVisible is used to make the node visible (if hidden).

此函数仅从第一个节点(ATree.Items [0])开始迭代ATree TreeView内部的所有节点。 迭代使用TTreeView类的GetNext方法在ATree中查找下一个节点(在所有子节点的所有节点内部查找)。 如果找到带有由AValue给出的文本(标签)的节点(不区分大小写),则该函数返回该节点。 布尔变量AVisible用于使节点可见(如果隐藏)。

  1. function GetNodeByText
  2. (ATree : TTreeView; AValue:String;
  3. AVisible: Boolean): TTreeNode;var
  4. Node: TTreeNode;begin
  5. Result := nil;if ATree.Items.Count = 0 then Exit;
  6. Node := ATree.Items[0];while Node nil dobeginif UpperCase(Node.Text) = UpperCase(AValue) thenbegin
  7. Result := Node;if AVisible then
  8. Result.MakeVisible;
  9. Break;end;
  10. Node := Node.GetNext;end;end;

This is the code that runs the ‘Find Node’ button OnClick event:

这是运行“查找节点”按钮的OnClick事件的代码:

  1. procedure TForm1.Button1Click(Sender: TObject);var
  2. tn : TTreeNode;begin
  3. tn:=GetNodeByText(TreeView1,Edit1.Text,CheckBox1.Checked);if tn = nil then
  4. ShowMessage('Not found!')elsebegin
  5. TreeView1.SetFocus;
  6. tn.Selected := True;end;end;

Note: If the node is located the code selects the node, if not a message is displayed.

注意:如果找到该节点,则代码将选择该节点,否则将显示一条消息。

That’s it. As simple as only Delphi can be. However, if you look twice, you’ll see something is missing: the code will find the FIRST node given by AText.

而已。 就像只有Delphi一样简单。 但是,如果您两次查看,就会发现缺少一些内容:该代码将找到AText指定的FIRST节点。

翻译自: https://www.thoughtco.com/locate-treeview-node-by-text-4077859

treeview 查找节点

发表评论

表情:
评论列表 (有 0 条评论,480人围观)

还没有评论,来说两句吧...

相关阅读

    相关 PB TreeView 查找节点

    PowerBuilder提供的TreeView控件可以使我们以树形方式分层组织和显示数据,使程序的表现更为灵活,用户的操作更加方便。通常情况下,在TreeView控件中通过展开

    相关 JS节点查找笔记

    > 要想获得未曾拥有的东西,就必须付出未曾有过的努力。 习惯了JQ的方便DOM节点查询,或许你已经忘记了js原生的一些查询操作了吧,下面列出js的节点查询,方便要用到的时候能