Autocomplete for Comboboxgadget - Simple version

Share your advanced PureBasic knowledge/code with the community.
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4790
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Autocomplete for Comboboxgadget - Simple version

Post by Fangbeast »

netmaestro wrote:
oldcomboproc is only for a single combo box as I understand it??
'oldcomboproc' is going to be the same for all comboboxes you subclass as it's the default combobox procedure. You can safely use this with multiple combo boxes.
Thank you NetMaestro, simply very useful to me. Don't have much brain left over while I am studying (at my age damn it!!) and taking exams.

Soon to have blessed relief from them.
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4790
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Autocomplete for Comboboxgadget - Simple version

Post by Fangbeast »

How do I make this work if the combobox has images?

When I start my program, the first entry is missing the image. When I start typing in the combo, the autocomplete works but no corresponding image is set for the completed item when it fully matches something in the combo list.

I imagine that a whole new approach is required??
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
mesozorn
Enthusiast
Enthusiast
Posts: 171
Joined: Fri Feb 20, 2009 2:23 am

Re: Autocomplete for Comboboxgadget - Simple version

Post by mesozorn »

Hi, apologies for my late return to this topic from a while back. A couple of things:

1) Note that what I am sub-classing is not the entire ComboBox, but rather the Edit Control sub-component of that ComboBox. So as long as you are only ever subclassing other comboboxes in your program in this exact same way using this autocomplete routine, you are fine to use "oldcomboproc" consistently for all of them at all times, as Netmaestro pointed out. If however you subclass another combobox in a totally different way, be sure not to use "oldcomboproc" since it is not in fact the default window procedure for combobox gadgets/controls in general, but rather the default winproc for Edit controls.

2) I have thrown this new example together rather quickly so it may possibly contain bugs I didn't notice, but it seems to work as you want for displaying images along with their selections in Image-enabled ComboBoxes:

Code: Select all

Global oldcomboProc
UsePNGImageDecoder()
LoadImage(0, #PB_Compiler_Home + "examples/sources/Data/world.png")

Procedure.i ComboAutoComplete(hWnd, uMsg, wParam, lParam)
  Protected result
  Select uMsg
    Case #WM_CHAR
    acg=GetActiveGadget() ;GetDlgCtrlID_(hwnd)
    SendMessage_(hwnd, #EM_GETSEL, @spos.l, @epos.l)
    
    matchesfound=0
      For x=0 To CountGadgetItems(acg)-1
        If LCase(Left(GetGadgetText(acg),spos)+LCase(Chr(wParam)))=LCase(Left(GetGadgetItemText(acg,x),spos+1)) And epos=Len(GetGadgetText(acg))
          matchesfound+1:match=x
          If matchesfound>1:Break:EndIf
        EndIf
      Next x
     
      If matchesfound=1
          ks=GetKeyState_(#VK_SHIFT)
          If ks<2:addchar$=LCase(Chr(wparam)):Else:addchar$=UCase(Chr(wparam)):EndIf
          SetGadgetState(acg,match)
          SendMessage_(hwnd, #EM_SETSEL, spos+1, epos+999)
          result=0
      Else
        g$=GetGadgetText(acg)
        SetGadgetState(acg,-1)
        SetGadgetText(acg,g$)
        SendMessage_(hwnd, #EM_SETSEL, spos, epos+999)
        result = CallWindowProc_(oldcomboproc, hWnd, uMsg, wParam, lParam)
      EndIf
     
    Default
      result = CallWindowProc_(oldcomboproc, hWnd, uMsg, wParam, lParam)
  EndSelect
  ProcedureReturn result
EndProcedure


OpenWindow(0, 0, 0, 270, 140, "ComboBoxGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  hWnd = ComboBoxGadget(0, 10, 10, 250, 21, #PB_ComboBox_Editable|#PB_ComboBox_Image)
  hWnd = GetWindow_(hWnd, #GW_CHILD)
  hWndEdit = FindWindowEx_(hWnd, 0, "EDIT", 0)
  oldcomboproc = SetWindowLong_(hWndEdit, #GWL_WNDPROC, @ComboAutoComplete())
  AddGadgetItem(0, -1, "Washington")
  AddGadgetItem(0, -1, "Jefferson")
  AddGadgetItem(0, -1, "Lincoln",ImageID(0))
  AddGadgetItem(0, -1, "Franklin")
  AddGadgetItem(0, -1, "Walters")
     
  SetActiveGadget(0)



Repeat
  W=WaitWindowEvent()
Until W = #PB_Event_CloseWindow
Seldon
Enthusiast
Enthusiast
Posts: 405
Joined: Fri Aug 22, 2003 7:12 am
Location: Italia

Re: Autocomplete for Comboboxgadget - Simple version

Post by Seldon »

The first published code it works under 4.51 , I think because PB now uses again Windows Combobox class if #PB_ComboBox_Image is NOT used. Actually if you set #PB_ComboBox_Image flag, it doesn't work (I guess in such case it uses ComboBoxEx class).
Ramihyn_
Enthusiast
Enthusiast
Posts: 314
Joined: Fri Feb 24, 2006 9:40 am

Re: Autocomplete for Comboboxgadget - Simple version

Post by Ramihyn_ »

Seldon wrote:The first published code it works under 4.51 , I think because PB now uses again Windows Combobox class if #PB_ComboBox_Image is NOT used. Actually if you set #PB_ComboBox_Image flag, it doesn't work (I guess in such case it uses ComboBoxEx class).
Check http://www.purebasic.fr/blog/?p=336
Seldon
Enthusiast
Enthusiast
Posts: 405
Joined: Fri Aug 22, 2003 7:12 am
Location: Italia

Re: Autocomplete for Comboboxgadget - Simple version

Post by Seldon »

Yes, thanks. It says "Combobox" & "WC_COMBOBOXEX" classes, so I suppose it works that way. Actually there was a thread in the forum and the great PB team provided such a 'smart' solution to let older code that used API functions to work. :)
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Autocomplete for Comboboxgadget - Simple version

Post by netmaestro »

For those who don't read (or perhaps believe) blogs, there is always this option:

Code: Select all

OpenWindow(0,0,0,320,240,"",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
ComboBoxGadget(0,20,40,200,20)
ComboBoxGadget(1,20,70,200,20, #PB_ComboBox_Image)
cn1$ = Space(50)
cn2$ = Space(50)
GetClassName_(GadgetID(0), @cn1$, 50)
GetClassName_(GadgetID(1), @cn2$, 50)
AddGadgetItem(0,-1,cn1$)
AddGadgetItem(1,-1,cn2$)
SetGadgetState(0,0)
SetGadgetState(1,0)

Repeat:Until WaitWindowEvent() = #PB_Event_CloseWindow
BERESHEIT
HanPBF
Enthusiast
Enthusiast
Posts: 570
Joined: Fri Feb 19, 2010 3:42 am

Re: Autocomplete for Comboboxgadget - Simple version

Post by HanPBF »

Thanks a lot for this auto complete example!

Why does it only work with
ComboBoxGadget(#PB_Any, X, Y, W, H, #PB_ComboBox_Image | #PB_ComboBox_Editable)


If I leave #PB_ComboBox_Image off the statement, FindwindowEx_ does result in 0...

Thanks!
Post Reply