Page 1 sur 1

Combo box autocompletition

Publié : mar. 17/août/2010 5:51
par Parole De JOJO
le code suivant est tire du codarchive, il est compatible seulement avec Purebasic 4.20
comment l'adapter a la nouvelle version?

merci d'avance

Code : Tout sélectionner

; English forum: http://www.purebasic.fr/english/viewtopic.php?t=7269&highlight=
; Author: ebs (updated for PB4.00 by blbltheworm)
; Date: 23. August 2003
; OS: Windows
; Demo: No

#NONE = -1 

;- autocomplete specified combobox 
Procedure AutocompleteComboBox(ComboBox.l) 
  Shared LenTextSave.l 
  Shared GadgetSave.l 

  ; get text entered by user 
  TextTyped.s = UCase(GetGadgetText(ComboBox)) 
  LenTextTyped.l = Len(TextTyped) 
  
  ; skip if same gadget and same length or shorter text (backspace?) 
  If ComboBox = GadgetSave And LenTextTyped <= LenTextSave 
    LenTextSave = LenTextTyped 
  ElseIf LenTextTyped 
    GadgetSave = #NONE 
    ; search combo contents for item that starts with text entered 
    MaxItem.l = CountGadgetItems(ComboBox) - 1 
    For Item.l = 0 To MaxItem 
      If TextTyped = UCase(Left(GetGadgetItemText(ComboBox, Item, 0), LenTextTyped)) 
        ; found matching item 
        ; set combo state 
        SetGadgetState(ComboBox, Item) 
        ; select added text only 
        hComboEdit.l = ChildWindowFromPoint_(GadgetID(ComboBox), 5, 5) 
        SendMessage_(hComboEdit, #EM_SETSEL, LenTextTyped, -1) 
        ; save gadget number and text length for next pass 
        LenTextSave = LenTextTyped 
        GadgetSave = ComboBox 
        ; exit For loop 
        Item = MaxItem 
      EndIf 
    Next 
  EndIf 
EndProcedure 

If OpenWindow(0, 0, 0, 300, 300, "Autocompletion", #PB_Window_SystemMenu|#PB_Window_TitleBar) 
  If CreateGadgetList(WindowID(0)) 
    ComboBoxGadget(0, 10, 10, 200, 100, #PB_ComboBox_Editable) 
    AddGadgetItem(0, -1, "Autocomplete") 
    AddGadgetItem(0, -1, "Bernard") 
    AddGadgetItem(0, -1, "Car") 
    AddGadgetItem(0, -1, "Explorer") 
    AddGadgetItem(0, -1, "Fantastic") 
    AddGadgetItem(0, -1, "General protection error") 
    AddGadgetItem(0, -1, "Purebasic") 
    AddGadgetItem(0, -1, "Purepower") 
    AddGadgetItem(0, -1, "Purevisual") 
    AddGadgetItem(0, -1, "Question") 
    AddGadgetItem(0, -1, "Zas") 
  EndIf 
EndIf 

Repeat 
  Event = WaitWindowEvent() 
  Select Event 
    Case #PB_Event_Gadget 
      If EventGadget() = 0 
        AutocompleteComboBox(0) 
      EndIf 
  EndSelect 
Until Event = #PB_Event_CloseWindow 

End

Re: Combo box autocompletition

Publié : mar. 17/août/2010 8:23
par Backup
....

Re: Combo box autocompletition

Publié : mar. 17/août/2010 12:29
par Parole De JOJO
merci, je connaissais cette mise a jour, mais ca ne marche pas

Re: Combo box autocompletition

Publié : mar. 17/août/2010 12:33
par Ar-S
ça marche sur la 1ère lettre du 1er mot.

Re: Combo box autocompletition

Publié : mar. 17/août/2010 17:18
par Backup
Ar-S a écrit :ça marche sur la 1ère lettre du 1er mot.
oui , et je ne pense pas que ce code soit fait pour rendre un résultat diffèrent ! :)
Parole De JOJO a écrit :merci, je connaissais cette mise a jour, mais ca ne marche pas
c'est pas une mise a jour , j'ai juste ajouter "point.point" a l'Api donné par ton code !

Code : Tout sélectionner

hComboEdit.l = ChildWindowFromPoint_(GadgetID(ComboBox), 5, 5) 
remplacé par

Code : Tout sélectionner

hComboEdit.l = ChildWindowFromPoint_(GadgetID(ComboBox),point.point) 
c'est ce qui plantait ...

j'ai corrigé, c'est deja pas mal non ?

Re: Combo box autocompletition

Publié : mer. 25/août/2010 18:36
par Le Soldat Inconnu
Oui, enfin, il y avait pas mal de chose à revoir sur ce code pour le faire fonctionner.

C'était plutôt mal fichu, je trouve.
Voici la correction

Code : Tout sélectionner

; Author: LSI (Correction of code from ebs, 23. August 2003)
; OS: Windows

;- autocomplete specified combobox
Procedure AutocompleteComboBox_SearchEdit(hwnd.l, lParam.l)
	Tampon.s = Space(50)
	GetClassName_(hwnd, @Tampon, 50)
	
	If Tampon = "Edit" ; Non de l'objet rechercher
		PokeL(lParam, hwnd)
	EndIf
	
	ProcedureReturn #True
EndProcedure
Procedure AutocompleteComboBox(ComboBox.l)
	
	; Get combobox edit hwnd
	ComboxEdit = 0
	EnumChildWindows_(GadgetID(ComboBox), @AutocompleteComboBox_SearchEdit(), @ComboxEdit)
	
	; get text entered by user
	TextTyped.s = UCase (GetGadgetText(ComboBox))
	SendMessage_(ComboxEdit, #EM_GETSEL, @Selection_Start, @Selection_End)
	LenTextTyped = Selection_Start
	LenTextSave = GetGadgetData(ComboBox)
	
	Debug TextTyped
	Debug LenTextTyped
	
	; skip if same gadget and same length or shorter text (backspace?)
	If LenTextTyped <= LenTextSave
		SetGadgetData(ComboBox, LenTextTyped)
	ElseIf LenTextTyped
		; search combo contents for item that starts with text entered
		MaxItem.l = CountGadgetItems(ComboBox) - 1
		For Item.l = 0 To MaxItem
			ItemText.s = GetGadgetItemText(ComboBox, Item, 0)
			If TextTyped = UCase (Left(ItemText, LenTextTyped))
				; found matching item²
				; set combo state
				SetGadgetState(ComboBox, Item)
				; select added text only
				Selection_Start = LenTextTyped
				Selection_End = Len(ItemText)
				SendMessage_(ComboxEdit, #EM_SETSEL, Selection_Start, Selection_End)
				; save gadget number and text length for next pass
				SetGadgetData(ComboBox, LenTextTyped)
				; exit For loop
				Item = MaxItem
				Break
			EndIf
		Next
	EndIf
EndProcedure

If OpenWindow(0, 0, 0, 300, 300, "Autocompletion", #PB_Window_SystemMenu | #PB_Window_TitleBar)
	If CreateGadgetList(WindowID(0))
		ComboBoxGadget(0, 10, 10, 200, 24, #PB_ComboBox_Editable)
		AddGadgetItem(0, -1, "Autocomplete")
		AddGadgetItem(0, -1, "Bernard")
		AddGadgetItem(0, -1, "Car")
		AddGadgetItem(0, -1, "Explorer")
		AddGadgetItem(0, -1, "Fantastic")
		AddGadgetItem(0, -1, "General protection error")
		AddGadgetItem(0, -1, "Purebasic")
		AddGadgetItem(0, -1, "Purepower")
		AddGadgetItem(0, -1, "Purevisual")
		AddGadgetItem(0, -1, "Question")
		AddGadgetItem(0, -1, "Zas")
	EndIf
EndIf

Repeat
	event = WaitWindowEvent()
	Select event
		Case #PB_Event_Gadget
			If EventGadget() = 0
				AutocompleteComboBox(0)
			EndIf
	EndSelect
Until event = #PB_Event_CloseWindow

End

Re: Combo box autocompletition

Publié : jeu. 26/août/2010 11:39
par Parole De JOJO
merci