'C# ObjectListView TreeListView - How to highlight items only in TreeViewList other than using ModelFilter
I'm using ObjectListView.Official.2.9.1.nupkg
http://objectlistview.sourceforge.net/cs/index.html
Here is my sample code: C# ObjectListView TreeListView add multi-level Nodes
Currently I'm using ModelFilter and the problem was when i perform Collapse, the nodes went missing.
Explanation: http://objectlistview.sourceforge.net/cs/filtering.html#filtering-and-treelistviews
private void txtSearchInTable_TextChanged(object sender, EventArgs e)
{
try
{
if (txtSearchInTable.Text == "")
{
this.treeListView1.ResetColumnFiltering();
treeListView1.ExpandAll();
}
else
{
this.treeListView1.ModelFilter = TextMatchFilter.Contains(this.treeListView1, txtSearchInTable.Text);
this.treeListView1.Refresh();
}
}
catch (Exception ex)
{
MessageBox.Show(ex + "", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Is there any function for search and highlight text only while keeping everything visible?
Update:
TextMatchFilter filter1 = TextMatchFilter.Contains(treeListView1, txtSearchInTable.Text);
filter1.Columns = new[] { this.olvColumn1, this.olvColumn2, this.olvColumn3, this.olvColumn4 };
treeListView1.DefaultRenderer = new HighlightTextRenderer(filter1);
Using DefaultRenderer it's work, but why only first column "olvColumn1" cannot be highlighted? even I set olvColumn1.Searchable = true;
Solution 1:[1]
You can use HighlightTextRenderer to highlight and optionally filter items.
As pointed out by Dialecticus in one of the comments, the first column of a TreeListView (in contrast to ObjectListView) requires special handling.
Quote from here:
On a TreeListView, the DefaultRenderer is used for all columns except the first. The first column is drawn by the TreeColumnRenderer, which has to be an instance of TreeRenderer. TreeRenderer already supports text highlighting. You just need to change the Filter property.
So your code just needs to add a single line to update the Filter property:
TextMatchFilter filter = new TextMatchFilter(this.treeListView1, toolStripTextBox1.Text);
this.treeListView1.DefaultRenderer = new HighlightTextRenderer(filter);
this.treeListView1.TreeColumnRenderer.Filter = filter;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |

