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

Search records by typing (grid) 2

Status
Not open for further replies.

Filip Brnic

Programmer
Dec 25, 2023
49
RS
Hello, I have a grid in my form and i wanna be able to, lets say on keypress, anywhere on the screen or maybe in a textbox, start searching for a certain record in field ime.list1 now i know how to get to that record with the define window etc, then using save noapp nomod nodel, but i wanna elevate this so i can just simply start typing C and it starts showing me the records starting on C, and when i start typing Co, it shows me lets say Coca cola if its the first alphabetical record, But i wanna be able to like stop searching for C records, so i type Coc, and then i give up, when i press lets say the 4th key, it starts searching again, idk if this is possible, In my agency we have a code that does that but right now i dont have access to it since my mentor is not there for sometime, and i wanna learn about this, of course, im not really asking for a whole code to just copy, since its probably a little harder what im asking, but i want some directions i guess...
 
Using a ComboBox (in drop down list style) or Listbox, there's simply a property incrementalsearch doing that, btw. The _INCSEEK variable determines a timeout after which you get to the behaviour you described as giving up and starting again, i.e. waiting too loong with the next character to add to the search word.

It has pros and cons, a pro obviously is it needs no code, a con it's not a property you have on a grid. I think it also works within a grid when AllowCellSelection is .f. and an index is set, but I haven't tried.

Chriss
 
Hi Filip,

...

and some demo code

Code:
LOCAL loForm
 
loForm = CREATEOBJECT("clssearch")
loForm.Show()

Read EVENTS

CLOSE ALL

CLEAR ALL 

RETURN 

***********
 
DEFINE CLASS clssearch AS form
 
	Top = 12
	Left = 12
	Height = 474
	Width = 1050
	MinWidth = This.Width
	Caption = "Incremental Search"
	WindowState = 0
	AutoCenter = .T.
	Name = "clssearch"
	Themes = .F.
	ShowTips = .T.
	
	ADD OBJECT lblName as Label WITH ;
		Left = 12, Top = 12, Caption = "Item to search :", FontBold = .T.
	
	ADD OBJECT lblFound as Label WITH ;
		Left = 270, Top = 12, Autosize = .T., Caption = "Item highlighted :", FontBold = .T.
	
	ADD OBJECT lblItemName as Label WITH ;
		Left = 270, Top = 36, AutoSize = .T., Caption = "", FontBold = .T., FontItalic = .T., FontSize = 12

	ADD OBJECT txtBox as TextBox WITH ;
		Left = 12, Top = 36, Format = "!", ToolTipText = "Double-click to clear textbox"
		
		PROCEDURE txtBox.DblClick()
			This.Value = ""

		ENDPROC 
		
		PROCEDURE txtBox.InteractiveChange
			LOCAL lcSearch
			
			lcSearch = ALLTRIM(This.Value)
			= INDEXSEEK(lcSearch, .T., "csrWords")
			
			WITH ThisForm
				.lblItemName.Caption = cWord
				.grdValues.Refresh()
			ENDWITH 
		ENDPROC 

	ADD OBJECT grdValues AS Grid WITH ;
		Left = 12, ;
		Top = 72, ;
		Height = 474 - 72 - 12, ;
		Width = 1050 - 24, ;
		Anchor = 15, ;
		BackColor = RGB(224, 224, 224), ;
		AllowRowSizing = .F., ;
		HeaderHeight = 21, ;
		AllowHeaderSizing = .F., ;
		DeleteMark = .F., ;
		HighLightStyle = 2, ;
		HighlightBackColor = RGB(0, 250, 250), ;
		HighlightForeColor = RGB(0, 0, 0), ;
		ColumnCount = -1, ;
		RowSourceType = 2, ;
		RowSource = "csrWords", ;
		ReadOnly = .T.
		
		PROCEDURE grdValues.Init()

			WITH This
				.SetAll("FontBold", .T., "Header")
				.SetAll("BackColor", RGB(0, 250, 250), "Header")
			ENDWITH 

			WITH This.Column1
				.Width = 246
				.Header1.Caption = "Name"
			ENDWITH

			WITH This.Column2
				.Width = 150
				.Header1.Caption = "Value One"
			ENDWITH
			
			WITH This.Column3
				.Width = 150
				.Header1.Caption = "Value Two"
			ENDWITH

			WITH This.Column4
				.Width = 150
				.Header1.Caption = "VO + VT"
			ENDWITH

			WITH This.Column5
				.Width = 150
				.Header1.Caption = "VO - VT"
			ENDWITH

			WITH This.Column6
				.Width = 150
				.Header1.Caption = "VO + (2 * VT)"
			ENDWITH	
		ENDPROC  
		
		PROCEDURE grdValues.AfterRowColChange()
			LPARAMETERS nColIndex

			WITH ThisForm
				.lblItemName.Caption = cWord
				.Refresh()
			ENDWITH 
		ENDPROC 

	PROCEDURE Load()
		LOCAL lnI, lnY, lnWordLength, lcItem, lnUpper, lnLower

		lnUpper = 90 &&ASCII (Z)
		lnLower = 65 &&ASCII (A)

		CREATE CURSOR csrWords ( cWord C(20), iValOne I, iValTwo I, iValThree I, iValFour I, iValFive I)
		INDEX on cWord TAG tagWord
		SET ORDER to tagWord
  
		FOR lnI = 1 to 1500
			lcItem = ""

			lnWordLength = INT(20 * RAND()) + 1
   
			FOR lnY = 1 TO lnWordLength
				lcItem = lcItem + CHR(INT((lnUpper - lnLower + 1) * RAND()) + lnLower)
			ENDFOR

			INSERT INTO csrWords VALUES (lcItem, RAND() * 5000, RAND() * 1250, 0, 0, 0)

		ENDFOR 

		UPDATE csrWords SET iValThree = iValOne + iValTwo, iValFour = iValOne - iValTwo, iValFive = iValOne + (2 * iValTwo)

	ENDPROC 
	
	PROCEDURE Destroy()
		ThisForm.Release()
		CLEAR Events
			
	ENDPROC              
ENDDEFINE

**********

hth

marK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top