过滤后保留选定的treenodes

过滤树状图后保留选定的treenodes 我过滤树状视图。当检查节点时,我想保留已过滤的树视图中的已检查节点。当我删除过滤器时,仍然必须检查节点。 我做错了什么,过滤器工作,但我松了检查。过滤后保留选定的treenodes

List<string> ListCheckboxItems = new List<string>(); //create a list from the checkedlistbox itmes 

List<string> KeepSelectedItems = new List<string>(); //Create a list with the selected items

private void textBox1_TextChanged(object sender, EventArgs e) //new 24-12-2017

{

var filteredItems = ListCheckboxItems.Where(item => item.Contains(textBox1.Text));

List<string> FilterList = filteredItems.ToList();

treeViewFilter.Nodes.Clear(); //remove all nodes

foreach (object item in FilterList) //Fill it again with only the filtered items

{

treeViewFilter.Nodes.Add(item.ToString()); //new 24-12-2017

}

foreach (string keepitem in KeepSelectedItems) //Keep the items checked when filter starts

{

TreeNode[] arr = treeViewFilter.Nodes.Find(keepitem, true);

foreach (TreeNode s in arr)

{

treeViewFilter.SelectedNode = s;

s.Checked = true;

}

}

//check if empty the put the original list back

if (textBox1.Text == string.Empty) //park the checked items

{

foreach (string item in KeepSelectedItems)

{

TreeNode[] arr = treeViewFilter.Nodes.Find(item, true);

foreach (TreeNode s in arr)

{

treeViewFilter.SelectedNode = s;

s.Checked = true;

}

}

}

}

回答:

我发现了。 :

private void textBox1_TextChanged(object sender, EventArgs e) //new 24-12-2017 

{

var filteredItems = ListTreeViewItems.Where(item => item.Contains(textBox1.Text));

List<string> FilterList = filteredItems.ToList();

treeViewFilter.Nodes.Clear(); //remove all nodes

foreach (object item in FilterList) //Fill it again with only the filtered items

{

TreeNode aNode = new TreeNode(item.ToString()) { Name = item.ToString() }; //THIS LINE IS THE SOLUTION

treeViewFilter.Nodes.Add(aNode);

}

//zet de vinkjes weer terug

foreach (string keepitem in KeepSelectedItems) //Keep the items checked when filter starts

{

TreeNode[] arr = treeViewFilter.Nodes.Find(keepitem, true);

Logging.WriteToLog("", "treeViewFilter.Nodes.Find(keepitem, true); " + treeViewFilter.Nodes.Find(keepitem, true));

// Logging.WriteToLog("", "test " + test);

foreach (TreeNode s in arr)

{

treeViewFilter.SelectedNode = s;

s.Checked = true;

}

}

//check if empty the put the original list back

if (textBox1.Text == string.Empty) //park the checked items

{

foreach (string item in KeepSelectedItems)

{

TreeNode[] arr = treeViewFilter.Nodes.Find(item, true);

foreach (TreeNode s in arr)

{

treeViewFilter.SelectedNode = s;

s.Checked = true;

}

}

}

}

以上是 过滤后保留选定的treenodes 的全部内容, 来源链接: utcz.com/qa/265669.html

回到顶部