ComboBox whit editable : numberic chr only

Just starting out? Need help? Post your questions and find answers here.
User avatar
Tomi
Enthusiast
Enthusiast
Posts: 270
Joined: Wed Sep 03, 2008 9:29 am

ComboBox whit editable : numberic chr only

Post by Tomi »

hello
i need a editable combo box whit number input only.
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: ComboBox whit editable : numberic chr only

Post 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
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: ComboBox whit editable : numberic chr only

Post 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
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
Tomi
Enthusiast
Enthusiast
Posts: 270
Joined: Wed Sep 03, 2008 9:29 am

Re: ComboBox whit editable : numberic chr only

Post by Tomi »

Many thanks from both, is very useful
User avatar
Tomi
Enthusiast
Enthusiast
Posts: 270
Joined: Wed Sep 03, 2008 9:29 am

Re: ComboBox whit editable : numberic chr only

Post by Tomi »

Is there a way to remove the tooltip? :?:
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: ComboBox whit editable : numberic chr only

Post 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
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: ComboBox whit editable : numberic chr only

Post 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

Egypt my love
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: ComboBox whit editable : numberic chr only

Post 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
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: ComboBox whit editable : numberic chr only

Post 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
Egypt my love
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: ComboBox whit editable : numberic chr only

Post by IdeasVacuum »

:mrgreen: At this rate, ComboBox is going to need it's very own forum :mrgreen:
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
Tomi
Enthusiast
Enthusiast
Posts: 270
Joined: Wed Sep 03, 2008 9:29 am

Re: ComboBox whit editable : numberic chr only

Post by Tomi »

Thanks to all, very well :D
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: ComboBox whit editable : numberic chr only

Post by MachineCode »

Don't forget to check for pasted input... the first example in this thread allows the user to paste text into it. ;)
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: ComboBox whit editable : numberic chr only

Post 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:
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
Tomi
Enthusiast
Enthusiast
Posts: 270
Joined: Wed Sep 03, 2008 9:29 am

Re: ComboBox whit editable : numberic chr only

Post 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
Post Reply