String gadget problems

Just starting out? Need help? Post your questions and find answers here.
humungus
User
User
Posts: 14
Joined: Sat Jan 22, 2011 10:01 am

String gadget problems

Post by humungus »

Hi to all,

learn PB and trying to make useful utility. This should work on this way:
- enter starting hex numbers (4 chars) in String gadgets for starting point
- enter hex number in step gadget (2 chars)
- enter ending hex numbers (4 chars) for ending

Depending on step, it should print all generated values in binay file.

But (of course) I have problems:
- how to limit string gadget to accept only numbers from 0-9 and A-F ?
- how to convert that string in hex value (to be able to make calculations)?

For now I have this code:

Code: Select all

IncludeFile "test.pbf"

OpenWindow_0()

SetGadgetAttribute(String_0, #PB_String_MaximumLength, 4)
SetGadgetAttribute(String_1, #PB_String_MaximumLength, 4)
SetGadgetAttribute(String_2, #PB_String_MaximumLength, 4)
SetGadgetAttribute(String_3, #PB_String_MaximumLength, 4)
SetGadgetAttribute(String_4, #PB_String_MaximumLength, 2)

Procedure button_clicked(eventType)
  MessageRequester("Button Clicked:", "It should print in file....but not now")
EndProcedure



While Window_0_Events(WaitWindowEvent()): Wend

Form looks like this (it is easier to understand what I`m trying to do):

Image
Little John
Addict
Addict
Posts: 4778
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: String gadget problems

Post by Little John »

humungus wrote:- how to limit string gadget to accept only numbers from 0-9 and A-F ?
- how to convert that string in hex value (to be able to make calculations)?
The following code shows a way how to do so.

Code: Select all

; PB 5.31

EnableExplicit

;-----------------------------------------------------------------------
; Only the following characters are accepted by the stringgadget:
#AllowedChars$ = "0123456789ABCDEF"
;-----------------------------------------------------------------------

; Gadgets
Enumeration
   #str
   #btn
EndEnumeration


Procedure.i HasBadCharacter (source$, allowed$)
   ; -- check whether source$ contains any character that is not in allowed$
   ; return value: #True / #False
   Protected *s.Character
   
   *s = @source$
   While *s\c <> 0
      If FindString(allowed$, Chr(*s\c)) = 0
         ProcedureReturn #True
      EndIf  
      *s + SizeOf(Character)
   Wend

   ProcedureReturn #False
EndProcedure


Define content$, newContent$
Define event.i, start.i, fin.i

OpenWindow(0, 0, 0, 300, 100, "StringGadget demo", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
StringGadget(#str, 5, 10, 290, 20, "FF")
ButtonGadget(#btn, 100, 50, 90, 30, "Get value")

content$ = GetGadgetText(#str)
Repeat
   event = WaitWindowEvent()
   Select event
      Case #PB_Event_Gadget
         Select EventGadget()
            Case #str
               Select EventType()
                  Case #PB_EventType_Change
                     SendMessage_(GadgetID(#str), #EM_GETSEL, @start, @fin)         ; only for Windows
                     newContent$ = GetGadgetText(#str)
                     If HasBadCharacter(newContent$, #AllowedChars$)
                        SetGadgetText(#str, content$)
                        SendMessage_(GadgetID(#str), #EM_SETSEL, start-1, start-1)  ; only for Windows
                     Else
                        content$ = newContent$
                     EndIf   
               EndSelect
               
            Case #btn
               Debug "Value: " + Val("$" + content$)
         EndSelect
   EndSelect      
Until event = #PB_Event_CloseWindow
User avatar
electrochrisso
Addict
Addict
Posts: 989
Joined: Mon May 14, 2007 2:13 am
Location: Darling River

Re: String gadget problems

Post by electrochrisso »

Nice Little John, code added to my archive. :)
PureBasic! Purely the best 8)
Little John
Addict
Addict
Posts: 4778
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: String gadget problems

Post by Little John »

Thank you, electrochrisso! :-)
Post Reply