ComboBoxGadget tab-stop workaround (Windows)

Share your advanced PureBasic knowledge/code with the community.
breeze4me
Enthusiast
Enthusiast
Posts: 633
Joined: Thu Mar 09, 2006 9:24 am
Location: S. Kor

ComboBoxGadget tab-stop workaround (Windows)

Post by breeze4me »

Edit:
the problem fixed, so it no longer needs. (PB 4.50 RC1+)


Newly introduced ComboBoxGadget(non-editable ComboBoxEx) doesn't work tab and shift + tab key for moving the keyboard focus, so needs a trick.
See the following code.

Code: Select all

#CBEM_GETEDITCONTROL = #WM_USER + 7 ;$407
#CBEM_GETCOMBOCONTROL = #WM_USER + 6  ;$406

Macro GetComboBoxEditHandle(hwndCombo)
  SendMessage_(hwndCombo, #CBEM_GETEDITCONTROL, 0, 0)
EndMacro

Procedure SetTabStop(gadget)  ;for non-editable ComboBox. PB v4.50 beta3+
  Protected hwnd
  If IsGadget(gadget)
    hwnd = GadgetID(gadget)
    SetWindowLongPtr_(hwnd, #GWL_STYLE, GetWindowLongPtr_(hwnd, #GWL_STYLE) & ~#WS_TABSTOP) ;Shift + Tab
    ;hwnd = FindWindowEx_(hwnd, 0, 0, 0)
    hwnd = SendMessage_(hwnd, #CBEM_GETCOMBOCONTROL, 0, 0)
    SetWindowLongPtr_(hwnd, #GWL_STYLE, GetWindowLongPtr_(hwnd, #GWL_STYLE) | #WS_TABSTOP)  ;Tab
  EndIf
EndProcedure

Enumeration
  #Gadget1
  #Gadget2
  #Gadget3
  #Gadget4
EndEnumeration

If OpenWindow(0, 100, 100, 300, 200, "Combo Test")
  
  ButtonGadget(4, 50, 10, 200, 20, "Button")
  
  ComboBoxGadget(#Gadget1, 50, 50, 200, 20,#PB_ComboBox_Editable)
  AddGadgetItem(#Gadget1, -1, "test1")
  AddGadgetItem(#Gadget1, -1, "test2")
  SetGadgetState(#Gadget1, 0)
  
  ComboBoxGadget(#Gadget2, 50, 80, 200, 20,#PB_ComboBox_Editable)
  AddGadgetItem(#Gadget2, -1, "test1")
  AddGadgetItem(#Gadget2, -1, "test2")
  SetGadgetState(#Gadget2, 0)
  
  ComboBoxGadget(#Gadget3, 50, 110, 200, 20)
  AddGadgetItem(#Gadget3, -1, "test1")
  AddGadgetItem(#Gadget3, -1, "test2")
  SetGadgetState(#Gadget3, 0)
  SetTabStop(#Gadget3)
  
  ComboBoxGadget(#Gadget4, 50, 140, 200, 20)
  AddGadgetItem(#Gadget4, -1, "test1")
  AddGadgetItem(#Gadget4, -1, "test2")
  SetGadgetState(#Gadget4, 0)
  SetTabStop(#Gadget4)
  
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow

EndIf