Page 1 of 1

ComboBox whit editable : numberic chr only

Posted: Mon May 28, 2012 5:18 pm
by Tomi
hello
i need a editable combo box whit number input only.

Re: ComboBox whit editable : numberic chr only

Posted: Mon May 28, 2012 6:26 pm
by jassing
Tomi wrote:hello
i need a editable combo box whit number input only.
Something like this:
http://forum.purebasic.com/english/view ... &view=next

Re: ComboBox whit editable : numberic chr only

Posted: Mon May 28, 2012 6:51 pm
by IdeasVacuum
The sRod example limits the input to +integers only:

Code: Select all

#ES_NUMBER=$2000

If OpenWindow(0, 0, 0, 270, 440, "Test ComboBoxGadget",#PB_Window_SystemMenu | #PB_Window_ScreenCentered)
   ComboBoxGadget(0, 10, 10, 250, 20, #PB_ComboBox_Editable)

  ;--> Get the handle to the Edit control (child of the editable ComboBoxGadget)
  hComboEdit = FindWindowEx_(GadgetID(0), #Null, "Edit", #Null)
  SetWindowLongPtr_(hComboEdit, #GWL_STYLE, GetWindowLongPtr_(hComboEdit, #GWL_STYLE) | #ES_NUMBER)
 
  AddGadgetItem(0, -1, "123")
  AddGadgetItem(0, -1, "456")
  AddGadgetItem(0, -1, "789")
  AddGadgetItem(0, -1, "EDIT ME")
  SetGadgetState(0,0)
  Repeat

  Until WaitWindowEvent() = #PB_Event_CloseWindow

EndIf

End
If you need to handle negative values or floats/doubles, listen for #PB_EventType_Change on the combo and test the input char.
Edit: Changed Set/Get WindowLong to WindowLongPtr

Re: ComboBox whit editable : numberic chr only

Posted: Tue May 29, 2012 2:54 am
by Tomi
Many thanks from both, is very useful

Re: ComboBox whit editable : numberic chr only

Posted: Tue May 29, 2012 2:59 am
by Tomi
Is there a way to remove the tooltip? :?:

Re: ComboBox whit editable : numberic chr only

Posted: Tue May 29, 2012 3:07 am
by IdeasVacuum
I don't know how to stop the tooltip, but if you roll your own char check, there won't be a tooltip:

Code: Select all

Procedure Win()
;--------------

       If OpenWindow(0, 0, 0, 200, 140, "Combo Numbers",#PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar | #PB_Window_ScreenCentered)

                      ComboBoxGadget(1,10, 10,180,20,#PB_ComboBox_Editable)
                       AddGadgetItem(1,-1,"10")
                       AddGadgetItem(1,-1,"20")  
       EndIf

EndProcedure

Procedure WaitForUser()
;---------------------
 sInput.s = ""
sNumber.s = ""
  sChar.s = ""

     Repeat
                   iEvent.i = WaitWindowEvent(1)
            Select iEvent

                         Case #PB_Event_Gadget

                              If((EventGadget() = 1) And (EventType() = #PB_EventType_Change))

                                          sInput = GetGadgetText(1)
                                            iLen = Len(sInput)
                                         sNumber = ""
                                         
                                         For iCnt = 1 To iLen
                                         
                                                     sChar = Mid(sInput,iCnt,1)
                                                    iAscii = Asc(sChar)
                                              iIncludeChar = #True
                                         
                                              Select iAscii
                                                     Case 48, 49, 50, 51, 52, 53, 54, 55, 56, 57 ;Accepted numbers
                                                     Case 45, 46  ; Minus sign, accepted delimiter
                                                 Default: iIncludeChar = #False
                                              EndSelect
                                         
                                              If((iAscii = 45) And (iCnt > 1)) : iIncludeChar = #False : EndIf
                                              If(iIncludeChar = #True) : sNumber = sNumber + sChar : EndIf
                                         
                                         Next
                                         
                                               iLen = (Len(sNumber) + 1)
                                         SetGadgetText(1,"")
                                         SetGadgetText(1,sNumber)
                                          SendMessage_(GadgetID(1),#CB_SETEDITSEL,iLen,iLen)
                                          SendMessage_(GadgetID(1),#CB_SETEDITSEL,-1,-1)
                              EndIf      
            EndSelect

     Until iEvent = #PB_Event_CloseWindow

EndProcedure

;###Main Entry Point
Win()
WaitForUser()
End

Re: ComboBox whit editable : numberic chr only

Posted: Tue May 29, 2012 3:23 am
by RASHAD
Different approach
You can adapt it as you like

Code: Select all

Global ComboGadgetSC

Procedure ComboGadgetProc(hWnd,uMsg,wParam,lParam)
   Select uMsg
      Case #WM_CHAR
        If (wParam < 48 And wParam <> 8) Or wParam > 57
           ProcedureReturn 0
        EndIf
   EndSelect
   ProcedureReturn CallWindowProc_(ComboGadgetSC,hWnd,uMsg,wParam,lParam)
 EndProcedure

If OpenWindow(0, 0, 0, 270, 440, "Test ComboBoxGadget",#PB_Window_SystemMenu | #PB_Window_ScreenCentered)
   ComboBoxGadget(0, 10, 10, 250, 20, #PB_ComboBox_Editable)

  hComboEdit = FindWindowEx_(GadgetID(0), #Null, "Edit", #Null)
  ComboGadgetSC = SetWindowLongPtr_(hComboEdit,#GWL_WNDPROC,@ComboGadgetProc())
 
  AddGadgetItem(0, -1, "123")
  AddGadgetItem(0, -1, "456")
  AddGadgetItem(0, -1, "789")
  AddGadgetItem(0, -1, "EDIT ME")
  SetGadgetState(0,0)
  Repeat

  Until WaitWindowEvent() = #PB_Event_CloseWindow

EndIf

End


Re: ComboBox whit editable : numberic chr only

Posted: Tue May 29, 2012 4:38 am
by IdeasVacuum
.... or merge the two approaches and you get:

Code: Select all

Global gComboGadgetSC.i

Procedure ComboGadgetProc(hWnd,uMsg,wParam,lParam)
;------------------------------------------------

     Select uMsg

          Case #WM_CHAR

               Select wParam

                      Case 48, 49, 50, 51, 52, 53, 54, 55, 56, 57 ;Accepted numbers
                      Case 45, 46  ; Minus sign, accepted delimiter
                      Default: ProcedureReturn 0

               EndSelect

     EndSelect

   ProcedureReturn CallWindowProc_(gComboGadgetSC,hWnd,uMsg,wParam,lParam)

 EndProcedure

Procedure Win()
;--------------

     If OpenWindow(0, 0, 0, 270, 140, "ComboBox Floats",#PB_Window_SystemMenu | #PB_Window_ScreenCentered)

           ComboBoxGadget(1, 10, 10, 250, 20, #PB_ComboBox_Editable)

              ihComboEdit = FindWindowEx_(GadgetID(1), #Null, "Edit", #Null)
           gComboGadgetSC = SetWindowLongPtr_(ihComboEdit,#GWL_WNDPROC,@ComboGadgetProc())
 
           AddGadgetItem(1, -1, "123")
           AddGadgetItem(1, -1, "456")
           AddGadgetItem(1, -1, "789")
           AddGadgetItem(1, -1, "EDIT ME")
          SetGadgetState(1,3)

     EndIf

EndProcedure

Win()

Repeat

Until WaitWindowEvent() = #PB_Event_CloseWindow

End

Re: ComboBox whit editable : numberic chr only

Posted: Tue May 29, 2012 5:15 am
by RASHAD
Back to square one

Code: Select all

#ES_NUMBER=$2000

     
If OpenWindow(0, 0, 0, 270, 200, "Test ComboBoxGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  ComboBoxGadget(0, 10, 10, 250, 24, #PB_ComboBox_Editable) 

  ;--> Get the handle to the Edit control (child of the editable ComboBoxGadget) 
  hComboEdit = FindWindowEx_(GadgetID(0), #Null, "Edit", #Null) 
  SetWindowLong_(hComboEdit, #GWL_STYLE, GetWindowLong_(hComboEdit, #GWL_STYLE) | #ES_NUMBER)
  
  AddGadgetItem(0, -1, "123") 
  AddGadgetItem(0, -1, "456") 
  AddGadgetItem(0, -1, "789") 
  AddGadgetItem(0, -1, "EDIT ME") 
  SetGadgetState(0,0) 
  Repeat
  If IsGadget(0)
   SendMessage_(hComboEdit, #EM_HIDEBALLOONTIP, 0, 0)
  EndIf 
    event = WaitWindowEvent() 
    Select event 
      Case #PB_Event_Gadget 
        Select EventGadget() 
          Case 0 
             
        EndSelect 
    EndSelect 
  Until event = #PB_Event_CloseWindow 
EndIf 
End

Re: ComboBox whit editable : numberic chr only

Posted: Tue May 29, 2012 5:33 am
by IdeasVacuum
:mrgreen: At this rate, ComboBox is going to need it's very own forum :mrgreen:

Re: ComboBox whit editable : numberic chr only

Posted: Tue May 29, 2012 12:21 pm
by Tomi
Thanks to all, very well :D

Re: ComboBox whit editable : numberic chr only

Posted: Tue May 29, 2012 12:57 pm
by MachineCode
Don't forget to check for pasted input... the first example in this thread allows the user to paste text into it. ;)

Re: ComboBox whit editable : numberic chr only

Posted: Tue May 29, 2012 2:34 pm
by IdeasVacuum
Don't forget to check for pasted input... the first example in this thread allows the user to paste text into it.
...as does my snippet that allows negative numbers/floats and performs the char-check in the loop. :wink:

Re: ComboBox whit editable : numberic chr only

Posted: Tue May 29, 2012 4:27 pm
by Tomi
MachineCode wrote:Don't forget to check for pasted input... the first example in this thread allows the user to paste text into it. ;)
Incidentally, the same across and got help to solve the issue of the ClearClipboard()
Thank you and remind you of discerning
Thank you