I would like to highlight the text from a node of VirtualStringTree according to search criteria like example from bellow:
Any suggestion please?
Descentant of TBaseVirtualTree which is able to manage node captions on its own. TVirtualStringTree adds no new functionality to TCustomVirtualStringTree but is publicly available version and appears in the component palette.
Highlighting allows you to get snippets of search results with matching keywords highlighted. It helps to improve your application’s search experience. You can highlight keywords in text in Manticore Search using several methods.
You can benefit from search results highlighting if you want to improve readability of search results in your application or a web site. Highlighting allows you to get snippets of search results with matching keywords highlighted. It helps to improve your application’s search experience.
Using the following steps; you can highlight search text in Textarea or Div using javaScript; as shown below: First of all create a new html file and update the following code into your file: <p>JavaScript is a dynamic computer programming language.
Thanks to TLama answer (How to underline or highlight a part of node caption) I adjust a bit the code in order to highlight the text also in the middle.
procedure Tform_main.vt_mainDrawText(Sender: TBaseVirtualTree;
TargetCanvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex;
const Text: string; const CellRect: TRect; var DefaultDraw: Boolean);
var
BackMode, position: Integer;
begin
// if the just rendered node's Text contain the text written in a TEdit control
// called Edit, then...
position:= Pos(AnsiLowerCase(edit_search.Text), AnsiLowerCase(text));
if position > 0 then
begin
// store the current background mode; we need to use Windows API here because the
// VT internally uses it (so the TCanvas object gets out of sync with the DC)
BackMode := GetBkMode(TargetCanvas.Handle);
// setup the color and draw the rectangle in a width of the matching text
TargetCanvas.Brush.Color := clYellow;
TargetCanvas.FillRect(Rect(
CellRect.Left + TargetCanvas.TextWidth(Copy(Text, 1, position-1)),
CellRect.Top + 3,
CellRect.Left + TargetCanvas.TextWidth(Copy(Text, 1, position-1)) + TargetCanvas.TextWidth(Copy(Text, position, Length(edit_search.Text))),
CellRect.Bottom - 3)
);
// restore the original background mode (as it likely was modified by setting the
// brush color)
SetBkMode(TargetCanvas.Handle, BackMode);
end;
end;
Best wishes to TLama!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With