Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using SELTEXT, LENGTH, START with Grids 1

Status
Not open for further replies.

crogec

Programmer
May 10, 2002
16
US
VFP50

I have a need (strong desire?) to be able to select and highlight portions of text found in grid cells based on a text input box as part of a table search. Is this a legal move? I so far have not been able to make it work. I can select the row and even the specific cell but it seems to be either an all or nothing afair. I can highlight the cell etc but want to be able to highlight only the portion of data as it is input into the user input text box.

Hope this is clear and hope someone can help me out here.
Thanks,
George
 
You can as long as you don't refresh the grid, like with a SetFocus or Activate. Then again, if you have NODEFAULT in those events, it may work.

First, do this:
MyForm.Grid1.Column2.SelectOnEntry = .f.

Then something like:
WITH MyForm.Grid1.Column2
.text1.SelStart = At(cSearchString, .text1.Value)
.text1.SelLength = Len(cSearchString)
ENDWITH


Dave S.
[cheers]
 
Dave

I don't know about that. I have tried your suggestion, and about 20 of my trials and I cannot get the text in the textbox to be highlighted. Can you? Maybe I'm missing something.

Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
After doing some more tinkering, here's what I came up with. The SelStart and SelLength can be set in the LostFocus, if SelectOnEntry is .F.:

Try this code. Enter something in the text box, number, letters, whatever. It does a LOCATE FOR the first occurrence of the text, highlights any cells containing that text (as well as memo fields containing the text), dates, numbers. Remember that the search is case-sensitive though. The data is a query from the resource file, into a cursor named 'testtest'.

Code:
*.........   hilites.prg  ..........
PUBLIC ohilites
ohilites = CREATEOBJECT("hilites")
ohilites.Show
RETURN

**************************************************
DEFINE CLASS hilites AS form


   Top = 0
   Left = 0
   Height = 220
   Width = 543
   DoCreate = .T.
   Caption = "Hilight Cells"
   Name = "hilites"
   cselfld = .F.


   ADD OBJECT grid1 AS grid WITH ;
      Height = 169, ;
      Left = 24, ;
      RecordSource = "testtest", ;
      Top = 48, ;
      Width = 492, ;
      Name = "Grid1"


   ADD OBJECT text1 AS textbox WITH ;
      Height = 25, ;
      Left = 190, ;
      Top = 12, ;
      Width = 145, ;
      Name = "Text1"


   ADD OBJECT label1 AS label WITH ;
      WordWrap = .T., ;
      Caption = &quot;In the text box type characters or numbers, then <Tab>&quot;, ;
      Height = 30, ;
      Left = 25, ;
      Top = 10, ;
      Width = 162, ;
      Name = &quot;Label1&quot;


   ADD OBJECT label2 AS label WITH ;
      WordWrap = .T., ;
      Caption = &quot;Examples:  WIN or 03 or 123 etc.  Hilite is case sensitive.&quot;, ;
      Height = 30, ;
      Left = 340, ;
      Top = 10, ;
      Width = 162, ;
      Name = &quot;Label2&quot;


   PROCEDURE hilite
      PARAMETERS search_string, cfield

      DO CASE
         CASE TYPE(cfield) = &quot;C&quot; OR TYPE(cfield) = &quot;M&quot;
            IF search_string $ EVAL(cfield)
               RETURN RGB(0,255,255) &&... lite blue
            ENDIF

         CASE TYPE(cfield) = &quot;N&quot;
            IF search_string $ STR(EVAL(cfield))
               RETURN RGB(255,0,0) &&... lite red
            ENDIF

         CASE TYPE(cfield) = &quot;D&quot;
            IF search_string $ DTOC(EVAL(cfield))
               RETURN RGB(0,255,0) &&... lite red
            ENDIF

         OTHERWISE
            *  nop.  Not pretty, but I got lazy

      ENDCASE
   ENDPROC


   PROCEDURE showsel
      ThisForm.Grid1.Column2.Text1.SelStart = ;
         At(Alltrim(thisform.text1.Value), ThisForm.Grid1.Column2.Text1.Value) -1

      ThisForm.Grid1.Column2.Text1.SelLength = Len(Alltrim(thisform.text1.Value))
   ENDPROC


   PROCEDURE Load
      *... Grab records from resource file
      SELECT * from Sys(2005) INTO CURSOR testtest
   ENDPROC


   PROCEDURE grid1.Init

      *... breeze through columns and hilite them if cell 
      *...   contains search string
      FOR jj = 1 TO This.ColumnCount
         STORE 'Column' + ALLTRIM(STR(jj)) TO MyColumn
         This.&MyColumn..dynamicbackcolor = ;
            &quot;ThisForm.hilite(ALLTRIM(ThisForm.Text1.Value), &quot; + ;
            &quot;This.&MyColumn..controlsource)&quot;
      NEXT

   ENDPROC


   PROCEDURE grid1.Refresh
      NODEFAULT
   ENDPROC


   PROCEDURE text1.Valid

      STORE ALLTRIM(this.Value) TO cval
      LOCATE REST FOR ;
            cval $ type or;
            cval $ id or;
            cval $ name or;
            cval $ STR(ckval) or;
            cval $ DTOC(updated)

      DO CASE 
         CASE cval $ type 
            ThisForm.cSelfld = 1
         CASE cval $ id
            ThisForm.cSelfld = 2
         CASE cval $ name
            ThisForm.cSelfld = 3
         CASE cval $ STR(ckval) 
            ThisForm.cSelfld = 4
         CASE cval $ DTOC(updated)
            ThisForm.cSelfld = 5
      ENDCASE       

      ThisForm.Grid1.Column2.Selectonentry = .F.
         
      RETURN .T.
   ENDPROC


   PROCEDURE text1.LostFocus
      ThisForm.grid1.Columns(ThisForm.cSelfld ).SetFocus

      ThisForm.Grid1.Column2.Text1.SelStart = ;
         At(Alltrim(thisform.text1.Value), ThisForm.Grid1.Column2.Text1.Value) -1

      ThisForm.Grid1.Column2.Text1.SelLength = Len(Alltrim(thisform.text1.Value))
   ENDPROC


ENDDEFINE
*
*-- EndDefine: hilites
**************************************************

Dave S.
[cheers]
 
For that last post, I had it so that only text in column 2 could show the selected text. Sorry for that oversight. Now, it will select text in character fields as well as numerica and date. It will also hilite all fields containing the text. If the text is in a memo field, the memo will be hilited, but no SelStart/SelLength.
Here is the revised code:

Code:
PUBLIC ohilites
ohilites = CREATEOBJECT(&quot;hilites&quot;)
ohilites.SHOW
RETURN

**************************************************
DEFINE CLASS hilites AS FORM


   TOP = 0
   LEFT = 0
   HEIGHT = 220
   WIDTH = 543
   DOCREATE = .T.
   CAPTION = &quot;Hilight Cells&quot;
   NAME = &quot;hilites&quot;
   cselfld = .F.


   ADD OBJECT grid1 AS GRID WITH ;
      HEIGHT = 169, ;
      LEFT = 24, ;
      RECORDSOURCE = &quot;testtest&quot;, ;
      TOP = 48, ;
      WIDTH = 492, ;
      NAME = &quot;Grid1&quot;


   ADD OBJECT text1 AS TEXTBOX WITH ;
      HEIGHT = 25, ;
      LEFT = 190, ;
      TOP = 12, ;
      WIDTH = 145, ;
      NAME = &quot;Text1&quot;


   ADD OBJECT label1 AS LABEL WITH ;
      WORDWRAP = .T., ;
      CAPTION = &quot;In the text box type characters or numbers, then <Tab>&quot;, ;
      HEIGHT = 30, ;
      LEFT = 25, ;
      TOP = 10, ;
      WIDTH = 162, ;
      NAME = &quot;Label1&quot;


   ADD OBJECT label2 AS LABEL WITH ;
      WORDWRAP = .T., ;
      CAPTION = &quot;Examples:  WIN or 03 or 123 etc.  Hilite is case sensitive.&quot;, ;
      HEIGHT = 30, ;
      LEFT = 340, ;
      TOP = 10, ;
      WIDTH = 162, ;
      NAME = &quot;Label2&quot;


   PROCEDURE hilite
      PARAMETERS search_string, cfield

      DO CASE
         CASE TYPE(cfield) = &quot;C&quot; OR TYPE(cfield) = &quot;M&quot;
            IF search_string $ EVAL(cfield)
               RETURN RGB(0,255,255) &&... lite blue
            ENDIF

         CASE TYPE(cfield) = &quot;N&quot;
            IF search_string $ STR(EVAL(cfield))
               RETURN RGB(255,0,0) &&... lite red
            ENDIF

         CASE TYPE(cfield) = &quot;D&quot;
            IF search_string $ DTOC(EVAL(cfield))
               RETURN RGB(0,255,0) &&... lite red
            ENDIF

         OTHERWISE
            *  nop.  Not pretty, but I got lazy

      ENDCASE
   ENDPROC


   PROCEDURE showsel
      THISFORM.Grid1.Column2.Text1.SELSTART = ;
         AT(ALLTRIM(THISFORM.text1.VALUE), THISFORM.Grid1.Column2.Text1.VALUE) -1

      THISFORM.Grid1.Column2.Text1.SELLENGTH = LEN(ALLTRIM(THISFORM.text1.VALUE))
   ENDPROC


   PROCEDURE LOAD
      *... Grab records from resource file
      *SELECT * from Sys(2005) INTO CURSOR testtest
      USE testtest
   ENDPROC


   PROCEDURE grid1.INIT

      *... breeze through columns and hilite them if cell
      *...   contains search string
      FOR jj = 1 TO THIS.COLUMNCOUNT
         STORE 'Column' + ALLTRIM(STR(jj)) TO MyColumn
         THIS.&MyColumn..DYNAMICBACKCOLOR = ;
            &quot;ThisForm.hilite(ALLTRIM(ThisForm.Text1.Value), &quot; + ;
            &quot;This.&MyColumn..controlsource)&quot;
      NEXT

   ENDPROC


   PROCEDURE grid1.REFRESH
      NODEFAULT
   ENDPROC


   PROCEDURE text1.VALID
      STORE ALLTRIM(THIS.VALUE) TO cval
      LOCATE REST FOR ;
         cval $ TYPE OR;
         cval $ ID OR;
         cval $ NAME OR;
         cval $ STR(ckval) OR;
         cval $ DTOC(UPDATED)

      DO CASE
         CASE cval $ TYPE
            THISFORM.cSelfld = 1
         CASE cval $ ID
            THISFORM.cSelfld = 2
         CASE cval $ STR(ckval)
            THISFORM.cSelfld = 5
         CASE cval $ DTOC(UPDATED)
            THISFORM.cSelfld = 7
         OTHERWISE
            THISFORM.cSelfld = 0
      ENDCASE

      THISFORM.Grid1.Column2.SELECTONENTRY = .F.

      RETURN .T.
   ENDPROC


   PROCEDURE text1.LOSTFOCUS
      IF THISFORM.cSelfld > 0
         WITH THISFORM.grid1.COLUMNS(THISFORM.cSelfld )
            .SETFOCUS

            DO CASE
               CASE TYPE(.CONTROLSOURCE) = &quot;C&quot; OR TYPE(.CONTROLSOURCE) = &quot;M&quot;
                  .Text1.SELSTART  = AT(ALLTRIM(THISFORM.text1.VALUE), .Text1.VALUE) -1

               CASE TYPE(.CONTROLSOURCE) = &quot;N&quot;
                  .Text1.SELSTART  = ;
                     AT(ALLTRIM(THISFORM.text1.VALUE), LTRIM(STR(.Text1.VALUE)))

               CASE TYPE(.CONTROLSOURCE) = &quot;D&quot;
                  .Text1.SELSTART  = ;
                     AT(ALLTRIM(THISFORM.text1.VALUE), DTOC(.Text1.VALUE)) -1

               OTHERWISE
                  *  nop.  Not pretty, but I got lazy

            ENDCASE
            .Text1.SELLENGTH = LEN(ALLTRIM(THISFORM.text1.VALUE))
         ENDWITH
      ENDIF
   ENDPROC

ENDDEFINE
*
*-- EndDefine: hilites
**************************************************

Dave S.
[cheers]
 
Dave, thanks for your efforts and help. Although your work gets me close, my goal was to only hilite a single cell with only the user input 'searchtext' hilited in that single cell. The user could then use the up/down arrow keys to scroll through any other records that met the filter criteria, (searchtext), and select the desired record by pressing the enter key. Further my desire was to have only the searchtext hilited in each subsequent cell as scrolling took place. I have accepted a lesser solution in the interest of expediency; a single row is hilited as the user scrolls. The selected record is then copied into a temp file for further processing. Although not elegant, it is functional and I must move on.

I view grids as an opportunity for further learning!

Again, thanks for you help.
George
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top