Ticket B3656
Visible to All Users

Highlight found words in cells

created 19 years ago

How to do a programed search and highligth found words in cxGrid?
Thank you !
HB

Show previous comments (12)
DevExpress Support Team 19 years ago

    For now, we cannot diagnose this problem. What is the text you try to find within cxRichEdit and how do you transmit it in to the FindText? Sorry, but I have to ask you to send a sample project to us again. I am looking forward to hearing from you.

      Thank you!
      Please fin the code I use bellow, I do not understand why the string seems to be corrupted when reurned by findtext, code is from you example where it runs fine with same modification.
      Customer want every found word in posibly several rows to be higlighted could I use CustomDrawCell and the change font color for found word.
      (Its read only)
      Thank you!
      HB
      Procedure TForm1.VisText(Finn,FN : String);
      var
        I, FoundPos : integer;
        AColumn : TcxGridDBColumn;
      // AText : String;
      begin
      // AText := Trim(Finn);
      // ShowMessage(AText);
      // with KKLoggGDBTableView1 do
        with cxGridDBTableView1 do
        begin
          AColumn := GetColumnByFieldName(FN);
          for I := 0 to ViewData.RecordCount - 1 do
            if ViewData.Rows[I].IsData then
            begin
              ViewData.Rows[I].Focused := True;
              with Controller.EditingController do
              begin
                ShowEdit(AColumn);
                FoundPos := TcxRichEdit(Edit).FindText(Finn, 0, Length(TcxRichEdit(Edit).Text), []);
                if not (FoundPos < 0) then
                begin
                  TcxRichEdit(Edit).SelStart := FoundPos;
                  TcxRichEdit(Edit).SelLength := Length( Finn);
                  SendMessage(TcxRichEdit(Edit).InnerControl.Handle,EM_SCROLLCARET, 0, 0);
                  Break;
                end
                else
                  AColumn.Editing := False;
              end;
            end;
        end;
      end;

      DevExpress Support Team 19 years ago

        As for your problem, I assume that you apply our code for the column with its Options.Editing set to False whereas the cell’s editor should be activated when using the RichEdit’s FindText. As for the several rows to be highlighted, you can implement it via the OnCustomDrawCell event handler as described in our knowledge base article "How to color a Grid cell at runtime", http://www.devexpress.com/kb55. But it is a complicated task to highlight several words using these event handlers. I suggest that you try to restrict editing via the View’s OnEditing event handler and its AAllow var-parameter. This will allow to restrict editing for users and allow it when implementing the search within editor. To highlight several words which were found, you can use the following approach.
        Once the word was found, you should set the proper background color and continue the search from the end of found word.
        To change the background color, you can use the Win32 API functions as our TcxRichEdit is based on the TRichEdit from the standard Delphi VCL. See the sample code below. InGrid the RichEdit column’s Properties.PlainText should be True. We do not support formatted text within a View at this time.
        uses ComCtrls,RichEdit;

        I, FoundPos: integer;
          AColumn : TcxGridDBColumn;
          AText : String;
          Format: CHARFORMAT2;
        begin
          …
           FoundPos := TcxRichEdit(Edit).FindText(AText, 0, Length(TcxRichEdit(Edit).Text), [stMatchCase]);
                  if not (FoundPos < 0) then
                  begin
                    TcxRichEdit(Edit).SelStart := FoundPos;
                    TcxRichEdit(Edit).SelLength := Length( AText);
                    SendMessage(TcxRichEdit(Edit).InnerControl.Handle,EM_SCROLLCARET, 0, 0);
        // Here is code for the approach
                   FillChar(Format, SizeOf(Format), 0);
                    with Format do
                    begin
                      cbSize := SizeOf(Format);
                      dwMask := CFM_BACKCOLOR;
                      crBackColor := clYellow; //<<<<<<<define background color
                      TcxRichEdit(Edit).InnerControl.Perform(EM_SETCHARFORMAT, SCF_SELECTION, Longint(@Format));
                    end;
                   …
                 end;
                 …
        end;
        After highlighting the first word, you can try to search another word. The main idea is to use the FindText’s StartPos parameter set to FoundPos + Length(AText), the Length parameter should be also corrected.
        FoundPos := TcxRichEdit(Edit).FindText(AText, FoundPos + Length(AText), Length(TcxRichEdit(Edit).Text)- FoundPos , [stMatchCase]);
        If FoundPos differ from -1, we can highlight the next founded word again.
        In common, your task can be completed as follows.
        1)     Determine the records in which the RichEdit’s column contains proper text.
        2)     Highlight the cells via appropriate color using the OnGetContentStyle or OnCustomDrawCell event handlers.
        3)     If a user wishes to see the words, you can activate the editor and show them as described above.
        I hope this will help you.

        Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

        Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.