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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to select an item using more than one key press 2

Status
Not open for further replies.

SitesMasstec

Programmer
Sep 26, 2010
523
Brasil
Hello dear colleagues!

I have a grid on a form, which displays all records from a file.

I just select an item (a record) of the file by key pressing the first letter of the desired item name ("PRODUTO"). It works fine. Please, see the ilustration bellow:
SelecaoItemEstoque1_ubuxh9.jpg


Now I want to make it to work better: If I want to select the item 'CHOCOLATE', instead of pressing just 'C' (it selects the first 'C' ocurrency, CAFE) and arrow down key 7 times to find the desired item, 'CHOCOLATE', I want to press the keys C and H to go directly to 'CHOCOLATE'.

Is it possible?

Thank you,
SitesMasstec
 
What you have is not the default behaviour of a grid. You clearly have some code in the Keypress event of your grid that is letting you go to the first item by pressing the relevant letter. You will need to modify that code to get the functionality you want.

If you could post the code in question (as code, not as a screenshot), we might be able to help.

Be aware also that the listbox has this behaviour build it. It is called an incremental search. But a listbox is quite different from a grid, and you probably won't want to use a different control just to get this functionality.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Hello Mike!

Here is the code.

Note that Grid is populated with data from the file ITENSEST

Code:
Object: Form1    Procedure: KeyPress

LPARAMETERS nKeyCode, nShiftAltCtrl

IF nKeyCode=13 AND nShiftAltCtrl=0 
   EsteCodigo=itensest.wecodi 
   EstaDescri=itensest.wenome
   thisform.Release
ENDIF

IF ISALPHA(CHR(nKeyCode))
   SELECT ITENSEST
   LOCATE FOR UPPER(LEFT(WENOME, 1)) = UPPER(CHR(nKeyCode))
ENDIF 

----------------------------------------------------------------
----------------------------------------------------------------

Object: Grid1    Procedure: DblClick

* Codigo Escolhido

EsteCodigo=itensest.wecodi 
EstaDescri=itensest.wenome
thisform.Release

----------------------------------------------------------------

Object: Grid1    Procedure: Init

EsteCodigo=0
EstaDescri=""

----------------------------------------------------------------

Thank you,
SitesMasstec
 
You were already getting advice in thread184-1777773

Olaf Doschke said:
...have a textbox above the grid header and let the user enter the "begins with" letters there.

Then the LOCATE becomes LOCATE FOR UPPER(WENOME) = UPPER(Thisform.SearchTetbox.Value)

or you go with other recommendations of that past thread, like using the Foxy Classes Tamar mentioned there.

Or go with this´´;
Bye, Olaf.

Olaf Doschke Software Engineering
 
Hello colleagues!

Now I wrote these codes:

Code:
Object: Form1    Procedure: Init

EsteCodigo=0
EstaDescri=""
thisform.SearchTetbox.SetFocus

----------------------------------------------------------------
----------------------------------------------------------------

Object: Grid1    Procedure: DblClick

* Codigo Escolhido

EsteCodigo=itensest.wecodi 
EstaDescri=itensest.wenome
thisform.Release

----------------------------------------------------------------
----------------------------------------------------------------

Object: SearchTextBox    Procedure: LostFocus


SELECT ITENSEST
LOCATE FOR UPPER(WENOME) = UPPER(Thisform.SearchTetbox.Value)

EsteCodigo=itensest.wecodi 
EstaDescri=itensest.wenome
*thisform.Release

---------------------------------------------------------------- 
----------------------------------------------------------------

And it resulted wrong:

SelecaoItemEstoque2_cr2msl.jpg


Thank you,
SitesMasstec
 
Olaf:

I used this and the result was the same:

Code:
LOCATE FOR UPPER(SUBSTR(WENOME,1,LEN(Thisform.SearchTextBox.Value))) = UPPER(Thisform.SearchTextBox.Value)

And Set Exact is set to OFF

Thank you,
SitesMasstec
 
Di you set a max length in the SearchTextbox? Anything that pads what you enter there with spaces?

For comparisons with EXACT OFF it's most important the right hand side of a comparison is just the letters typed in, you don't need SUBSTR() and if you'd shorten the field value, then better with LEFT().

Bye, Olaf.

Olaf Doschke Software Engineering
 
Besides, the WENOME field values are already all upper case, as can be seen from the grid, don't use function on fields, that just means an index on WENOME will not be used.

SELECT ITENSEST
LOCATE FOR WENOME = UPPER(ALLTRIM(Thisform.SearchTextBox.Value))

Unless you have an index on UPPER(WENOME), then you can stay with LOCATE FOR UPPER(WENOME) = UPPER(ALLTRIM(Thisform.SearchTextBox.Value)), but don't apply more complex expressions on the WENOME field, that disables the use of this index.

Bye, Olaf.

Olaf Doschke Software Engineering
 
Hello colleagues!

Now a small problem appeared: when executing the form (1), if the mouse pointer is over the 2nd column Header ("Produto") and in which area the SearchTextBox is located, the objects there become invisible (2).

SelecaoItemEstoque11_kfniyg.jpg


If the user click over the area where the invisible SearchTextBox is, then it becomes visible again.

Is there a way to avoid this?


Thank you,
SitesMasstec
 
I've just got back to this thread after my initial post yesterday, and I see Olaf has given you a good solution. Unfortunately, I don't know the answer to your latest problem - where the textbox becomes invisible until the user clicks on it. If you don't find a better solution, would it be an option to move the textbox completely above the grid?

But I did notice another issue. What happens if the user types a value that is not present in the table? In other words, if the LOCATE fails? As far as I can see from your code, the highlight will go the end of the table in that case, which is probably not what you want.

The solution would be to save the current record pointer immediately before the LOCATE, and then return to that record if the LOCATE fails:

Code:
lnSaveRec = RECNO()
LOCATE FOR ....
IF NOT FOUND()
  GO lnSaveRec
ENDIF

Keep in mind too that after moving the highlight you might need to set focus on the grid to make the change visible.

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Hello Mike!

I followed your 2 advices:
1) put the SearchTextBox outside the Grid
2) the record pointer does not move if an item to be searched is not found

... and it looks great now! Thank you very much.

SelecaoItemEstoque12_h2gxyo.jpg



Thank you,
SitesMasstec
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top