本文介绍了过滤用一个文本框树视图在C#WinForms应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个C#的winform一个TreeView。我希望能够通过一个搜索框添加一个搜索功能。
基本上在信用户类型(我猜的_TextChanged事件),我仅显示包含与已输入的字母...



我的树视图包含了总计超过15000节点,所以我需要的东西有点高性能53父节点。我建立从CSV我的TreeView,我加载到数据表,然后进行查询与相关子节点...



update来获取父节点



我有一个想法。
的最终目的是,当它被添加到ListView子节点上的用户doubleclicks。



我第一次在一个简单的列表,可以实现该搜索功能查看在那里我没有我的数据分为两类。



我的想法是,一旦用户开始在打字的东西,我关掉我的树状视图,并显示列表视图而不是...



我会尝试和实施,看看它给性能明智的......这种想法的任何批评都欢迎。


解决方案

最后,这是我做什么,它很适合我的要求。
我第一次让我的TreeView的副本,并存储到fieldsTreeCache。然后我清除fieldsTree。然后,我通过缓存搜索和添加包含到fieldsTree我的搜索参数的任何节点。这里请注意,一旦你搜索,你不再有显示父节点。你只是得到所有的终端节点。我这样做,因为如果不是我有两个选择:




  • 展开全部包含匹配孩子的父节点但随后缓慢,一个父可能有50个孩子是不是很大视觉。

  • 不展开父节点,但是,你只是得到的类别,而不是你要搜索的子节点。

     无效fieldFilterTxtBx_TextChanged(对象发件人,EventArgs五)
    {
    //块重画树,直到所有对象加载
    this.fieldsTree.BeginUpdate();
    this.fieldsTree.Nodes.Clear();
    如果(this.fieldFilterTxtBx.Text!=的String.Empty)
    {
    的foreach(树节点_parentNode在_fieldsTreeCache.Nodes)
    {
    的foreach(树节点_childNode在_parentNode .Nodes)
    {
    如果(_childNode.Text.StartsWith(this.fieldFilterTxtBx.Text))
    {
    this.fieldsTree.Nodes.Add((树节点)_childNode.Clone ());
    }
    }
    }
    }
    ,否则
    {
    的foreach(在this._fieldsTreeCache.Nodes树节点_node)
    {
    fieldsTree.Nodes.Add((树节点)_node.Clone());
    }
    }
    //使毕竟对象已添加
    this.fieldsTree.EndUpdate重绘树();
    }



I have a TreeView in my a C# winform. I would like to be able to add a search functionality through a search box.Basically as the user types in letters (I'm guessing on the _TextChanged event), I show only the nodes that contain childnodes with the inputed letters...

My TreeView contains 53 parent nodes for a total of over 15000 Nodes so I need something a bit performant. I build my TreeView from a csv that I load into a DataTable and then make queries on to get the Parent nodes with associated child nodes...

UPDATE

I have an idea.The final aim is that when a user doubleclicks on a child node it gets added to a listView.

I had first implemented this search function in a simple list view where I didn't separate my data into categories.

My idea is that once the user starts typing in things, I turn off my Tree view and show the list view instead...

I'll try and implement and see what it gives performance wise... Any critics on this idea are welcome.

解决方案

Finally this is what I did, it suits my requirements.I first make a copy of my TreeView and store into fieldsTreeCache. I then clear the fieldsTree. I then search through the cache and add any node containing my search parameter to the fieldsTree. Note here that once you search, you no longer have the parent nodes that show. You just get all of the end nodes. I did this because if not I had 2 choices:

  • Expand all the parent nodes containing childs that match but then it was slow and one parent might have 50 children which isn't great visually.
  • Not expand the parent nodes but then you just get the categories and not the children nodes that you're searching for.

    void fieldFilterTxtBx_TextChanged(object sender, EventArgs e)
    {
        //blocks repainting tree till all objects loaded
        this.fieldsTree.BeginUpdate();
        this.fieldsTree.Nodes.Clear();
        if (this.fieldFilterTxtBx.Text != string.Empty)
        {
            foreach (TreeNode _parentNode in _fieldsTreeCache.Nodes)
            {
                foreach (TreeNode _childNode in _parentNode.Nodes)
                {
                    if (_childNode.Text.StartsWith(this.fieldFilterTxtBx.Text))
                    {
                        this.fieldsTree.Nodes.Add((TreeNode)_childNode.Clone());
                    }
                }
            }
        }
        else
        {
            foreach (TreeNode _node in this._fieldsTreeCache.Nodes)
            {
                fieldsTree.Nodes.Add((TreeNode)_node.Clone());
            }
        }
        //enables redrawing tree after all objects have been added
        this.fieldsTree.EndUpdate();
    }
    

这篇关于过滤用一个文本框树视图在C#WinForms应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 08:53