Skip to main content

TreeList.ParseFindPanelText Event

Fires after the query in the find panel changes. Allows you to create a filter condition based on the query and specify how to highlight results in the control.

Namespace: DevExpress.XtraTreeList

Assembly: DevExpress.XtraTreeList.v24.2.dll

Declaration

[DXCategory("Data")]
public event EventHandler<ParseFindPanelTextEventArgs> ParseFindPanelText

Event Data

The ParseFindPanelText event's data class is DevExpress.Data.ParseFindPanelTextEventArgs.

Remarks

The FindPanelText event argument returns the query in the find panel. Based on the query, you can create a CriteriaOperator object that specifies a filter condition. To apply the condition to the control, use the following methods:

  • SetFindCriteria - applies the specified filter condition.
  • SetFindCriteriaAndHighlightedRangesGetterFromDisplayText - allows you to provide a function that highlights results in the control. The function accepts the text displayed in the processed cell and returns positions that should be highlighted.
  • SetFindCriteriaAndHighlightedRangesGetterFromDisplayTextAndFieldName - the function accepts the text displayed in the processed cell and the field name.

The Handled event argument should be set to true to prevent the default filter algorithm from being invoked.

Examples

The code below shows how to use a comma to split the search query into several queries.

image

using DevExpress.Data;
using DevExpress.Data.Filtering;

private void PropertyGridControl1_ParseFindPanelText(object sender, DevExpress.Data.ParseFindPanelTextEventArgs e) {
    if (string.IsNullOrWhiteSpace(e.FindPanelText)) return;
    List<string> criteria = new List<string>();
    foreach (string criteriaString in e.FindPanelText.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) {
        string str = criteriaString;
        if (criteriaString[0] == ' ')
            str = criteriaString.Remove(0, 1);
        criteria.Add(str);
    }

    if (criteria.Count < 2)
        return;

    string filterString = String.Empty;
    for (int i = 0; i < criteria.Count; i++) {
        filterString += "Contains([Caption], '" + criteria[i] + "')";
        if (i != criteria.Count - 1)
            filterString += " OR ";
    }

    CriteriaOperator _findCriteria = CriteriaOperator.Parse(filterString);
    e.SetFindCriteriaAndHighlightedRangesGetterFromDisplayTextAndFieldName(_findCriteria, 
        (x, y) => {
        var arr = new DisplayTextHighlightRange[criteria.Count()];
        for (int i = 0; i < criteria.Count(); i++) {
            arr[i] = new DisplayTextHighlightRange(x.IndexOf(criteria[i]), criteria[i].Length);
        }
        return arr;
    });
    e.Handled = true;
}

The code below shows how to search for an exact match with the query in a particular column.

using DevExpress.Data;
using DevExpress.Data.Filtering;

private void gridView1_ParseFindPanelText(object sender, DevExpress.Data.ParseFindPanelTextEventArgs e) {
    string searchField = "Country";
    CriteriaOperator criterion = new BinaryOperator(searchField, e.FindPanelText, BinaryOperatorType.Equal);
    e.Handled = true;
    e.SetFindCriteriaAndHighlightedRangesGetterFromDisplayTextAndFieldName(criterion, (displayText, fieldName) => {
        if (fieldName != searchField)
            return null;
        if (string.IsNullOrEmpty(displayText))
            return null;
        var indexOf = displayText.IndexOf(e.FindPanelText, StringComparison.CurrentCultureIgnoreCase);
        if (indexOf < 0)
            return null;
        return new DisplayTextHighlightRange(indexOf, e.FindPanelText.Length);
    });
}

For performance reasons, only the first occurrence of the search query is highlighted in a column.

image

The code below shows how to highlight all occurrences.

image

using DevExpress.Data;
using DevExpress.Data.Filtering;
using DevExpress.XtraGrid.Views.Grid;
using System;

private void gridView1_ParseFindPanelText(object sender, DevExpress.Data.ParseFindPanelTextEventArgs e) {
    GridView view = sender as GridView;
    CriteriaOperator _findCriteria = CriteriaOperator.Parse(string.Format("Contains([CompanyName],'{0}')", e.FindPanelText));
    e.SetFindCriteriaAndHighlightedRangesGetterFromDisplayTextAndFieldName(_findCriteria, (displayText, fieldName) => {
        if (fieldName != "CompanyName")
            return null;
        if (string.IsNullOrEmpty(displayText))
            return null;
        var indexOf = 0;
        int length = e.FindPanelText.Length;
        List<DisplayTextHighlightRange> res = new List<DisplayTextHighlightRange>();
        do {
            indexOf = displayText.IndexOf(e.FindPanelText, indexOf, StringComparison.CurrentCultureIgnoreCase);
            if (indexOf >= 0) {
                res.Add(new DisplayTextHighlightRange(indexOf, length));
                indexOf += length;
            }
        } while (indexOf >= 0);
        return res.ToArray();
    });
}
See Also